Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env python32# Copyright (c) 2015-present The Bitcoin Core developers3# Distributed under the MIT software license, see the accompanying4# file COPYING or http://www.opensource.org/licenses/mit-license.php.6from argparse import ArgumentParser7from getpass import getpass8from secrets import token_hex, token_urlsafe9import hmac10import json12def generate_salt(size):13"""Create size byte hex salt"""14return token_hex(size)16def generate_password():17"""Create 32 byte b64 password"""18return token_urlsafe(32)20def password_to_hmac(salt, password):21m = hmac.new(salt.encode('utf-8'), password.encode('utf-8'), 'SHA256')22return m.hexdigest()24def main():25parser = ArgumentParser(description='Create login credentials for a JSON-RPC user')26parser.add_argument('username', help='the username for authentication')27parser.add_argument('password', help='leave empty to generate a random password or specify "-" to prompt for password', nargs='?')28parser.add_argument("-j", "--json", help="output to json instead of plain-text", action='store_true')29args = parser.parse_args()31if not args.password:32args.password = generate_password()33elif args.password == '-':34args.password = getpass()36# Create 16 byte hex salt37salt = generate_salt(16)38password_hmac = password_to_hmac(salt, args.password)40if args.json:41odict={'username':args.username, 'password':args.password, 'rpcauth':f'{args.username}:{salt}${password_hmac}'}42print(json.dumps(odict))43else:44print('String to be appended to bitcoin.conf:')45print(f'rpcauth={args.username}:{salt}${password_hmac}')46print(f'Your password:\n{args.password}')48if __name__ == '__main__':49main()
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.