1#!/usr/bin/env python2
2# Copyright (c) 2015 The Bitcoin Core developers
3# Distributed under the MIT software license, see the accompanying
4# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6import hashlib
7import sys
8import os
9from random import SystemRandom
10import base64
11import hmac
12
13if len(sys.argv) < 2:
14    sys.stderr.write('Please include username as an argument.\n')
15    sys.exit(0)
16
17username = sys.argv[1]
18
19#This uses os.urandom() underneath
20cryptogen = SystemRandom()
21
22#Create 16 byte hex salt
23salt_sequence = [cryptogen.randrange(256) for i in range(16)]
24hexseq = list(map(hex, salt_sequence))
25salt = "".join([x[2:] for x in hexseq])
26
27#Create 32 byte b64 password
28password = base64.urlsafe_b64encode(os.urandom(32))
29
30digestmod = hashlib.sha256
31
32if sys.version_info.major >= 3:
33    password = password.decode('utf-8')
34    digestmod = 'SHA256'
35
36m = hmac.new(bytearray(salt, 'utf-8'), bytearray(password, 'utf-8'), digestmod)
37result = m.hexdigest()
38
39print("String to be appended to bitcoin.conf:")
40print("rpcauth="+username+":"+salt+"$"+result)
41print("Your password:\n"+password)
42