1
- 'use strict' ;
1
+ const path = require ( 'path' ) ;
2
+ const bindings = require ( 'node-gyp-build' ) ( path . resolve ( __dirname ) ) ;
2
3
3
- var path = require ( 'path' ) ;
4
- var bindings = require ( 'node-gyp-build' ) ( path . resolve ( __dirname ) ) ;
4
+ const crypto = require ( 'crypto' ) ;
5
5
6
- var crypto = require ( 'crypto' ) ;
7
-
8
- var promises = require ( './promises' ) ;
6
+ const promises = require ( './promises' ) ;
9
7
10
8
/// generate a salt (sync)
11
9
/// @param {Number } [rounds] number of rounds (default 10)
12
10
/// @return {String } salt
13
- module . exports . genSaltSync = function genSaltSync ( rounds , minor ) {
11
+ function genSaltSync ( rounds , minor ) {
14
12
// default 10 rounds
15
13
if ( ! rounds ) {
16
14
rounds = 10 ;
17
15
} else if ( typeof rounds !== 'number' ) {
18
16
throw new Error ( 'rounds must be a number' ) ;
19
17
}
20
18
21
- if ( ! minor ) {
19
+ if ( ! minor ) {
22
20
minor = 'b' ;
23
- } else if ( minor !== 'b' && minor !== 'a' ) {
21
+ } else if ( minor !== 'b' && minor !== 'a' ) {
24
22
throw new Error ( 'minor must be either "a" or "b"' ) ;
25
23
}
26
24
27
25
return bindings . gen_salt_sync ( minor , rounds , crypto . randomBytes ( 16 ) ) ;
28
- } ;
26
+ }
29
27
30
28
/// generate a salt
31
29
/// @param {Number } [rounds] number of rounds (default 10)
32
30
/// @param {Function } cb callback(err, salt)
33
- module . exports . genSalt = function genSalt ( rounds , minor , cb ) {
34
- var error ;
31
+ function genSalt ( rounds , minor , cb ) {
32
+ let error ;
35
33
36
34
// if callback is first argument, then use defaults for others
37
35
if ( typeof arguments [ 0 ] === 'function' ) {
38
36
// have to set callback first otherwise arguments are overridden
39
37
cb = arguments [ 0 ] ;
40
38
rounds = 10 ;
41
39
minor = 'b' ;
42
- // callback is second argument
40
+ // callback is second argument
43
41
} else if ( typeof arguments [ 1 ] === 'function' ) {
44
42
// have to set callback first otherwise arguments are overridden
45
43
cb = arguments [ 1 ] ;
@@ -56,35 +54,35 @@ module.exports.genSalt = function genSalt(rounds, minor, cb) {
56
54
} else if ( typeof rounds !== 'number' ) {
57
55
// callback error asynchronously
58
56
error = new Error ( 'rounds must be a number' ) ;
59
- return process . nextTick ( function ( ) {
57
+ return process . nextTick ( function ( ) {
60
58
cb ( error ) ;
61
59
} ) ;
62
60
}
63
61
64
- if ( ! minor ) {
62
+ if ( ! minor ) {
65
63
minor = 'b'
66
- } else if ( minor !== 'b' && minor !== 'a' ) {
64
+ } else if ( minor !== 'b' && minor !== 'a' ) {
67
65
error = new Error ( 'minor must be either "a" or "b"' ) ;
68
- return process . nextTick ( function ( ) {
66
+ return process . nextTick ( function ( ) {
69
67
cb ( error ) ;
70
68
} ) ;
71
69
}
72
70
73
- crypto . randomBytes ( 16 , function ( error , randomBytes ) {
71
+ crypto . randomBytes ( 16 , function ( error , randomBytes ) {
74
72
if ( error ) {
75
73
cb ( error ) ;
76
74
return ;
77
75
}
78
76
79
77
bindings . gen_salt ( minor , rounds , randomBytes , cb ) ;
80
78
} ) ;
81
- } ;
79
+ }
82
80
83
81
/// hash data using a salt
84
82
/// @param {String|Buffer } data the data to encrypt
85
83
/// @param {String } salt the salt to use when hashing
86
84
/// @return {String } hash
87
- module . exports . hashSync = function hashSync ( data , salt ) {
85
+ function hashSync ( data , salt ) {
88
86
if ( data == null || salt == null ) {
89
87
throw new Error ( 'data and salt arguments required' ) ;
90
88
}
@@ -98,25 +96,25 @@ module.exports.hashSync = function hashSync(data, salt) {
98
96
}
99
97
100
98
return bindings . encrypt_sync ( data , salt ) ;
101
- } ;
99
+ }
102
100
103
101
/// hash data using a salt
104
102
/// @param {String|Buffer } data the data to encrypt
105
103
/// @param {String } salt the salt to use when hashing
106
104
/// @param {Function } cb callback(err, hash)
107
- module . exports . hash = function hash ( data , salt , cb ) {
108
- var error ;
105
+ function hash ( data , salt , cb ) {
106
+ let error ;
109
107
110
108
if ( typeof data === 'function' ) {
111
109
error = new Error ( 'data must be a string or Buffer and salt must either be a salt string or a number of rounds' ) ;
112
- return process . nextTick ( function ( ) {
110
+ return process . nextTick ( function ( ) {
113
111
data ( error ) ;
114
112
} ) ;
115
113
}
116
114
117
115
if ( typeof salt === 'function' ) {
118
116
error = new Error ( 'data must be a string or Buffer and salt must either be a salt string or a number of rounds' ) ;
119
- return process . nextTick ( function ( ) {
117
+ return process . nextTick ( function ( ) {
120
118
salt ( error ) ;
121
119
} ) ;
122
120
}
@@ -133,33 +131,33 @@ module.exports.hash = function hash(data, salt, cb) {
133
131
134
132
if ( data == null || salt == null ) {
135
133
error = new Error ( 'data and salt arguments required' ) ;
136
- return process . nextTick ( function ( ) {
134
+ return process . nextTick ( function ( ) {
137
135
cb ( error ) ;
138
136
} ) ;
139
137
}
140
138
141
139
if ( ! ( typeof data === 'string' || data instanceof Buffer ) || ( typeof salt !== 'string' && typeof salt !== 'number' ) ) {
142
140
error = new Error ( 'data must be a string or Buffer and salt must either be a salt string or a number of rounds' ) ;
143
- return process . nextTick ( function ( ) {
141
+ return process . nextTick ( function ( ) {
144
142
cb ( error ) ;
145
143
} ) ;
146
144
}
147
145
148
146
149
147
if ( typeof salt === 'number' ) {
150
- return module . exports . genSalt ( salt , function ( err , salt ) {
148
+ return module . exports . genSalt ( salt , function ( err , salt ) {
151
149
return bindings . encrypt ( data , salt , cb ) ;
152
150
} ) ;
153
151
}
154
152
155
153
return bindings . encrypt ( data , salt , cb ) ;
156
- } ;
154
+ }
157
155
158
156
/// compare raw data to hash
159
157
/// @param {String|Buffer } data the data to hash and compare
160
158
/// @param {String } hash expected hash
161
159
/// @return {bool } true if hashed data matches hash
162
- module . exports . compareSync = function compareSync ( data , hash ) {
160
+ function compareSync ( data , hash ) {
163
161
if ( data == null || hash == null ) {
164
162
throw new Error ( 'data and hash arguments required' ) ;
165
163
}
@@ -169,25 +167,25 @@ module.exports.compareSync = function compareSync(data, hash) {
169
167
}
170
168
171
169
return bindings . compare_sync ( data , hash ) ;
172
- } ;
170
+ }
173
171
174
172
/// compare raw data to hash
175
173
/// @param {String|Buffer } data the data to hash and compare
176
174
/// @param {String } hash expected hash
177
175
/// @param {Function } cb callback(err, matched) - matched is true if hashed data matches hash
178
- module . exports . compare = function compare ( data , hash , cb ) {
179
- var error ;
176
+ function compare ( data , hash , cb ) {
177
+ let error ;
180
178
181
179
if ( typeof data === 'function' ) {
182
180
error = new Error ( 'data and hash arguments required' ) ;
183
- return process . nextTick ( function ( ) {
181
+ return process . nextTick ( function ( ) {
184
182
data ( error ) ;
185
183
} ) ;
186
184
}
187
185
188
186
if ( typeof hash === 'function' ) {
189
187
error = new Error ( 'data and hash arguments required' ) ;
190
- return process . nextTick ( function ( ) {
188
+ return process . nextTick ( function ( ) {
191
189
hash ( error ) ;
192
190
} ) ;
193
191
}
@@ -204,24 +202,24 @@ module.exports.compare = function compare(data, hash, cb) {
204
202
205
203
if ( data == null || hash == null ) {
206
204
error = new Error ( 'data and hash arguments required' ) ;
207
- return process . nextTick ( function ( ) {
205
+ return process . nextTick ( function ( ) {
208
206
cb ( error ) ;
209
207
} ) ;
210
208
}
211
209
212
210
if ( ! ( typeof data === 'string' || data instanceof Buffer ) || typeof hash !== 'string' ) {
213
211
error = new Error ( 'data and hash must be strings' ) ;
214
- return process . nextTick ( function ( ) {
212
+ return process . nextTick ( function ( ) {
215
213
cb ( error ) ;
216
214
} ) ;
217
215
}
218
216
219
217
return bindings . compare ( data , hash , cb ) ;
220
- } ;
218
+ }
221
219
222
220
/// @param {String } hash extract rounds from this hash
223
221
/// @return {Number } the number of rounds used to encrypt a given hash
224
- module . exports . getRounds = function getRounds ( hash ) {
222
+ function getRounds ( hash ) {
225
223
if ( hash == null ) {
226
224
throw new Error ( 'hash argument required' ) ;
227
225
}
@@ -231,4 +229,14 @@ module.exports.getRounds = function getRounds(hash) {
231
229
}
232
230
233
231
return bindings . get_rounds ( hash ) ;
234
- } ;
232
+ }
233
+
234
+ module . exports = {
235
+ genSaltSync,
236
+ genSalt,
237
+ hashSync,
238
+ hash,
239
+ compareSync,
240
+ compare,
241
+ getRounds,
242
+ }
0 commit comments