0

MD5 and SHA functions output a string of Hex representing the data the hash generated. Is there a hash that exists, or a way to convert these hashes to create an output that would only result in a string using only printable Ascii characters.

Instead of getting a hash that only has the possible values of [0-9] and [A-F]

I would like to get [0-9] and [A-Z] and [a-z] [!@#$%^&*-=_+[]{}|;':",./<>?`~]

I do understand that the Hex value that MD5 and Sha spit out is just a representation of the binary value they actually represent, so perhaps it is not so much a hash function that I need but rather a different way to parse the binary.

However if I'm not mistaken a direct translation from Hex to Ascii would most probably include non printable characters which would defeat the purpose, so I suppose I would need a final hash function who's soul purpose is to do just this.

closed as off-topic by Arminius, Steffen Ullrich, Xander, Xiong Chiamiov, crovers Jul 10 '17 at 2:23

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Information security within the scope defined in the help center." – Arminius, Xander, crovers
If this question can be reworded to fit the rules in the help center, please edit the question.

  • The OP starts with the wrong assumption that a hash function returns hex. But, the result of a hash function is binary which can be encoded in different ways, like as hex, base64 or whatever. Since encoding of binary data into ASCII data is not a question of information security I propose to close the question as off-topic. – Steffen Ullrich Jul 9 '17 at 15:41
  • Didn't I already address this in the third paragraph? @SteffenUllrich – Mallow Jul 9 '17 at 16:31
  • In the third paragraph you are still assuming that the hash functions outputs hex. But the result of a hash function is binary not hex. Hex is just one of the ways these binary data can be encoded and is commonly used for encoding since binary data are not easily readable by a human. – Steffen Ullrich Jul 9 '17 at 16:36
  • So the difference you are objecting to is that I didn't specify that the hex is just it's encoding. Suffice to say, I do think I addressed it in the third, and acknowledge that I didn't make a wrong assumption but rather I didn't go into the nitty gritty. I don't think it's worth diving deeper on the particulars of that issue as it derails the original question. How to use the binary and have it encoded to a readable ASCII set. – Mallow Jul 9 '17 at 19:25
  • How to use the binary and have it encoded to a readable ASCII set. - how to encode binary data into ASCII is not an information security question and therefore off-topic. – Steffen Ullrich Jul 9 '17 at 19:30
3

As you say, the output of a hash function is merely a string of bits, and so not restricted to the ASCII character set. So what you'd like to do is encode those bits more efficiently than hexadecimal encoding, which is only 50% efficient.

You should review the list of binary-to-text encoding methods on Wikipedia, but your best bet is Base 85, which is 80% efficient:

The proposed character set is, in order, 0–9, A–Z, a–z, and then
the 23 characters !#$%&()*+-;<=>?@^_`{|}~.

Here is an example of hash output being encoded both ways:

$ python3
>>> import base64
>>> import hashlib
>>> data = 'The quick brown fox jumped over the lazy dog.'
>>> m = hashlib.md5(data.encode('ascii'))
>>> base64.b16encode(m.digest())
b'5C6FFBDD40D9556B73A21E63C3E0E904'
>>> base64.b85encode(m.digest())
b'TyOi`K-pDmbD|z&!{F%z'
>>>

(note that the encoded output strings are python quoted with b'', so the first two characters and the last character are not part of the actual string)

Not the answer you're looking for? Browse other questions tagged or ask your own question.