I want to get 64 bit hash code of given string. How can i do that with fastest way ? There is a ready method for get 32 bit hash code but i need 64 bit.
I am looking for only integer hashing. Not md5.
Thank you very much.
C# 4.0
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
Since the question was about making URL I presume you always need the same hashed 64 bit int. GetHashCode is not relyable in this way. To make a hash with few collisions i use this one.
public static ulong GetUInt64Hash(HashAlgorithm hasher, string text)
{
using (hasher)
{
var bytes = hasher.ComputeHash(Encoding.Default.GetBytes(text));
Array.Resize(ref bytes, bytes.Length + bytes.Length % 8); //make multiple of 8 if hash is not, for exampel SHA1 creates 20 bytes.
return Enumerable.Range(0, bytes.Length / 8) // create a counter for de number of 8 bytes in the bytearray
.Select(i => BitConverter.ToUInt64(bytes, i * 8)) // combine 8 bytes at a time into a integer
.Aggregate((x, y) =>x ^ y); //xor the bytes together so you end up with a ulong (64-bit int)
}
}
To use it just pass whatever hashalgorithm you prefer
ulong result = GetUInt64Hash(SHA256.Create(), "foodiloodiloo")
//result: 259973318283508806
or
ulong result = GetUInt64Hash(SHA1.Create(), "foodiloodiloo")
//result: 6574081600879152103
Difference between this one and the accepted answer is that this one XOR's all the bits, and you can use whatever algorithm you want
I'll introduce a new possible answer. xxHash is very fast. Check out the benchmarks here:
https://cyan4973.github.io/xxHash/
It has a NuGet package: https://www.nuget.org/packages/System.Data.HashFunction.xxHash
Or open sources: https://github.com/brandondahler/Data.HashFunction/blob/master/src/System.Data.HashFunction.xxHash/xxHash_Implementation.cs
The other answers here are either 1. questionable as to their real prevention of collision or 2. just wrappers around the large and slow existing HashAlgorithm implementations.
xxHash is not cryptographic strength, but it would seem to fit the bill better for what you need. Its:
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