Skip to content

Instantly share code, notes, and snippets.

@deiu
Last active February 13, 2020 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deiu/34c47cccd2545fd8a7dcf5114a0fb231 to your computer and use it in GitHub Desktop.
Save deiu/34c47cccd2545fd8a7dcf5114a0fb231 to your computer and use it in GitHub Desktop.
Generate AES key using PBDKF2 password derivation
import 'package:steel_crypt/steel_crypt.dart';
import 'dart:convert';
void main() async {
final passHash = PassCrypt('SHA-256/HMAC/PBKDF2');
final ivsalt = CryptKey().genDart(16);
final derivationPassword = 'foobar';
final derivedHash = passHash.hashPass(ivsalt, derivationPassword);
// prepare the key to be used by AesCrypt
final derivedBytes = base64Decode(derivedHash);
final derivedKey = String.fromCharCodes(derivedBytes);
// generate the AES key using the derived bytes
final aesEncrypter = AesCrypt(derivedKey);
final encrypted = aesEncrypter.encrypt('super secret text', ivsalt);
print('Encrypted msg: $encrypted');
// generated the AES key a 2nd time using the same derived bytes, then use it to decrypt
final aesEncrypter2 = AesCrypt(derivedKey);
print('Decrypted msg: ${aesEncrypter2.decrypt(encrypted, ivsalt)}');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment