hashRfc5054 function
Future<Uint8List>
hashRfc5054({
- required List<
Uint8List> byteLists, - required Uint8List safePrime,
- required HashFunction hashFunction,
Hashing procedure required by RFC5054 for certain variables.
RFC5054 requires left-padding pre-concatenated bytes with zeros if bytes length less than that of the safe prime.
Parameters:
byteLists
: List of byte arrays to concatenate and hash.safePrime
: Safe prime bytes used to determine padding length.hashFunction
: Hash algorithm to use (e.g., SHA-256).
Returns: Hash of the zero-padded, concatenated byte arrays.
Implementation
Future<Uint8List> hashRfc5054({
required List<Uint8List> byteLists,
required Uint8List safePrime,
required HashFunction hashFunction
}) async {
// Build the padded byte array.
int totalSize = byteLists.length * safePrime.length;
final bytes = Uint8List(totalSize);
int offset = 0;
for (var byteList in byteLists) {
// Left-pad with zeros.
final paddingLength = safePrime.length - byteList.length;
// Since Uint8List is zero-initialized, just copy the actual bytes.
bytes.setRange(offset + paddingLength, offset + safePrime.length, byteList);
offset += safePrime.length;
}
final hash = await hashFunction.hash(bytes);
return hash;
}