hash function

Uint8List hash(
  1. dynamic content, {
  2. String algo = 'sha256',
  3. bool isContentHexa = true,
})

Create a hash digest from the data with an hash algorithm identification prepending the digest @param {String | Uint8List} content Data to hash (string or buffer) @param {String} algo Hash algorithm ("sha256", "sha512", "sha3-256", "sha3-512", "blake2b")

Implementation

Uint8List hash(
  dynamic content, {
  String algo = 'sha256',
  bool isContentHexa = true,
}) {
  if (content is! List<int> && content is! String) {
    throw "'content' must be a string or Uint8List";
  }

  if (content is String) {
    if (isContentHexa && !isHex(content)) {
      throw const FormatException("'content' must be an hexadecimal string");
    }

    if (isContentHexa) {
      content = Uint8List.fromList(utf8.encode(content));
    } else {
      content = Uint8List.fromList(content.codeUnits);
    }
  }

  final algoID = hashAlgoToID(algo);
  final digest = getHashDigest(content, algo);

  return concatUint8List(<Uint8List>[
    Uint8List.fromList(<int>[algoID]),
    digest,
  ]);
}