digest function

LivrRule digest(
  1. List<Object?> args,
  2. RuleContext context
)

digest: the value must be a hexadecimal digest of one of the given algorithms, md5 (32 digits), sha1 (40), sha256 (64), sha384 (96) or sha512 (128): {digest: 'sha256'}, {digest: ['md5', 'sha1']}. The output is the value as a string.

Implementation

LivrRule digest(List<Object?> args, RuleContext context) {
  if (args.isEmpty) {
    throw const LivrSchemaException(
      'Rule [digest] expects one or more algorithms',
    );
  }

  final lengths = List<int>.generate(args.length, (i) {
    final length = _hexLengths[args[i]];
    if (length == null) {
      throw LivrSchemaException(
        'Rule [digest] got an unknown algorithm [${args[i]}]',
      );
    }
    return length;
  }, growable: false);

  return (value, allValues) => _check(value, (str) {
    final length = str.length;
    if (!lengths.contains(length)) return false;
    for (var i = 0; i < length; i++) {
      if (!_isHexChar(str.codeUnitAt(i))) return false;
    }
    return true;
  });
}