Skip to content

Commit 0bc980b

Browse files
committed
Merge #13146: rpcauth: Make it possible to provide a custom password
2a89b0c rpcauth: Make it possible to provide a custom password (Wladimir J. van der Laan) Pull request description: This adds the functionality to specify a custom password to `rpcauth.py`, as well as makes the code (IMO) easier to understand. Tree-SHA512: 458d54cc258e16917c0f0ce5ae1c3d6c0c03b5ab931011bf3feb09a3474f1511c38ec45822a4af2aadeaca522a002ba04a564849dd3f42fa6f36dd21b0cba093
2 parents 57c57df + 2a89b0c commit 0bc980b

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

share/rpcauth/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ Create login credentials for a JSON-RPC user.
88
Usage:
99

1010
./rpcauth.py <username>
11+
12+
in which case the script will generate a password. To specify a custom password do:
13+
14+
./rpcauth.py <username> <password>

share/rpcauth/rpcauth.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,27 @@ def generate_salt():
1717
salt_sequence = [cryptogen.randrange(256) for _ in range(16)]
1818
return ''.join([format(r, 'x') for r in salt_sequence])
1919

20-
def generate_password(salt):
20+
def generate_password():
2121
"""Create 32 byte b64 password"""
22-
password = base64.urlsafe_b64encode(os.urandom(32)).decode('utf-8')
22+
return base64.urlsafe_b64encode(os.urandom(32)).decode('utf-8')
2323

24+
def password_to_hmac(salt, password):
2425
m = hmac.new(bytearray(salt, 'utf-8'), bytearray(password, 'utf-8'), 'SHA256')
25-
password_hmac = m.hexdigest()
26-
27-
return password, password_hmac
26+
return m.hexdigest()
2827

2928
def main():
3029
if len(sys.argv) < 2:
31-
sys.stderr.write('Please include username as an argument.\n')
30+
sys.stderr.write('Please include username (and an optional password, will generate one if not provided) as an argument.\n')
3231
sys.exit(0)
3332

3433
username = sys.argv[1]
3534

3635
salt = generate_salt()
37-
password, password_hmac = generate_password(salt)
36+
if len(sys.argv) > 2:
37+
password = sys.argv[2]
38+
else:
39+
password = generate_password()
40+
password_hmac = password_to_hmac(salt, password)
3841

3942
print('String to be appended to bitcoin.conf:')
4043
print('rpcauth={0}:{1}${2}'.format(username, salt, password_hmac))

test/util/rpcauth-test.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,15 @@ def test_generate_salt(self):
2828
self.assertGreaterEqual(len(self.rpcauth.generate_salt()), 16)
2929

3030
def test_generate_password(self):
31-
salt = self.rpcauth.generate_salt()
32-
password, password_hmac = self.rpcauth.generate_password(salt)
33-
31+
password = self.rpcauth.generate_password()
3432
expected_password = base64.urlsafe_b64encode(
3533
base64.urlsafe_b64decode(password)).decode('utf-8')
3634
self.assertEqual(expected_password, password)
3735

3836
def test_check_password_hmac(self):
3937
salt = self.rpcauth.generate_salt()
40-
password, password_hmac = self.rpcauth.generate_password(salt)
38+
password = self.rpcauth.generate_password()
39+
password_hmac = self.rpcauth.password_to_hmac(salt, password)
4140

4241
m = hmac.new(bytearray(salt, 'utf-8'),
4342
bytearray(password, 'utf-8'), 'SHA256')

0 commit comments

Comments
 (0)