Is there anyway that I can hash a random string into a 8 digit number without implementing any algorithms myself?
hashCode() method of String class can be used to convert a string into hash code. hashCode() method will return either negative or positive integer hash values. The returned hash value cannot be re-converted back to string once hashed.
The process of hashing in cryptography is to map any string of any given length, to a string with a fixed length. This smaller, fixed length string is known as a hash. To create a hash from a string, the string must be passed into a hash function.
In cryptography, SHA-1 (Secure Hash Algorithm 1) is a cryptographically broken but still widely used hash function which takes an input and produces a 160-bit (20-byte) hash value known as a message digest – typically rendered as a hexadecimal number, 40 digits long.
Yes, you can use the built-in hashlib
module or the built-in hash
function. Then, chop-off the last eight digits using modulo operations or string slicing operations on the integer form of the hash:
>>> s = 'she sells sea shells by the sea shore' >>> # Use hashlib >>> import hashlib >>> int(hashlib.sha1(s.encode("utf-8")).hexdigest(), 16) % (10 ** 8) 58097614L >>> # Use hash() >>> abs(hash(s)) % (10 ** 8) 82148974
Raymond's answer is great for python2 (though, you don't need the abs() nor the parens around 10 ** 8). However, for python3, there are important caveats. First, you'll need to make sure you are passing an encoded string. These days, in most circumstances, it's probably also better to shy away from sha-1 and use something like sha-256, instead. So, the hashlib approach would be:
>>> import hashlib >>> s = 'your string' >>> int(hashlib.sha256(s.encode('utf-8')).hexdigest(), 16) % 10**8 80262417
If you want to use the hash() function instead, the important caveat is that, unlike in Python 2.x, in Python 3.x, the result of hash() will only be consistent within a process, not across python invocations. See here:
$ python -V Python 2.7.5 $ python -c 'print(hash("foo"))' -4177197833195190597 $ python -c 'print(hash("foo"))' -4177197833195190597 $ python3 -V Python 3.4.2 $ python3 -c 'print(hash("foo"))' 5790391865899772265 $ python3 -c 'print(hash("foo"))' -8152690834165248934
This means the hash()-based solution suggested, which can be shortened to just:
hash(s) % 10**8
will only return the same value within a given script run:
#Python 2: $ python2 -c 's="your string"; print(hash(s) % 10**8)' 52304543 $ python2 -c 's="your string"; print(hash(s) % 10**8)' 52304543 #Python 3: $ python3 -c 's="your string"; print(hash(s) % 10**8)' 12954124 $ python3 -c 's="your string"; print(hash(s) % 10**8)' 32065451
So, depending on if this matters in your application (it did in mine), you'll probably want to stick to the hashlib-based approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With