Skip to content

Instantly share code, notes, and snippets.

@jO-Osko
Created June 30, 2022 13:27
Show Gist options
  • Save jO-Osko/ddd7f3b1cb1ed2031a43c446f9c7af34 to your computer and use it in GitHub Desktop.
Save jO-Osko/ddd7f3b1cb1ed2031a43c446f9c7af34 to your computer and use it in GitHub Desktop.
Salt transcoding
import string
import base64
# bcrypt uses a strange base64 encoding, which is incompatible with the common one, go figure...
B64_CHARS = ''.join((string.ascii_uppercase, string.ascii_lowercase, string.digits, '+/')).encode("utf-8")
B64_CHARS_BCRYPT = ''.join(('./', string.ascii_uppercase, string.ascii_lowercase, string.digits)).encode("UTF-8")
B64_TO_BCRYPT = bytes.maketrans(B64_CHARS, B64_CHARS_BCRYPT)
B64_FROM_BCRYPT = bytes.maketrans(B64_CHARS_BCRYPT, B64_CHARS)
def decode_b64(data):
return base64.b64encode(data).translate(B64_TO_BCRYPT, b'=')
def translate_salt(rounds: int, salt: bytes) -> bytes:
return b"$" + b"2b" + b"$" + ("%2.2u" % rounds).encode("ascii") + b"$" + decode_b64(salt)
if __name__ == '__main__':
print(translate_salt(10, b"abcdefghabcdefgh").decode("utf-8"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment