BcryptContext.fromEncoded constructor

BcryptContext.fromEncoded(
  1. CryptData data
)

Creates an BcryptContext instance from encoded string.

The encoded string may look like this: $2a$12$WApznUOJfkEGSmYRfnkrPOr466oFDCaj4b6HY3EXGvfxm43seyhgC

Implementation

factory BcryptContext.fromEncoded(CryptData data) {
  var version = _nameToVersion(data.id);
  var cost = int.tryParse(data.salt ?? '0');
  if (cost == null) {
    throw FormatException('Invalid cost');
  }
  Uint8List? salt;
  var hash = data.hash;
  if (hash != null) {
    if (hash.length != 22 && hash.length != 53) {
      throw FormatException('Invalid hash');
    }
    salt = fromBase64(
      hash.substring(0, 22),
      codec: Base64Codec.bcrypt,
    );
  }
  return BcryptContext(
    salt: salt,
    cost: cost,
    version: version,
  );
}