Skip to content

Instantly share code, notes, and snippets.

@anapsix
Created May 22, 2018 19:33
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save anapsix/4c3e8a8685ce5a3f0d7599c9902fd0d5 to your computer and use it in GitHub Desktop.
Save anapsix/4c3e8a8685ce5a3f0d7599c9902fd0d5 to your computer and use it in GitHub Desktop.
generate RabbitMQ compatible SHA256 password hash
#!/usr/bin/env python
# details on rabbitMQ password hashing
# https://www.rabbitmq.com/passwords.html#computing-password-hash
from __future__ import print_function
import base64
import os
import hashlib
import struct
import getpass
# This is the password we wish to encode
password1 = getpass.getpass("password: ")
password2 = getpass.getpass("again: ")
if password1 != password2:
print("passwords do not match")
exit(1)
# 1.Generate a random 32 bit salt:
# This will generate 32 bits of random data:
salt = os.urandom(4)
# 2.Concatenate that with the UTF-8 representation of the password
tmp0 = salt + password1.encode('utf-8')
# 3. Take the SHA256 hash and get the bytes back
tmp1 = hashlib.sha256(tmp0).digest()
# 4. Concatenate the salt again:
salted_hash = salt + tmp1
# 5. convert to base64 encoding:
pass_hash = base64.b64encode(salted_hash)
print(pass_hash)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment