forked from kevinejohn/ripple-brainwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathripple-brainwallet.js
66 lines (54 loc) · 1.46 KB
/
ripple-brainwallet.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
var mn = require('./lib/mnemonic');
/**
* Encrypt a secret
*
* @param {String} secret
* @return {String} words
*/
var encryptNoVerify = function(secret) {
if (typeof secret !== 'string') {
throw new Error('Secret missing');
}
// TODO: Validate secret
// Pad secret (Add 3 arbitrary characters) to get 32 characters
return mn.encode(new Buffer(secret + 'FFF', 'base64').toString('hex'));
};
module.exports.encrypt = function(secret) {
var encrypted = encryptNoVerify(secret);
// Verify encrypted
var decrypted = decryptNoVerify(encrypted);
if (decrypted !== secret) {
throw new Error('Invalid secret');
}
return encrypted;
};
/**
* Decrypt brainwallet
*
* @param {String|Array} words
* @return {String} secret
*/
var decryptNoVerify = function(arg0) {
var words = [ ];
if (Array.isArray(arg0)) {
words = arg0;
} else if (arguments.length === 18) {
words = Array.prototype.slice.call(arguments);
} else {
words = arg0.split(/\s/g);
}
if (words.length !== 18) {
throw new Error('Less than 18 words supplied to decrypt');
}
// Slice last 3 padded characters
return new Buffer(mn.decode(words.join(' ')), 'hex').toString('base64').slice(0,-3);
};
module.exports.decrypt = function(arg0) {
var decrypted = decryptNoVerify(arg0);
// Verify encrypted
var encrypted = encryptNoVerify(decrypted);
if (encrypted !== arg0) {
throw new Error('Invalid brain wallet');
}
return decrypted;
};