Crypt constructor

Crypt(
  1. String cryptFormatStr
)

Creates a crypt from a crypt format string.

Produce a Crypt object from parsing a crypt format string.

Throws FormatException or RangeError if the crypt format string is incorrect.

Implementation

Crypt(String cryptFormatStr) {
  // TODO(any): implement without using split, since salt could contain a $

  final parts = cryptFormatStr.split(r'$');
  if ((parts.length == 4 || parts.length == 5) && parts[0].isEmpty) {
    _type = parts[1];

    if (_type == idSha256 || _type == idSha512) {
      // SHA-256

      // Get the rounds (if any)

      if (parts.length == 5) {
        // Parse explicitly specified rounds
        const roundsStr = 'rounds=';
        if (!parts[2].startsWith(roundsStr)) {
          throw FormatException('Crypt string invalid rounds: ${parts[2]}');
        }
        try {
          final r = int.parse(parts[2].substring(roundsStr.length));
          if (r < _minShaRounds || _maxShaRounds < r) {
            throw RangeError('Crypt string rounds out of range: $r');
          }
          _rounds = r;
        } on FormatException catch (_) {
          throw FormatException('Crypt string invalid rounds: ${parts[2]}');
        }
      } else {
        // No explicit rounds specified
        _rounds = null; // default rounds
      }

      // Get the salt

      _salt = parts[parts.length - 2];
      if (_maxShaSaltLength < salt.length) {
        throw const FormatException('Crypt string unexpected salt length');
      }

      // Get the hash

      _hash = parts[parts.length - 1];
      if (_hash.isEmpty) {
        throw const FormatException('Crypt string empty hash');
      }
    } else {
      throw FormatException(
          'Crypt string: unsupported algorithm: ${parts[1]}');
    }
  } else {
    throw const FormatException('Crypt string invalid format');
  }
}