validate method

void validate()

Validate this PHC string. Throws ArgumentError when any field is invalid.

Implementation

void validate() {
  final digitRe = RegExp(r'^[0-9]+$');
  final alnumRe = RegExp(r'^[a-z0-9-]{1,32}$');
  final base64Re = RegExp(r'^[a-zA-Z0-9/+.-]+$');

  // id
  if (!alnumRe.hasMatch(id)) {
    throw ArgumentError.value(
        id, 'id', 'must be [a-z0-9-] and under 32 characters');
  }

  // version (optional)
  if (version != null && !digitRe.hasMatch(version!)) {
    throw ArgumentError.value(version, 'version', 'must be decimal digits');
  }

  // params (optional)
  if (params != null) {
    for (final e in params!.entries) {
      final k = e.key;
      final v = e.value;
      if (!alnumRe.hasMatch(k)) {
        throw ArgumentError.value(
            k, 'params.key', 'must be [a-z0-9-] and under 32 chars');
      }
      if (k == 'v') {
        throw ArgumentError.value(
            k, 'params.key', 'reserved; use version field instead');
      }
      if (v.isEmpty) {
        throw ArgumentError.value(v, 'params[$k]', 'value is empty');
      } else if (!base64Re.hasMatch(v)) {
        throw ArgumentError.value(
            v, 'params[$k]', 'value has invalid characters');
      }
    }
  }

  // salt (optional)
  if (salt != null && !base64Re.hasMatch(salt!)) {
    throw ArgumentError.value(
        salt, 'salt', 'expected base64 string without padding');
  }

  // hash (optional)
  if (hash != null && !base64Re.hasMatch(hash!)) {
    throw ArgumentError.value(
        hash, 'hash', 'expected base64 string without padding');
  }
}