views:

476

answers:

3

Is there any chance that a SHA-1 hash can be purely numeric, or does the algorithm ensure that there must be at least one alphabetical character?

Edit: I'm representing it in base 16, as a string returned by PHP's sha1() function.

+13  A: 

technically, a SHA1 hash is a number, it is just most often encoded in base 16 (which is what PHP's sha1() does) so that it nearly always has a letter in it. There is no guarantee of this though.

The odds of a hex encoded 160 bit number having no digits A-F are (10/16)40 or about 6.84227766 × 10-9

cobbal
Thanks! Precisely what I wanted to know. :)
Ryan McCue
In more palatable terms: About 1 in every 146,000,000 SHA1s expressed in hexidecimal have no digits higher than 9.
Nick Johnson
For example, hashlib.sha1('169977707').hexdigest() =='5938266572196464632409940308852871296620'
Nick Johnson
+2  A: 

You can represent the output of SHA1 (just like any binary data) in any base you want. Specifically, you can encode the result in base-8/10.

Assaf Lavie
+3  A: 

The SHA-1 hash is a 160 bit number. For the ease of writing it, it is normally written in hexadecimal. Hexadecimal (base 16) digits are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e and f. There is nothing special about the letters. Each hexadecimal character equivalent to 4 bits which means the hash can be written in 40 characters.

I don't believe there is any reason why a SHA-1 hash can't have any letters, but it is improbable. It's like generating a 40 digit (base 10) random number and not getting any 7s, 8s or 9s.

David Johnstone