<p>Some useful criteria for evaluating string hashing functions are discussed here:</p>

<ul>
<li><a href="http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.40.1573&amp;rep=rep1&amp;type=pdf" rel="nofollow">Performance in Practice of String Hashing Functions</a></li>
<li><a href="http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=4808979" rel="nofollow">Choosing Best Hashing Strategies and Hash Functions</a></li>
<li><a href="http://home.comcast.net/~bretm/hash/5.html" rel="nofollow">Evaluating Hash Functions</a></li>
<li><a href="http://en.wikipedia.org/wiki/Hash_function#Properties" rel="nofollow">Hash Function properties</a></li>
</ul>

<p>There's an <a href="http://www.strchr.com/hash_functions" rel="nofollow">empirical comparison of some popular string hashing functions here</a> 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.</p>

<p>If you know the input set in advance, you can generate a <a href="http://en.wikipedia.org/wiki/Perfect_hash_function" rel="nofollow">perfect hash function</a> that is collision-free. Something like <a href="http://cmph.sourceforge.net/" rel="nofollow">CMPH</a> or <a href="http://www.gnu.org/software/gperf/" rel="nofollow">gperf</a> can automatically generate a PHF for a set of strings.</p>

<p>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.</p>

<p>For a cryptographically secure hash, you'd want something that has good <a href="http://en.wikipedia.org/wiki/Avalanche_effect" rel="nofollow">avalanche effect</a> characteristics.</p>

<p><a href="http://www.cse.yorku.ca/~oz/hash.html#djb2" rel="nofollow">Bernstein's hash</a> is often used in string dictionary data structure implementations, but of course there are many to choose from. (Also <a href="http://sites.google.com/site/murmurhash/" rel="nofollow">MurmurHash</a>.)</p>

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

CC BY-SA 2.5
2011-02-24T07:03:59.537Z
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
CC BY-SA 2.5
2011-02-24T03:52:58.820Z
2011-02-24T05:54:15.253Z
2011-02-24T05:54:15.253Z
mhadley
<p>SHA-2 (<a href="http://en.wikipedia.org/wiki/SHA-512" rel="nofollow">http://en.wikipedia.org/wiki/SHA-512</a>) is collision resistant and the output (32 or 64 bytes) can be easily converted to a (big) integer.</p>

<p>In Java you can easily do it with:</p>

<pre><code>BigInteger number = new BigInteger(1,messageDigest);
</code></pre>

<p>(Java has a whole integrated <a href="http://download.oracle.com/javase/1.4.2/docs/guide/security/CryptoSpec.html" rel="nofollow">cryptography API</a>)</p>

<p>A SHA-256 Java example:</p>

<pre><code>import java.security.MessageDigest;
public class SHAHashingExample {
public static void main(String[] args)throws Exception {
    String password = "YOUR FAVOURITE STRING HERE"; 
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(password.getBytes()); 
    byte byteData[] = md.digest(); 
    BigInteger number = new BigInteger(1,byteData);
    System.out.println("Your integer : " + number.toString());
}
}
</code></pre>

CC BY-SA 2.5
2011-02-25T00:22:59.957Z
thanxs for the code snippet. However I am looking for a concrete approach like actually hashing algorithms. I have no issue in its implementation.
Shani
CC BY-SA 2.5
2011-02-24T08:24:58.157Z
2011-02-24T08:35:55.470Z
2011-02-24T08:35:55.470Z
Marzio De Biasi
<p>Is it possible to convert a string to a unique number. </p>

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

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

CC BY-SA 2.5
2011-02-24T04:05:38.447Z
Why not dividing the string to parts and compute a fixed length [hash function](http://en.wikipedia.org/wiki/Cryptographic_hash_function) on each part separately?
Kaveh
CC BY-SA 2.5
2011-02-24T04:39:07.393Z
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
CC BY-SA 2.5
2011-02-24T06:48:31.783Z
ps: also see http://en.wikipedia.org/wiki/NIST_hash_function_competition#External_links and http://ehash.iaik.tugraz.at/wiki/The_eHash_Main_Page
Kaveh
CC BY-SA 2.5
2011-02-24T06:53:10.713Z
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
CC BY-SA 2.5
2011-02-24T08:26:43.790Z
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](http://meta.cstheory.stackexchange.com/) to argue against my decision.)
Kaveh
<cr.crypto-security><hash-function>
How to compute Integer Hash of a string
Shani
# How to compute Integer Hash of a string

(Asked by: username_0 on 2011-02-24)

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.

**Tags:** cr.crypto-security, hash-function

> **Comments:**
> **username_1**: Why not dividing the string to parts and compute a fixed length [hash function](http://en.wikipedia.org/wiki/Cryptographic_hash_function) on each part separately?  
> **username_1**: 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.  
> **username_1**: ps: also see http://en.wikipedia.org/wiki/NIST_hash_function_competition#External_links and http://ehash.iaik.tugraz.at/wiki/The_eHash_Main_Page  
> **username_0**: 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.  
> **username_1**: 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](http://meta.cstheory.stackexchange.com/) to argue against my decision.)  

---

## 2 answers

### Answer by username_2 on 2011-02-24. Score: 3

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

* [Performance in Practice of String Hashing Functions](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.40.1573&rep=rep1&type=pdf)
* [Choosing Best Hashing Strategies and Hash Functions](http://ieeexplore.ieee.org/xpl/freeabs_all.jsp?arnumber=4808979)
* [Evaluating Hash Functions](http://home.comcast.net/~bretm/hash/5.html)
* [Hash Function properties](http://en.wikipedia.org/wiki/Hash_function#Properties)

There's an [empirical comparison of some popular string hashing functions here](http://www.strchr.com/hash_functions) 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](http://en.wikipedia.org/wiki/Perfect_hash_function) that is collision-free. Something like [CMPH](http://cmph.sourceforge.net/) or [gperf](http://www.gnu.org/software/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](http://en.wikipedia.org/wiki/Avalanche_effect) characteristics.

[Bernstein's hash](http://www.cse.yorku.ca/~oz/hash.html#djb2) is often used in string dictionary data structure implementations, but of course there are many to choose from. (Also [MurmurHash](http://sites.google.com/site/murmurhash/).)

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

> **Comments:**
> **username_0**: 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.  

---

### Answer by username_3 on 2011-02-24. Score: -1

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 {
    String password = "YOUR FAVOURITE STRING HERE"; 
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(password.getBytes()); 
    byte byteData[] = md.digest(); 
    BigInteger number = new BigInteger(1,byteData);
    System.out.println("Your integer : " + number.toString());
}
}

> **Comments:**
> **username_0**: thanxs for the code snippet. However I am looking for a concrete approach like actually hashing algorithms. I have no issue in its implementation.  

---

