hashBytes function
Computes hash values for the given bytes.
If isBinary is true, or if isBinary is null and isBinaryContent detects
a NUL byte, it falls back to raw hashing only where the normalized hash matches
the raw hash.
Otherwise, it attempts to decode the bytes as UTF-8. If decoding succeeds, it normalizes the line endings (CRLF and lone CR to LF) and computes the normalized hash. If decoding fails, it returns InvalidUtf8Result.
Implementation
HashResult hashBytes(List<int> bytes, {bool? isBinary}) {
final bool binary = isBinary ?? isBinaryContent(bytes);
final String rawHash = computeRawHash(bytes);
if (binary) {
return SuccessfulHash(
rawHash: rawHash,
normalizedHash: rawHash,
isBinary: true,
);
}
try {
final String decoded = utf8.decode(bytes);
final String normalized = normalizeLineEndings(decoded);
final String normalizedHash = computeRawHash(utf8.encode(normalized));
return SuccessfulHash(
rawHash: rawHash,
normalizedHash: normalizedHash,
isBinary: false,
);
} on FormatException catch (e) {
return InvalidUtf8Result(message: e.message, bytes: bytes);
}
}