CFML implementation of Password-Based Key-Derivation Function (PBKDF)
generatePBKDFKey(algorithm, passphrase, salt, iterations, keySize);
→ returns string
PBKDF2WithHmacSHA1
PBKDF2WithSHA1
PBKDF2WithSHA224
PBKDF2WithSHA256
PBKDF2WithSHA384
PBKDF2WithSHA512
PBKDF2WithHmacSHA256
PBKDF2WithHmacSHA384
PBKDF2WithHmacSHA512
PBKDF2WithHmacSHA512
) .
iterations
and keySize
parameters are optional in Lucee.
The PBKDF2WithHmacSHA1
algorithm will work on older JVMs, or older versions of CF
generatePBKDFKey("PBKDF2WithHmacSHA1", "secret", "salty", 5000, 128)
Expected Result: Y0MCpCe3zb0CNJvyXNUWEQ==
// some variables
password = "top_secret";
dataToEncrypt= "the most closely guarded secret";
encryptionAlgorithm = "AES";
keysize = 128;
algorithmVersion = 512;
PBKDFalgorithm = 'PBKDF2WithHmacSHA' & algorithmVersion;
// Generate key as recommended in docs
length = keysize / 8;
multiplicator = 10 ^ length;
salt = Round(Randomize(5,'SHA1PRNG') * multiplicator);
// The magic happens here
PBKDFKey = GeneratePBKDFKey(PBKDFalgorithm, password, salt, algorithmVersion, keysize);
encryptedData = encrypt(dataToEncrypt, PBKDFKey, encryptionAlgorithm, "BASE64");
decryptedData = decrypt(encryptedData, PBKDFKey, encryptionAlgorithm, "BASE64");
//Output
writeOutput("<b>Generated PBKDFKey (Base 64)</b>: " & PBKDFKey);
writeOutput("<br /><b>Data After Encryption</b>: " & encryptedData);
writeOutput("<br /><b>Data After Decryption</b>: " & decryptedData);
Signup for cfbreak
to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.