Skip to content

Commit 54cd15a

Browse files
committed
use 15k iterations in PBKDF2 in backward compatible way
1 parent b797d78 commit 54cd15a

6 files changed

+391
-373
lines changed

cli/index.js

+26-14
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,20 @@ try{
7070
* Salt and encrypt a msg with a password.
7171
* Inspired by https://github.com/adonespitogo
7272
*/
73-
var keySize = 256;
74-
var iterations = 1000;
73+
var pbkdf2Parameters = {
74+
keySize: 256/32,
75+
iterations: 1000,
76+
};
77+
78+
if (isTemplateSupporting15kIterations()) {
79+
pbkdf2Parameters.iterations = 15000;
80+
pbkdf2Parameters.hasher = CryptoJS.algo.SHA256;
81+
}
82+
7583
function encrypt (msg, password) {
7684
var salt = CryptoJS.lib.WordArray.random(128/8);
77-
78-
var key = CryptoJS.PBKDF2(password, salt, {
79-
keySize: keySize/32,
80-
iterations: iterations
81-
});
85+
86+
var key = CryptoJS.PBKDF2(password, salt, pbkdf2Parameters);
8287

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

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

9196
// salt, iv will be hex 32 in length
9297
// append them to the ciphertext for use in decryption
93-
var encryptedMsg = salt.toString()+ iv.toString() + encrypted.toString();
98+
var encryptedMsg = salt.toString() + iv.toString() + encrypted.toString();
9499
return encryptedMsg;
95100
}
96101

@@ -123,19 +128,26 @@ var data = {
123128

124129
genFile(data);
125130

131+
function isTemplateSupporting15kIterations() {
132+
return getTemplateContent().includes("// STATICRYPT VERSION: >= 1.4.3");
133+
}
134+
135+
function getTemplateContent() {
136+
try {
137+
return FileSystem.readFileSync(namedArgs.f, 'utf8');
138+
} catch (e) {
139+
console.log("Failure: could not read template!");
140+
process.exit(1);
141+
}
142+
}
126143

127144
/**
128145
* Fill the template with provided data and writes it to output file.
129146
*
130147
* @param data
131148
*/
132149
function genFile(data){
133-
try{
134-
var templateContents = FileSystem.readFileSync(namedArgs.f, 'utf8');
135-
}catch(e){
136-
console.log("Failure: could not read template!");
137-
process.exit(1);
138-
}
150+
var templateContents = getTemplateContent();
139151

140152
var renderedTemplate = render(templateContents, data);
141153

0 commit comments

Comments
 (0)