Last active
February 13, 2020 15:50
-
-
Save deiu/34c47cccd2545fd8a7dcf5114a0fb231 to your computer and use it in GitHub Desktop.
Generate AES key using PBDKF2 password derivation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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