bhanu
bhanu

Reputation: 149

Do the SHA1 of a string always return ASCII characters?

The input string can be a unicode string.Do the output string after calculating SHA1 will always return ASCII characters?

Upvotes: 12

Views: 10798

Answers (3)

Graeme Perrow
Graeme Perrow

Reputation: 57258

It depends but strictly speaking, no. The output of the SHA-1 hash is 160 bits, or 20 bytes, but the bytes are not guaranteed to be in the ASCII range.

However, some hash functions output the hex equivalent (i.e. 40 characters) of the 20 bytes, so if the first three bytes of the actual hash are 0x7e, 0x03, and 0xb2, the output would begin with "7e03b2", in which case the output is ASCII.

Upvotes: 9

Yann Ramin
Yann Ramin

Reputation: 33197

SHA1 returns 20 bytes. SHA1 does not deal with encodings, text, ASCII, etc.

One common way to represent binary data is by encoding it in hexadecimal - in this case, the output is always [a-f][0-9]

Upvotes: 5

bdonlan
bdonlan

Reputation: 231303

sha1 returns a binary string. Some sha1 functions may, as a convenience, also encode that binary string into hexadecimal or base64 - if so, the result will be ASCII characters. But sha1 itself does not return ASCII.

Upvotes: 4

Related Questions