hash function

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

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'}) {
  if (content is! Uint8List && content is! String) {
    throw "'content' must be a string or Uint8List";
  }

  if (content is String) {
    if (isHex(content)) {
      content = Uint8List.fromList(utf8.encode(content));
    } else {
      content = Uint8List.fromList(content.codeUnits);
    }
  }

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

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