You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rpcuser.py 1.1KB

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