A Dart package for hashing and comparing strings using the bcrypt algorithm. It provides a simple, secure, and effective way to handle password storage and verification.
This package is a lightweight wrapper around the excellent bcrypt package, offering a simplified static API with a Result type for robust error handling.
- ✅ Secure Hashing: Hash strings using the battle-tested bcrypt algorithm.
- ✅ Easy Comparison: Safely compare plain text with a hash to verify passwords.
- ✅ Salt Generation: Generate secure salts with a configurable work factor (log rounds).
- ✅ Simplified API: A clean, static API that requires no instantiation.
- ✅ Robust Error Handling: Clear success/failure
Resulttypes for all operations.
Add this to your package's pubspec.yaml file:
dependencies:
fpd_bcrypt: ^1.0.0Then run:
dart pub getHere's a simple example of hashing a password and then verifying it.
import 'package:fpd_bcrypt/fpd_bcrypt.dart';
void main() {
final textToHash = 'mySuperSecurePassword';
// 1. Generate a hash with a new, automatically generated salt
final hashResult = FpdBcrypt.hashWithSalt(text: textToHash, logRounds: 10);
if (hashResult.isSuccess) {
final hash = hashResult.data!;
print('Original Text: $textToHash');
print('Generated Hash: $hash');
// 2. Compare the original text with the generated hash
final compareResult = FpdBcrypt.compare(text: textToHash, hash: hash);
if (compareResult.isSuccess) {
if (compareResult.data!) {
print('✅ The password and hash match!');
} else {
print('❌ The password and hash do not match.');
}
} else {
print('Error during comparison: ${compareResult.error}');
}
} else {
print('Error during hashing: ${hashResult.error}');
}
}Hashes a string using a newly generated salt. This is the recommended method for creating new hashes.
text: The plain text string to hash.logRounds: The work factor. Higher is more secure but slower. Defaults to 10.
Returns a Result<String> with the hash on success.
Compares a plain text string against a bcrypt hash. This method is constant-time to prevent timing attacks.
text: The plain text string.hash: The hash to compare against.
Returns a Result<bool> which is true if they match, false otherwise.
Generates a salt string. Useful if you need to manage the salt separately.
logRounds: The work factor. Defaults to 10.
Returns the generated salt string.
Hashes a string using a pre-existing salt.
text: The plain text string.salt: The salt generated fromgenerateSalt.
Returns a Result<String> with the hash on success.
A generic wrapper for operations that can either succeed or fail.
isSuccess: A boolean indicating if the operation was successful.isFailure: A boolean indicating if the operation failed.data: The result of the operation on success (of typeT).error: AStringmessage describing the failure.
This package comes with a comprehensive test suite. To run the tests:
dart testTo view test coverage:
dart test --coverage=coverage
genhtml coverage/lcov.info -o coverage/htmlContributions are welcome! If you find a bug or want to suggest a new feature, please open an issue.
- Fork the repository.
- Create your feature branch (
git checkout -b feature/my-new-feature). - Commit your changes (
git commit -am 'Add some feature'). - Push to the branch (
git push origin feature/my-new-feature). - Create a new Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
Made with ❤️ by the FlutterPilot team