Skip to content

Commit

Permalink
use 15k iterations in PBKDF2 in backward compatible way
Browse files Browse the repository at this point in the history
  • Loading branch information
robinmoisson committed Apr 19, 2023
1 parent b797d78 commit 54cd15a
Show file tree
Hide file tree
Showing 6 changed files with 391 additions and 373 deletions.
40 changes: 26 additions & 14 deletions cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,20 @@ try{
* Salt and encrypt a msg with a password.
* Inspired by https://github.com/adonespitogo
*/
var keySize = 256;
var iterations = 1000;
var pbkdf2Parameters = {
keySize: 256/32,
iterations: 1000,
};

if (isTemplateSupporting15kIterations()) {
pbkdf2Parameters.iterations = 15000;
pbkdf2Parameters.hasher = CryptoJS.algo.SHA256;
}

function encrypt (msg, password) {
var salt = CryptoJS.lib.WordArray.random(128/8);

var key = CryptoJS.PBKDF2(password, salt, {
keySize: keySize/32,
iterations: iterations
});

var key = CryptoJS.PBKDF2(password, salt, pbkdf2Parameters);

var iv = CryptoJS.lib.WordArray.random(128/8);

Expand All @@ -90,7 +95,7 @@ function encrypt (msg, password) {

// salt, iv will be hex 32 in length
// append them to the ciphertext for use in decryption
var encryptedMsg = salt.toString()+ iv.toString() + encrypted.toString();
var encryptedMsg = salt.toString() + iv.toString() + encrypted.toString();
return encryptedMsg;
}

Expand Down Expand Up @@ -123,19 +128,26 @@ var data = {

genFile(data);

function isTemplateSupporting15kIterations() {
return getTemplateContent().includes("// STATICRYPT VERSION: >= 1.4.3");
}

function getTemplateContent() {
try {
return FileSystem.readFileSync(namedArgs.f, 'utf8');
} catch (e) {
console.log("Failure: could not read template!");
process.exit(1);
}
}

/**
* Fill the template with provided data and writes it to output file.
*
* @param data
*/
function genFile(data){
try{
var templateContents = FileSystem.readFileSync(namedArgs.f, 'utf8');
}catch(e){
console.log("Failure: could not read template!");
process.exit(1);
}
var templateContents = getTemplateContent();

var renderedTemplate = render(templateContents, data);

Expand Down
Loading

0 comments on commit 54cd15a

Please sign in to comment.