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

发布于 2024-08-04 19:15:26 字数 277 浏览 3 评论 0原文

在此页面上:

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

它说一旦你生成了一个哈希值,你就会:

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

csharp 中有代码可以做到这一点吗?

on this page:

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

it says once you generate a hash you then:

convert the hash to a hexadecimal character string

is there code in csharp to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慈悲佛祖 2024-08-11 19:15:26

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

编辑:像这样:

byte[] hashBytes = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(str));

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

BitConverter.ToString(hashBytes).Replace("-", "");

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

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);
}

To get the hash, use the System.Security.Cryptography.SHA1Managed class.

EDIT: Like this:

byte[] hashBytes = new SHA1Managed().ComputeHash(Encoding.UTF8.GetBytes(str));

To convert the hash to a hex string, use the following code:

BitConverter.ToString(hashBytes).Replace("-", "");

If you want a faster implementation, use the following function:

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);
}
双手揣兜 2024-08-11 19:15:26

我还不能发表评论,没有足够的代表,但 Lior 将 Java 与 C# 混合在一起是一个非常错误的答案。

C# 中的字节是无符号字节,这与所有其他整数类型完全相反在 C# 中,默认情况下是有签名的。

0xFF 部分完全没有意义,因为即使字节已签名,例如 0xFE 也是 -2。例如,使用按位与 0xFE 和 0xFF 不会阻止负数 0xFE 成为结果。 0x7F 会。

关于最重要的答案,我很确定这些微优化可能会有所帮助,尽管它们是可能不会产生任何真正差异的微优化,因为 JIT 编译器可能只是做其他事情而且因为计算机速度太快了。

    chars[2 * i + 2] = ToHexDigit(bytes[i] / 16);
    chars[2 * i + 3] = ToHexDigit(bytes[i] % 16);

小改动使其使用位移位和按位运算而不是除法器和模数。

    chars[2 * i + 2] = ToHexDigit((bytes[i] >> 4) & 0xF);
    chars[2 * i + 3] = ToHexDigit(bytes[i] & 0xF);

这是另一个“优化”,尽管我认为它更具可读性。也许那是因为我熟记了大部分 ASCII 表。

    private static char ToHexDigit(int i)
    {
        return (char)(i + (i < 10 ? 48 : 55));
    }

如果您想实现小写十六进制(不使用 ToLower),只需将 55 与 87 交换即可。这使得它非常简单。

更新:看起来 Lior 或某人删除了他非常不恰当的答案。这是一个很好的举动。

I can't comment yet, not enough rep, but Lior is mixing Java with C# in a very wrong answer.

byte in C# is an unsigned byte, which is the complete opposite of all other whole number types in C#, which are signed by default.

The 0xFF part is completely pointless because even if byte was signed, 0xFE for example is -2. Using a bitwise-and with 0xFE and 0xFF, for example, would not stop negative number 0xFE from being the outcome. 0x7F would.

Regarding the top answer, I'm pretty sure these micro-optimizations might help, albeit, they're micro-optimizations that are likely to not make any real difference because the JIT compiler might just do something else and because computers are just too blazing fast.

    chars[2 * i + 2] = ToHexDigit(bytes[i] / 16);
    chars[2 * i + 3] = ToHexDigit(bytes[i] % 16);

Small change to make it use bitshift and bitwise ops instead of the divider and modulo.

    chars[2 * i + 2] = ToHexDigit((bytes[i] >> 4) & 0xF);
    chars[2 * i + 3] = ToHexDigit(bytes[i] & 0xF);

This is the other "optimization", although I see it as a more readable thing. Maybe that's because I know most of the ASCII table by heart.

    private static char ToHexDigit(int i)
    {
        return (char)(i + (i < 10 ? 48 : 55));
    }

If you're trying to achieve lower case hex (without ToLower), just swap 55 with 87. It makes it quite simple.

Update: It looks like Lior or someone removed his very off answer. That was a good move.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文