1

For example, I want the hash value that I get by using python function blake2b to have only (acdefghjklmnpqrstuvwxyz2345679)

Varun Kumar
  • 53
  • 1
  • 12

1 Answers1

0

A hash is a bit string. You can encode this bit string using a specific set of printable characters if you want. Hexadecimal (using 0123456789abcdef) is the most common way, but if you want a different set of characters, you can choose those instead.

To encode the hash value in hexadecimal, assuming that you have it as a raw string like the value returned by the digest method in the standard hashlib module, use hash.hex() in Python 3 and hash.encode('hex') in Python 2. The hashlib module has a method hexdigest which returns this encoding directly.

If you want to encode the value using single-case letters and digits without a risk of confusion on 0/O and 1/I, there's a standard for that called Base32. Base32 is available in Python in the base64 module. The standard encoding uses only uppercase, but you can translate to lowercase if you want. Base32 pads with =, but you can remove them for storage.

import base64, hashlib
hash = hashlib.new('SHA256', b'input').digest()
b32_hash = base64.b32encode(hash).lower().rstrip(b'=')

If you really want that specific 30-character set, you can convert the hexadecimal representation to an integer using int(….hexdigest(), 16) then convert that integer to a string using the digits of your choice.

Community
  • 1
  • 1
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254