@@ -148,42 +148,42 @@ export function genSalt(rounds, seed_length, callback) {
148
148
}
149
149
150
150
/**
151
- * Synchronously generates a hash for the given string .
152
- * @param {string } s String to hash
151
+ * Synchronously generates a hash for the given password .
152
+ * @param {string } password Password to hash
153
153
* @param {(number|string)= } salt Salt length to generate or salt to use, default to 10
154
154
* @returns {string } Resulting hash
155
155
*/
156
- export function hashSync ( s , salt ) {
156
+ export function hashSync ( password , salt ) {
157
157
if ( typeof salt === "undefined" ) salt = GENSALT_DEFAULT_LOG2_ROUNDS ;
158
158
if ( typeof salt === "number" ) salt = genSaltSync ( salt ) ;
159
- if ( typeof s !== "string" || typeof salt !== "string" )
160
- throw Error ( "Illegal arguments: " + typeof s + ", " + typeof salt ) ;
161
- return _hash ( s , salt ) ;
159
+ if ( typeof password !== "string" || typeof salt !== "string" )
160
+ throw Error ( "Illegal arguments: " + typeof password + ", " + typeof salt ) ;
161
+ return _hash ( password , salt ) ;
162
162
}
163
163
164
164
/**
165
- * Asynchronously generates a hash for the given string .
166
- * @param {string } s String to hash
165
+ * Asynchronously generates a hash for the given password .
166
+ * @param {string } password Password to hash
167
167
* @param {number|string } salt Salt length to generate or salt to use
168
168
* @param {function(Error, string=)= } callback Callback receiving the error, if any, and the resulting hash
169
169
* @param {function(number)= } progressCallback Callback successively called with the percentage of rounds completed
170
170
* (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms.
171
171
* @returns {!Promise } If `callback` has been omitted
172
172
* @throws {Error } If `callback` is present but not a function
173
173
*/
174
- export function hash ( s , salt , callback , progressCallback ) {
174
+ export function hash ( password , salt , callback , progressCallback ) {
175
175
function _async ( callback ) {
176
- if ( typeof s === "string" && typeof salt === "number" )
176
+ if ( typeof password === "string" && typeof salt === "number" )
177
177
genSalt ( salt , function ( err , salt ) {
178
- _hash ( s , salt , callback , progressCallback ) ;
178
+ _hash ( password , salt , callback , progressCallback ) ;
179
179
} ) ;
180
- else if ( typeof s === "string" && typeof salt === "string" )
181
- _hash ( s , salt , callback , progressCallback ) ;
180
+ else if ( typeof password === "string" && typeof salt === "string" )
181
+ _hash ( password , salt , callback , progressCallback ) ;
182
182
else
183
183
nextTick (
184
184
callback . bind (
185
185
this ,
186
- Error ( "Illegal arguments: " + typeof s + ", " + typeof salt ) ,
186
+ Error ( "Illegal arguments: " + typeof password + ", " + typeof salt ) ,
187
187
) ,
188
188
) ;
189
189
}
@@ -220,39 +220,41 @@ function safeStringCompare(known, unknown) {
220
220
}
221
221
222
222
/**
223
- * Synchronously tests a string against a hash.
224
- * @param {string } s String to compare
223
+ * Synchronously tests a password against a hash.
224
+ * @param {string } password Password to compare
225
225
* @param {string } hash Hash to test against
226
226
* @returns {boolean } true if matching, otherwise false
227
227
* @throws {Error } If an argument is illegal
228
228
*/
229
- export function compareSync ( s , hash ) {
230
- if ( typeof s !== "string" || typeof hash !== "string" )
231
- throw Error ( "Illegal arguments: " + typeof s + ", " + typeof hash ) ;
229
+ export function compareSync ( password , hash ) {
230
+ if ( typeof password !== "string" || typeof hash !== "string" )
231
+ throw Error ( "Illegal arguments: " + typeof password + ", " + typeof hash ) ;
232
232
if ( hash . length !== 60 ) return false ;
233
233
return safeStringCompare (
234
- hashSync ( s , hash . substring ( 0 , hash . length - 31 ) ) ,
234
+ hashSync ( password , hash . substring ( 0 , hash . length - 31 ) ) ,
235
235
hash ,
236
236
) ;
237
237
}
238
238
239
239
/**
240
- * Asynchronously compares the given data against the given hash.
241
- * @param {string } s Data to compare
242
- * @param {string } hashValue Data to be compared to
240
+ * Asynchronously tests a password against a hash.
241
+ * @param {string } password Password to compare
242
+ * @param {string } hashValue Hash to test against
243
243
* @param {function(Error, boolean)= } callback Callback receiving the error, if any, otherwise the result
244
244
* @param {function(number)= } progressCallback Callback successively called with the percentage of rounds completed
245
245
* (0.0 - 1.0), maximally once per `MAX_EXECUTION_TIME = 100` ms.
246
246
* @returns {!Promise } If `callback` has been omitted
247
247
* @throws {Error } If `callback` is present but not a function
248
248
*/
249
- export function compare ( s , hashValue , callback , progressCallback ) {
249
+ export function compare ( password , hashValue , callback , progressCallback ) {
250
250
function _async ( callback ) {
251
- if ( typeof s !== "string" || typeof hashValue !== "string" ) {
251
+ if ( typeof password !== "string" || typeof hashValue !== "string" ) {
252
252
nextTick (
253
253
callback . bind (
254
254
this ,
255
- Error ( "Illegal arguments: " + typeof s + ", " + typeof hashValue ) ,
255
+ Error (
256
+ "Illegal arguments: " + typeof password + ", " + typeof hashValue ,
257
+ ) ,
256
258
) ,
257
259
) ;
258
260
return ;
@@ -262,7 +264,7 @@ export function compare(s, hashValue, callback, progressCallback) {
262
264
return ;
263
265
}
264
266
hash (
265
- s ,
267
+ password ,
266
268
hashValue . substring ( 0 , 29 ) ,
267
269
function ( err , comp ) {
268
270
if ( err ) callback ( err ) ;
@@ -314,6 +316,18 @@ export function getSalt(hash) {
314
316
return hash . substring ( 0 , 29 ) ;
315
317
}
316
318
319
+ /**
320
+ * Tests if a password will be truncated when hashed, that is its length is
321
+ * greater than 72 bytes when converted to UTF-8.
322
+ * @param {string } password The password to test
323
+ * @returns {boolean } `true` if truncated, otherwise `false`
324
+ */
325
+ export function truncates ( password ) {
326
+ if ( typeof password !== "string" )
327
+ throw Error ( "Illegal arguments: " + typeof password ) ;
328
+ return utf8Length ( password ) > 72 ;
329
+ }
330
+
317
331
/**
318
332
* Continues with the callback on the next tick.
319
333
* @function
@@ -960,7 +974,7 @@ function _crypt(b, salt, rounds, callback, progressCallback) {
960
974
j ;
961
975
962
976
//Use typed arrays when available - huge speedup!
963
- if ( Int32Array ) {
977
+ if ( typeof Int32Array === "function" ) {
964
978
P = new Int32Array ( P_ORIG ) ;
965
979
S = new Int32Array ( S_ORIG ) ;
966
980
} else {
@@ -1014,18 +1028,18 @@ function _crypt(b, salt, rounds, callback, progressCallback) {
1014
1028
}
1015
1029
1016
1030
/**
1017
- * Internally hashes a string .
1018
- * @param {string } s String to hash
1031
+ * Internally hashes a password .
1032
+ * @param {string } password Password to hash
1019
1033
* @param {?string } salt Salt to use, actually never null
1020
1034
* @param {function(Error, string=)= } callback Callback receiving the error, if any, and the resulting hash. If omitted,
1021
1035
* hashing is performed synchronously.
1022
1036
* @param {function(number)= } progressCallback Callback called with the current progress
1023
1037
* @returns {string|undefined } Resulting hash if callback has been omitted, otherwise `undefined`
1024
1038
* @inner
1025
1039
*/
1026
- function _hash ( s , salt , callback , progressCallback ) {
1040
+ function _hash ( password , salt , callback , progressCallback ) {
1027
1041
var err ;
1028
- if ( typeof s !== "string" || typeof salt !== "string" ) {
1042
+ if ( typeof password !== "string" || typeof salt !== "string" ) {
1029
1043
err = Error ( "Invalid string / salt: Not a string" ) ;
1030
1044
if ( callback ) {
1031
1045
nextTick ( callback . bind ( this , err ) ) ;
@@ -1070,9 +1084,9 @@ function _hash(s, salt, callback, progressCallback) {
1070
1084
r2 = parseInt ( salt . substring ( offset + 1 , offset + 2 ) , 10 ) ,
1071
1085
rounds = r1 + r2 ,
1072
1086
real_salt = salt . substring ( offset + 3 , offset + 25 ) ;
1073
- s += minor >= "a" ? "\x00" : "" ;
1087
+ password += minor >= "a" ? "\x00" : "" ;
1074
1088
1075
- var passwordb = utf8Array ( s ) ,
1089
+ var passwordb = utf8Array ( password ) ,
1076
1090
saltb = base64_decode ( real_salt , BCRYPT_SALT_LEN ) ;
1077
1091
1078
1092
/**
@@ -1115,23 +1129,23 @@ function _hash(s, salt, callback, progressCallback) {
1115
1129
/**
1116
1130
* Encodes a byte array to base64 with up to len bytes of input, using the custom bcrypt alphabet.
1117
1131
* @function
1118
- * @param {!Array.<number> } b Byte array
1119
- * @param {number } len Maximum input length
1132
+ * @param {!Array.<number> } bytes Byte array
1133
+ * @param {number } length Maximum input length
1120
1134
* @returns {string }
1121
1135
*/
1122
- export function encodeBase64 ( b , len ) {
1123
- return base64_encode ( b , len ) ;
1136
+ export function encodeBase64 ( bytes , length ) {
1137
+ return base64_encode ( bytes , length ) ;
1124
1138
}
1125
1139
1126
1140
/**
1127
1141
* Decodes a base64 encoded string to up to len bytes of output, using the custom bcrypt alphabet.
1128
1142
* @function
1129
- * @param {string } s String to decode
1130
- * @param {number } len Maximum output length
1143
+ * @param {string } string String to decode
1144
+ * @param {number } length Maximum output length
1131
1145
* @returns {!Array.<number> }
1132
1146
*/
1133
- export function decodeBase64 ( s , len ) {
1134
- return base64_decode ( s , len ) ;
1147
+ export function decodeBase64 ( string , length ) {
1148
+ return base64_decode ( string , length ) ;
1135
1149
}
1136
1150
1137
1151
export default {
@@ -1144,6 +1158,7 @@ export default {
1144
1158
compare,
1145
1159
getRounds,
1146
1160
getSalt,
1161
+ truncates,
1147
1162
encodeBase64,
1148
1163
decodeBase64,
1149
1164
} ;
0 commit comments