简体   繁体   中英

How to create simple short hash value? C#

How to create simple hash value? For example I have string "TechnologyIsCool" and how to have hash value from this string?

I want to do some method like:

public string HashThis(string value)
{
    string hashResult = string.Empty;

    ...

    return hashResult;
}

and call this method like:

string hash = HashThis("TechnologyIsCool");

and after that have hash like "5qazws".

Use String.GetHashCode Method

 public static void Main() 
    {
        DisplayHashCode( "Abcdei" );
    }

static void DisplayHashCode( String Operand )
{
    int     HashCode = Operand.GetHashCode( );
    Console.WriteLine("The hash code for \"{0}\" is: 0x{1:X8}, {1}",
                      Operand, HashCode );
}

Use GetHashCode();

public string HashThis(string value)
{
    return hashResult = value.GetHashCode();
}

不,你不能使用String.GetHashCode(),因为这不保证每次都在同一个字符串上返回相同的哈希。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM