forked from jlopp/bitcoin-core-rpc-auth-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpcauth.js
40 lines (32 loc) · 1.07 KB
/
rpcauth.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
// Source: https://github.com/an-ivannikov-dev/bitcoin-rpcauth-js
const crypto = require('crypto');
const base64url = require('base64url');
// Create size byte hex salt
global.genSalt = function(size = 16) {
const buffer = crypto.randomBytes(size);
return buffer.toString('hex');
}
// Create 32 byte b64 password
global.genPass = function(size = 32) {
const buffer = crypto.randomBytes(size);
return base64url.fromBase64(buffer.toString('base64'));
}
global.genUser = function() {
return 'user_' + Math.round(Math.random() * 1000);
}
global.genHash = function(password, salt) {
const hash = crypto
.createHmac('sha256', salt)
.update(password)
.digest('hex');
return hash;
}
global.genRpcAuth = function(username = genUser(), password = genPass(), salt = genSalt()) {
const hash = genHash(password, salt);
return { username, password, salt, hash };
}
global.genRpcAuthStr = function(username, password, salt) {
const rpcauth = genRpcAuth(username, password, salt);
const str = `rpcauth=${rpcauth.username}:${rpcauth.salt}$${rpcauth.hash}`;
return str;
}