Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert pbkdf2 result to string? #32

Closed
winnie-chaintope opened this issue Sep 7, 2020 · 1 comment
Closed

Convert pbkdf2 result to string? #32

winnie-chaintope opened this issue Sep 7, 2020 · 1 comment

Comments

@winnie-chaintope
Copy link

winnie-chaintope commented Sep 7, 2020

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)
@RimaitosLab
Copy link

RimaitosLab commented Sep 14, 2020

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants