将哈希值转换为十六进制字符串

leo*_*ora 10 c# string hash hex

在本页面:

http://www.shutterfly.com/documentation/OflyCallSignature.sfly

它说,一旦你生成一个哈希你然后:

将哈希值转换为十六进制字符串

csharp中有代码可以执行此操作吗?

SLa*_*aks 15

要获取哈希值,请使用System.Security.Cryptography.SHA1Managed该类.

编辑:像这样:

byte[] hashBytes = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(str));
Run Code Online (Sandbox Code Playgroud)

要将哈希值转换为十六进制字符串,请使用以下代码:

BitConverter.ToString(hashBytes).Replace("-", "");
Run Code Online (Sandbox Code Playgroud)

如果您想要更快的实现,请使用以下函数:

private static char ToHexDigit(int i) {
    if (i < 10) 
        return (char)(i + '0');
    return (char)(i - 10 + 'A');
}
public static string ToHexString(byte[] bytes) {
    var chars = new char[bytes.Length * 2 + 2];

    chars[0] = '0';
    chars[1] = 'x';

    for (int i = 0; i < bytes.Length; i++) {
        chars[2 * i + 2] = ToHexDigit(bytes[i] / 16);
        chars[2 * i + 3] = ToHexDigit(bytes[i] % 16);
    }

    return new string(chars);
}
Run Code Online (Sandbox Code Playgroud)