cryptography icon indicating copy to clipboard operation
cryptography copied to clipboard

Convert pbkdf2 result to string?

Open winnie-chaintope opened this issue 5 years ago • 1 comments

final pbkdf2 = Pbkdf2(
  macAlgorithm: Hmac(sha256),
  iterations: 100000,
  bits: 128,
);

final nonce = Nonce(base64Decode('EQsBDQcMBQEWBAsaFBkUEQ=='));
print(nonce);
final hashBytes = await pbkdf2.deriveBits(
  utf8.encode('Lorem ipsum dolor sit amet, consetetur...'),
  nonce: nonce,
);
print('Hash: $hashBytes');

String result = utf8.decode(hashBytes); <----- **HOW DO I CONVERT THIS TO STRING? NEED A STRING FORMAT.**
print(result);

i got an error when i tried to use utf8.decode to decode the hashBytes. any other way to convert to string? or this a bug? please help. thanks

Results:

flutter: Nonce(['17, 11, 1, 13, 7, 12, 5, 1, 22, 4, 11, 26, 20, 25, 20, 17'])
flutter: Hash: [35, 222, 169, 18, 135, 61, 210, 235, 7, 169, 94, 202, 113, 77, 103, 77]
[VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: FormatException: Unexpected extension byte (at offset 4)

winnie-chaintope avatar Sep 07 '20 11:09 winnie-chaintope

PBDKF2 returns raw data. It isn't encoded in UTF-8 and as such can't be used with utf8.decode(). If you want to have a String you have to encode the data. Standart is to either use Hex encoding which turns every byte in two chars or to use Base64 which turns every 3 bytes to 4 chars.

So the final line could look like String result = base64Encode(hashBytes)

RimaitosLab avatar Sep 14 '20 13:09 RimaitosLab