dint-dev / cryptography

Cryptography for Flutter developers: encryption, digital signatures, key agreement, etc.
https://pub.dev/packages/cryptography
Apache License 2.0
162 stars 90 forks source link

How to convert a Sha256 hash result to a string #164

Closed yeikel16 closed 10 months ago

yeikel16 commented 1 year ago

Hi, I building a Sha256 hash but I need the string from bytes list. How I can get this?

yeikel16 commented 10 months ago

I build my hashResult

final argument = 'This is my String argument';
final algorithm = Sha256();
final argumentHash = utf8.encode(argument);
final hashResult = await algorithm.hash(argumentHash);

and convert the hashResult to String with this function:

static String _hexEncode(List<int> bytes) {
    const hexDigits = '0123456789abcdef';
    var charCodes = Uint8List(bytes.length * 2);
    for (var i = 0, j = 0; i < bytes.length; i++) {
      var byte = bytes[i];
      charCodes[j++] = hexDigits.codeUnitAt((byte >> 4) & 0xF);
      charCodes[j++] = hexDigits.codeUnitAt(byte & 0xF);
    }
    return String.fromCharCodes(charCodes);
}