# How to compute Integer Hash of a string [closed]

Is it possible to convert a string to a unique number.

Similar to any hashing algorithm (MD5, SHA-1 and SHA-2), I want to compute a unique integer value for an arbitrary length string, which should be collusion resistant, hard to invert, and easy to compute.

Can you name the technique to compute it, so that I can dig into the theory of it.

• Why not dividing the string to parts and compute a fixed length hash function on each part separately? – Kaveh Feb 24 '11 at 4:05
• It seems to me that this question is not a research level question and therefore should be closed as off-topic for cstheory, but I am not a crypto expert and therefore will wait for some feedback from more knowledgeable users. – Kaveh Feb 24 '11 at 4:39
• – Kaveh Feb 24 '11 at 6:48
• I am working on research problem to encode access policies as big numbers, for that I need to convert specific access attributes to integers for further manipulation. The existing hashing algorithms encodes the message into message digest i.e., {0-9} {a-z} {A-Z} of fixed length. But I need to convert them to only number whilst maintaining the same properties of hashing algorithm. – Shani Feb 24 '11 at 6:53
• you can read the resulting strings as numbers in base $b$ where $b$ is the number of symbols in the encoding. This does not seem to be a research level question in theoretical computer science so I am closing this question as off-topic. (note: your question can be reopened either by me or by other high reputation users if my decision is wrong or if I misunderstood the question. You can also use meta to argue against my decision.) – Kaveh Feb 24 '11 at 8:26

Some useful criteria for evaluating string hashing functions are discussed here:

There's an empirical comparison of some popular string hashing functions here which looks at some of the criteria you mentioned. The appropriateness of various hashes would depend on the application and data characteristics. Uniformity and efficiency are usually the major concerns.

If you know the input set in advance, you can generate a perfect hash function that is collision-free. Something like CMPH or gperf can automatically generate a PHF for a set of strings.

If the input isn't known in advance, it still might be biased toward a particular distribution that makes one hash more favorable than other. Benchmarking can be useful.

For a cryptographically secure hash, you'd want something that has good avalanche effect characteristics.

Bernstein's hash is often used in string dictionary data structure implementations, but of course there are many to choose from. (Also MurmurHash.)

I misunderstood the question - this is for integers that are word-sized. For a secure crypto hash like SHA-2, see Kaveh's comments.

• thanks for the reply, I want to convert a string to arbitrary number that will be unique (similar to message digest, but I want message digest be a number only). I think it is little bit different from lookup strategies you have suggested. – Shani Feb 24 '11 at 7:03

SHA-2 (http://en.wikipedia.org/wiki/SHA-512) is collision resistant and the output (32 or 64 bytes) can be easily converted to a (big) integer.

In Java you can easily do it with:

BigInteger number = new BigInteger(1,messageDigest);


(Java has a whole integrated cryptography API)

A SHA-256 Java example:

import java.security.MessageDigest;
public class SHAHashingExample {
public static void main(String[] args)throws Exception {