1 | 'use strict';
|
2 |
|
3 | var nodePreGyp = require('@mapbox/node-pre-gyp');
|
4 | var path = require('path');
|
5 | var binding_path = nodePreGyp.find(path.resolve(path.join(__dirname, './package.json')));
|
6 | var bindings = require(binding_path);
|
7 |
|
8 | var crypto = require('crypto');
|
9 |
|
10 | var promises = require('./promises');
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | module.exports.genSaltSync = function genSaltSync(rounds, minor) {
|
16 |
|
17 | if (!rounds) {
|
18 | rounds = 10;
|
19 | } else if (typeof rounds !== 'number') {
|
20 | throw new Error('rounds must be a number');
|
21 | }
|
22 |
|
23 | if(!minor) {
|
24 | minor = 'b';
|
25 | } else if(minor !== 'b' && minor !== 'a') {
|
26 | throw new Error('minor must be either "a" or "b"');
|
27 | }
|
28 |
|
29 | return bindings.gen_salt_sync(minor, rounds, crypto.randomBytes(16));
|
30 | };
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | module.exports.genSalt = function genSalt(rounds, minor, cb) {
|
36 | var error;
|
37 |
|
38 |
|
39 | if (typeof arguments[0] === 'function') {
|
40 |
|
41 | cb = arguments[0];
|
42 | rounds = 10;
|
43 | minor = 'b';
|
44 |
|
45 | } else if (typeof arguments[1] === 'function') {
|
46 |
|
47 | cb = arguments[1];
|
48 | minor = 'b';
|
49 | }
|
50 |
|
51 | if (!cb) {
|
52 | return promises.promise(genSalt, this, [rounds, minor]);
|
53 | }
|
54 |
|
55 |
|
56 | if (!rounds) {
|
57 | rounds = 10;
|
58 | } else if (typeof rounds !== 'number') {
|
59 |
|
60 | error = new Error('rounds must be a number');
|
61 | return process.nextTick(function() {
|
62 | cb(error);
|
63 | });
|
64 | }
|
65 |
|
66 | if(!minor) {
|
67 | minor = 'b'
|
68 | } else if(minor !== 'b' && minor !== 'a') {
|
69 | error = new Error('minor must be either "a" or "b"');
|
70 | return process.nextTick(function() {
|
71 | cb(error);
|
72 | });
|
73 | }
|
74 |
|
75 | crypto.randomBytes(16, function(error, randomBytes) {
|
76 | if (error) {
|
77 | cb(error);
|
78 | return;
|
79 | }
|
80 |
|
81 | bindings.gen_salt(minor, rounds, randomBytes, cb);
|
82 | });
|
83 | };
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | module.exports.hashSync = function hashSync(data, salt) {
|
90 | if (data == null || salt == null) {
|
91 | throw new Error('data and salt arguments required');
|
92 | }
|
93 |
|
94 | if (!(typeof data === 'string' || data instanceof Buffer) || (typeof salt !== 'string' && typeof salt !== 'number')) {
|
95 | throw new Error('data must be a string or Buffer and salt must either be a salt string or a number of rounds');
|
96 | }
|
97 |
|
98 | if (typeof salt === 'number') {
|
99 | salt = module.exports.genSaltSync(salt);
|
100 | }
|
101 |
|
102 | return bindings.encrypt_sync(data, salt);
|
103 | };
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 | module.exports.hash = function hash(data, salt, cb) {
|
110 | var error;
|
111 |
|
112 | if (typeof data === 'function') {
|
113 | error = new Error('data must be a string or Buffer and salt must either be a salt string or a number of rounds');
|
114 | return process.nextTick(function() {
|
115 | data(error);
|
116 | });
|
117 | }
|
118 |
|
119 | if (typeof salt === 'function') {
|
120 | error = new Error('data must be a string or Buffer and salt must either be a salt string or a number of rounds');
|
121 | return process.nextTick(function() {
|
122 | salt(error);
|
123 | });
|
124 | }
|
125 |
|
126 |
|
127 |
|
128 | if (cb && typeof cb !== 'function') {
|
129 | return promises.reject(new Error('cb must be a function or null to return a Promise'));
|
130 | }
|
131 |
|
132 | if (!cb) {
|
133 | return promises.promise(hash, this, [data, salt]);
|
134 | }
|
135 |
|
136 | if (data == null || salt == null) {
|
137 | error = new Error('data and salt arguments required');
|
138 | return process.nextTick(function() {
|
139 | cb(error);
|
140 | });
|
141 | }
|
142 |
|
143 | if (!(typeof data === 'string' || data instanceof Buffer) || (typeof salt !== 'string' && typeof salt !== 'number')) {
|
144 | error = new Error('data must be a string or Buffer and salt must either be a salt string or a number of rounds');
|
145 | return process.nextTick(function() {
|
146 | cb(error);
|
147 | });
|
148 | }
|
149 |
|
150 |
|
151 | if (typeof salt === 'number') {
|
152 | return module.exports.genSalt(salt, function(err, salt) {
|
153 | return bindings.encrypt(data, salt, cb);
|
154 | });
|
155 | }
|
156 |
|
157 | return bindings.encrypt(data, salt, cb);
|
158 | };
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | module.exports.compareSync = function compareSync(data, hash) {
|
165 | if (data == null || hash == null) {
|
166 | throw new Error('data and hash arguments required');
|
167 | }
|
168 |
|
169 | if (!(typeof data === 'string' || data instanceof Buffer) || typeof hash !== 'string') {
|
170 | throw new Error('data must be a string or Buffer and hash must be a string');
|
171 | }
|
172 |
|
173 | return bindings.compare_sync(data, hash);
|
174 | };
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | module.exports.compare = function compare(data, hash, cb) {
|
181 | var error;
|
182 |
|
183 | if (typeof data === 'function') {
|
184 | error = new Error('data and hash arguments required');
|
185 | return process.nextTick(function() {
|
186 | data(error);
|
187 | });
|
188 | }
|
189 |
|
190 | if (typeof hash === 'function') {
|
191 | error = new Error('data and hash arguments required');
|
192 | return process.nextTick(function() {
|
193 | hash(error);
|
194 | });
|
195 | }
|
196 |
|
197 |
|
198 |
|
199 | if (cb && typeof cb !== 'function') {
|
200 | return promises.reject(new Error('cb must be a function or null to return a Promise'));
|
201 | }
|
202 |
|
203 | if (!cb) {
|
204 | return promises.promise(compare, this, [data, hash]);
|
205 | }
|
206 |
|
207 | if (data == null || hash == null) {
|
208 | error = new Error('data and hash arguments required');
|
209 | return process.nextTick(function() {
|
210 | cb(error);
|
211 | });
|
212 | }
|
213 |
|
214 | if (!(typeof data === 'string' || data instanceof Buffer) || typeof hash !== 'string') {
|
215 | error = new Error('data and hash must be strings');
|
216 | return process.nextTick(function() {
|
217 | cb(error);
|
218 | });
|
219 | }
|
220 |
|
221 | return bindings.compare(data, hash, cb);
|
222 | };
|
223 |
|
224 |
|
225 |
|
226 | module.exports.getRounds = function getRounds(hash) {
|
227 | if (hash == null) {
|
228 | throw new Error('hash argument required');
|
229 | }
|
230 |
|
231 | if (typeof hash !== 'string') {
|
232 | throw new Error('hash must be a string');
|
233 | }
|
234 |
|
235 | return bindings.get_rounds(hash);
|
236 | };
|