-
Notifications
You must be signed in to change notification settings - Fork 1
/
xpasswd.js
159 lines (137 loc) · 3.3 KB
/
xpasswd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
"use strict";
/**
* Module dependencies.
*/
var crypto = require("crypto");
/**
* Password version definitions.
*/
exports.definitions = {
1: {
digest: "sha256",
iterations: 4096,
keylen: 1024,
version: 1
}
};
/**
* Copy properties from sources to target.
*
* @param {Object} target The target object.
* @param {...Object} sources The source object.
* @return {Object} The target object.
* @private
*/
function extend (target /* ...sources */) {
var source, key, i = 1;
while (source = arguments[i++]) {
for (key in source) target[key] = source[key];
}
return target;
};
/**
* Pack a password into a string.
*
* @param {String} key The hashed password to pack.
* @param {Object} options
* @param {String} options.salt
* @param {Integer} options.version
* @return {String}
* @private
*/
function pack (key, options) {
return [
"",
options.version,
options.salt,
key
].join("$");
}
/**
* Unpack a password from a string.
*
* @param {String} digest The packed password to unpack.
* @return {Object}
* @private
*/
function unpack (digest) {
var parts = digest.split("$");
return {
version: ~~parts[1],
salt: parts[2],
key: parts[3]
};
}
/**
* Digest a password using PBKDF2.
*
* @param {String} secret
* @param {Object} options
* @param {String} options.salt
* @param {Integer} [options.iterations]
* @param {Integer} [options.keylen]
* @param {String} [options.digest]
* @return {Promise}
* @private
*/
function _digest (secret, options) {
return new Promise(function (resolve, reject) {
crypto.pbkdf2(
secret,
options.salt,
options.iterations,
options.keylen,
options.digest,
function (err, key) {
if (err) return reject(err);
key = key.toString("base64").replace(/=+$/, "");
resolve(key);
});
});
}
/**
* Digest a password.
*
* @param {String} secret
* @param {Object} options
* @param {String} options.salt
* @param {String} [options.digest]
* @param {Integer} [options.iterations]
* @param {Integer} [options.keylen]
* @param {Integer} [options.version]
* @return {Promise} A promise resolving to a packed password digest.
* @api public
*/
exports.digest = function digest (secret, options) {
// set defaults
var version = options && options.version || 1;
options = extend({}, exports.definitions[version], options);
// generate salt if not given
if (!options.salt) options.salt = crypto.randomBytes(9).toString("base64");
// calculate PBKDF2 key
return _digest(secret, options).then(function (key) {
return pack(key, options);
});
}
/**
* Validate a password.
*
* @param {String} secret The password to validate.
* @param {String} digest The packed password against which to validate.
* @return {Promise} A promise resolving to true if the password is valid.
* @api public
*/
exports.validate = function validate (secret, digest) {
// unpack digest
var options = unpack(digest);
// check version
if (exports.definitions[options.version]) {
extend(options, exports.definitions[options.version]);
} else {
return Promise.reject(new Error(`Invalid key version ${version}`));
}
// validate PBKDF2 key
return _digest(secret, options).then(function (key) {
return key == options.key;
});
}