dint-dev / cryptography

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

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

Closed yeikel16 closed 5 months ago

yeikel16 commented 9 months ago

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

yeikel16 commented 5 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);
}