I'm building a simple encryption tool in dart.
I need to encrypt my rsa private key with a pass phrase.
I'm using AES to do the encryption.
AES requires keys to be a specific no. of bits.
In my case 256 bits.
I'm requiring that the user enter a passphrase of at least 16 characters (128 bits).
As such I need to extend the passphrase to a full 256 bits.
Currently I just double the pass phrase and then clip the result to 256 bits.
Is this the correct thing to do or is the a more secure method of padding the key?
I'm using the dart pointycastle libraries which are based on the java bouncycastle libraries.
Update
Here is my latest attempt
class StrongKey extends Key {
StrongKey.fromPassPhrase(String passPhrase) : super.fromUtf8(passPhrase);
Key secureStretch(Uint8List salt) {
return stretch(256, iterationCount: 100000, salt: salt);
}
@override
Key stretch(int desiredKeyLength, {int iterationCount = 100, Uint8List salt}) {
final params = Pbkdf2Parameters(salt, iterationCount, desiredKeyLength);
final pbkdf2 = PBKDF2KeyDerivator(Mac('SHA-512/HMAC'))..init(params);
return Key(pbkdf2.process(bytes));
}
static Uint8List get generateSalt => SecureRandom(256).bytes;
}