70
70
* Salt and encrypt a msg with a password.
71
71
* Inspired by https://github.com/adonespitogo
72
72
*/
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
+
75
83
function encrypt ( msg , password ) {
76
84
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 ) ;
82
87
83
88
var iv = CryptoJS . lib . WordArray . random ( 128 / 8 ) ;
84
89
@@ -90,7 +95,7 @@ function encrypt (msg, password) {
90
95
91
96
// salt, iv will be hex 32 in length
92
97
// 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 ( ) ;
94
99
return encryptedMsg ;
95
100
}
96
101
@@ -123,19 +128,26 @@ var data = {
123
128
124
129
genFile ( data ) ;
125
130
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
+ }
126
143
127
144
/**
128
145
* Fill the template with provided data and writes it to output file.
129
146
*
130
147
* @param data
131
148
*/
132
149
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 ( ) ;
139
151
140
152
var renderedTemplate = render ( templateContents , data ) ;
141
153
0 commit comments