verify method

Result<bool> verify(
  1. String value,
  2. String hash
)

Verifies if a value matches a hash. This is a convenience method that hashes the value and compares. Uses constant-time comparison to prevent timing attacks. Returns Result<bool> - true if value matches hash, false otherwise.

Implementation

Result<bool> verify(String value, String hash) {
  final hashResult = this.hash(value);
  if (hashResult.isError) {
    return Result.error(hashResult.asError!.error);
  }

  final computed = hashResult.asValue!.value;

  // Constant-time comparison to prevent timing attacks
  var result = computed.length ^ hash.length;
  final minLength =
      computed.length < hash.length ? computed.length : hash.length;

  for (var i = 0; i < minLength; i++) {
    result |= computed.codeUnitAt(i) ^ hash.codeUnitAt(i);
  }

  return Result.value(result == 0);
}