18

I need to store fixed-length (up to 8 digits) numbers produced from a variable length strings. The hash need not be unique. It just needs to change when input string changes. Is there a hash function in .Net that does this?

Thanks
Kishore.

3 Answers 3

26

I assume you are doing this because you need to store the value elsewhere and compare against it. Thus Zach's answer (while entirely correct) may cause you issues since the contract for String.GetHashCode() is explicit about its scope for changing.

Thus here is a fixed and easily repeatable in other languages version.

I assume you will know at compile time the number of decimal digits available. This is based on the Jenkins One At a Time Hash (as implemented and exhaustively tested by Bret Mulvey), as such it has excellent avalanching behaviour (a change of one bit in the input propagates to all bits of the output) which means the somewhat lazy modulo reduction in bits at the end is not a serious flaw for most uses (though you could do better with more complex behaviour)

const int MUST_BE_LESS_THAN = 100000000; // 8 decimal digits

public int GetStableHash(string s)
{
    uint hash = 0;
    // if you care this can be done much faster with unsafe 
    // using fixed char* reinterpreted as a byte*
    foreach (byte b in System.Text.Encoding.Unicode.GetBytes(s))
    {   
        hash += b;
        hash += (hash << 10);
        hash ^= (hash >> 6);    
    }
    // final avalanche
    hash += (hash << 3);
    hash ^= (hash >> 11);
    hash += (hash << 15);
    // helpfully we only want positive integer < MUST_BE_LESS_THAN
    // so simple truncate cast is ok if not perfect
    return (int)(hash % MUST_BE_LESS_THAN);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Code is missing a semi-colon on last line of code. Tried to edit but SO requires 6 changes.
6

Simple approach (note that this is platform-dependent):

int shorthash = "test".GetHashCode() % 100000000; // 8 zeros
if (shorthash < 0) shorthash *= -1;

3 Comments

This will not render the same value for two different strings with the same contents
@joshperry: Thanks, I've added a disclaimer in the answer.
@joshperry - er, yes it will.. it just isn't guaranteed to remain the same between .NET versions. However, no hash can guarantee to change when the input text changes - collisions, although unlikely, will happen (very, very, very rarely).
0

Use System.Security.Cryptography.MD5CryptoServiceProvider.ComputeHash to get a MD5 hash, truncate it to the desired length.

2 Comments

not addressing the OP requirement but this is the most reliable solution without any collision..
@Luckylukee why doesn't this address the OP requirment?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.