Skip to content

Commit

Permalink
feat: custom KDF hasher
Browse files Browse the repository at this point in the history
  • Loading branch information
Alanscut committed Sep 3, 2021
1 parent 0229694 commit 09ee2ab
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/cipher-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -765,14 +765,19 @@ CryptoJS.lib.Cipher || (function (undefined) {
* var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32);
* var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32, 'saltsalt');
*/
execute: function (password, keySize, ivSize, salt) {
execute: function (password, keySize, ivSize, salt, hasher) {
// Generate random salt
if (!salt) {
salt = WordArray.random(64/8);
}

// Derive key and IV
var key = EvpKDF.create({ keySize: keySize + ivSize }).compute(password, salt);
if (!hasher) {
var key = EvpKDF.create({ keySize: keySize + ivSize }).compute(password, salt);
} else {
var key = EvpKDF.create({ keySize: keySize + ivSize, hasher: hasher }).compute(password, salt);
}


// Separate key and IV
var iv = WordArray.create(key.words.slice(keySize), ivSize * 4);
Expand Down Expand Up @@ -819,7 +824,7 @@ CryptoJS.lib.Cipher || (function (undefined) {
cfg = this.cfg.extend(cfg);

// Derive key and other params
var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, cfg.salt);
var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, cfg.salt, cfg.hasher);

// Add IV to config
cfg.iv = derivedParams.iv;
Expand Down Expand Up @@ -858,7 +863,7 @@ CryptoJS.lib.Cipher || (function (undefined) {
ciphertext = this._parse(ciphertext, cfg.format);

// Derive key and other params
var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, ciphertext.salt);
var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, ciphertext.salt, cfg.hasher);

// Add IV to config
cfg.iv = derivedParams.iv;
Expand Down
25 changes: 25 additions & 0 deletions test/config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,31 @@ YUI.add('config-test', function (Y) {
var encryptedB = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltB });
Y.Assert.areEqual('Test', C.AES.decrypt(encryptedA, 'Pass').toString(C.enc.Utf8));
Y.Assert.areEqual('Test', C.AES.decrypt(encryptedB, 'Pass').toString(C.enc.Utf8));
},

testCustomKDFHasher: function () {
//SHA1
let encryptedSHA1 = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA, hasher: C.algo.SHA1}).toString();
Y.Assert.areEqual('Test', C.AES.decrypt(encryptedSHA1, 'Pass', { hasher: C.algo.SHA1}).toString(C.enc.Utf8));

//SHA256
let encryptedSHA256 = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA, hasher: C.algo.SHA256}).toString();
Y.Assert.areEqual('Test', C.AES.decrypt(encryptedSHA256, 'Pass', { hasher: C.algo.SHA256}).toString(C.enc.Utf8));

//SHA512
let encryptedSHA512 = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA, hasher: C.algo.SHA512}).toString();
Y.Assert.areEqual('Test', C.AES.decrypt(encryptedSHA512, 'Pass', { hasher: C.algo.SHA512}).toString(C.enc.Utf8));

//Default: MD5
let encryptedDefault = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA }).toString();
let encryptedMD5 = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA, hasher: C.algo.MD5}).toString();
Y.Assert.areEqual('Test', C.AES.decrypt(encryptedMD5, 'Pass', { hasher: C.algo.MD5}).toString(C.enc.Utf8));
Y.Assert.areEqual(encryptedDefault, encryptedMD5);

//Different KDFHasher
Y.Assert.areNotEqual(encryptedDefault, encryptedSHA1);
Y.Assert.areNotEqual(encryptedDefault, encryptedSHA256);
Y.Assert.areNotEqual(encryptedDefault, encryptedSHA512);
}
}));
}, '$Rev$');

0 comments on commit 09ee2ab

Please sign in to comment.