Skip to content

A Dart package for hashing and comparing strings using the bcrypt algorithm

License

Notifications You must be signed in to change notification settings

flutterpilot/fpd_bcrypt

Repository files navigation

fpd_bcrypt

Pub Version Dart CI License: MIT

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.

🔐 Features

  • 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 Result types for all operations.

📦 Installation

Add this to your package's pubspec.yaml file:

dependencies:
  fpd_bcrypt: ^1.0.0

Then run:

dart pub get

🚀 Quick Start

Here'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}');
  }
}

📚 API Reference

Core Methods

FpdBcrypt.hashWithSalt({required String text, int logRounds = 10})

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.

FpdBcrypt.compare({required String text, required String hash})

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.

FpdBcrypt.generateSalt({int logRounds = 10})

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.

FpdBcrypt.hash({required String text, required String salt})

Hashes a string using a pre-existing salt.

  • text: The plain text string.
  • salt: The salt generated from generateSalt.

Returns a Result<String> with the hash on success.

Data Types

Result<T>

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 type T).
  • error: A String message describing the failure.

🧪 Testing

This package comes with a comprehensive test suite. To run the tests:

dart test

To view test coverage:

dart test --coverage=coverage
genhtml coverage/lcov.info -o coverage/html

🤝 Contributing

Contributions are welcome! If you find a bug or want to suggest a new feature, please open an issue.

  1. Fork the repository.
  2. Create your feature branch (git checkout -b feature/my-new-feature).
  3. Commit your changes (git commit -am 'Add some feature').
  4. Push to the branch (git push origin feature/my-new-feature).
  5. Create a new Pull Request.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.


Made with ❤️ by the FlutterPilot team

About

A Dart package for hashing and comparing strings using the bcrypt algorithm

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages