1

I am searching for a library where I need to hash a string which should producer numbers rather than alpha numeric

eg:
Input string: hello world
Salt value: 5467865390
Output value: 9223372036854775808

I have searched many libraries, but those library produces alpha-numeric as output, but I need plain numbers as output.

Is there is any such library? Though the problem of having only numbers as output will have high chance of collision, but though it is fine for my business use case.

EDIT 1: Also I need to control the number of digits in output. I want to store the value in database which has Numeric datatype. So I need to control the number of digits to fit the size within the data type range

Ram Kumar
  • 93
  • 7

2 Answers2

4

Hexadecimal hash codes can be interpreted as (rather large) numbers:

import hashlib
hex_hash = hashlib.sha1('hello world'.encode('utf-8')).hexdigest()
int_hash = int(hex_hash, 16)  # convert hexadecimal to integer
print(hex_hash)
print(int_hash)

outputs

'2aae6c35c94fcfb415dbe95f408b9ce91ee846ed'
243667368468580896692010249115860146898325751533

EDIT: As asked in the comments, to limit the number to a certain range, you can simply use the modulus operator. Note, of course, that this will increase the possibility of collisions. For instance, we can limit the "hash" to 0 .. 9,999,999 with modulus 10,000,000.

limited_hex_hash = hex_hash % 10_000_000
print(limited_hex_hash)

outputs

5751533
AKX
  • 93,995
  • 11
  • 81
  • 98
  • By any chances can we control the number of digits in output? I want to store the value in database which has integer as datatype. So I need to control the number of digits to fit the size within the data type range – Ram Kumar Dec 30 '18 at 14:49
-3

I think there is no need for libraries. You can simply accomplish this with hash() function in python.

InputString="Hello World!!"
HashValue=hash(InputString)
print(HashValue)
print(type(HashValue))

Output:

8831022758553168752                                                                              
<class 'int'> 

Solution for the problem based on Latest EDIT :

The above method is the simplest solution, changing the hash for each invocation will help us prevent attackers from tampering our application.

If you like to switch off the randomization you can simply do that by assigning PYTHONHASHSEED to zero.

For information on switching off the randomization check the official docs https://docs.python.org/3.3/using/cmdline.html#cmdoption-R

  • 1
    This is not a good answer for OP's question (as they are storing the output into a database), since the value of `hash()` changes between invocations of `python` - see https://stackoverflow.com/a/27522708/51685 – AKX Dec 30 '18 at 15:17
  • For your information initially, he doesn`t mention that... I will refrain my answer based on his edit. – Ranjith Udayakumar Dec 30 '18 at 15:27
  • @AKX if you like to switch off the randomization you can simply do that by assigning `PYTHONHASHSEED to zero`. For information on switching off the randomization check the official docs https://docs.python.org/3.3/using/cmdline.html#cmdoption-R If you find it right then please remove the downvote it would be helpful. – Ranjith Udayakumar Dec 30 '18 at 15:38
  • It's still not a generic solution. Nothing guarantees the implementation of `hash()` will be the same between Python versions, for instance. Also, in the case of a web application, I would consider it unacceptable to have to turn hash randomization off. – AKX Dec 30 '18 at 15:50