38 lines
1.4 KiB
C#
38 lines
1.4 KiB
C#
using System;
|
|
using System.Text;
|
|
|
|
namespace JD.Core
|
|
{
|
|
/// <summary>
|
|
/// Hash codes useful functions
|
|
/// </summary>
|
|
public static class HashCode
|
|
{
|
|
/// <summary>
|
|
/// Compute a 64bits hash code from a string
|
|
/// (From here: http://www.codeproject.com/Articles/34309/Convert-String-to-64bit-Integer)
|
|
/// </summary>
|
|
public static UInt64 GetUInt64HashCode(string strText)
|
|
{
|
|
UInt64 hashCode = 0;
|
|
if (!string.IsNullOrEmpty(strText))
|
|
{
|
|
//Unicode Encode Covering all characterset
|
|
var byteContents = Encoding.Unicode.GetBytes(strText);
|
|
System.Security.Cryptography.SHA256 hash =
|
|
new System.Security.Cryptography.SHA256CryptoServiceProvider();
|
|
var hashText = hash.ComputeHash(byteContents);
|
|
//32Byte hashText separate
|
|
//hashCodeStart = 0~7 8Byte
|
|
//hashCodeMedium = 8~23 8Byte
|
|
//hashCodeEnd = 24~31 8Byte
|
|
//and Fold
|
|
var hashCodeStart = BitConverter.ToUInt64(hashText, 0);
|
|
var hashCodeMedium = BitConverter.ToUInt64(hashText, 8);
|
|
var hashCodeEnd = BitConverter.ToUInt64(hashText, 24);
|
|
hashCode = hashCodeStart ^ hashCodeMedium ^ hashCodeEnd;
|
|
}
|
|
return (hashCode);
|
|
}
|
|
}
|
|
}
|