gpt4 book ai didi

c# - 在 C# 和 mysql 中制作相同的 sha512

转载 作者:行者123 更新时间:2023-11-29 02:33:46 27 4
gpt4 key购买 nike

我想在 C# 和 mysql 中制作一个相同的 sha512。

c#

System.Security.Cryptography.SHA512.Create().ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes("test"));

C# 结果长度为 64

238 38 176 221 74 247 231 73 170 26 142 227 193 10 233 146 63 97 137 128 119 46 71 63 136 25 165 212 148 14 13 178 122 193 133 248 160 225 213 248 79 136 188 136 127 214 123 20 55 50 195 4 204 95 169 173 142 111 87 245 0 40 168 255 

数据库

INSERT INTO users_table (password) VALUES (  SHA2("test", 512));
// password is a BINARY(128)

mysql结果长度128

101 101 50 54 98 48 100 100 52 97 102 55 101 55 52 57 97 97 49 97 56 101 101 51 99 49 48 97 101 57 57 50 51 102 54 49 56 57 56 48 55 55 50 101 52 55 51 102 56 56 49 57 97 53 100 52 57 52 48 101 48 100 98 50 55 97 99 49 56 53 102 56 97 48 101 49 100 53 102 56 52 102 56 56 98 99 56 56 55 102 100 54 55 98 49 52 51 55 51 50 99 51 48 52 99 99 53 102 97 57 97 100 56 101 54 102 53 55 102 53 48 48 50 56 97 56 102 102 

我以字节为单位比较它们,但结果彼此不同:(

感谢您的帮助!

问候约翰

最佳答案

MySql 正在返回一个十六进制编码的字符串:

As of MySQL 5.5.6, the return value is a nonbinary string in the connection character set. Before 5.5.6, the return value is a binary string; see the notes at the beginning of this section about using the value as a nonbinary string.

你可以看出这是因为 C# 的第一个字节是 238,也就是 0xee,而 e 的 ASCII 码是 101 -- MySql 的返回以 101 101 开头。所以你只需要再次使用 UNHEX 将十六进制编码的字符串转换为二进制字符串。 (但仅限于 MySql > 5.5.6)。

因此,为了在您的案例中获得相同的结果:

INSERT INTO users_table (password) VALUES (UNHEX(SHA2("test", 512))); 

并使用 MySql conditional comments (哦,太丑了!)可以跨版本移植:

INSERT INTO users_table (password) VALUES (/*!50506 UNHEX(*/SHA2("test",512)/*!50506 )*/); 

关于c# - 在 C# 和 mysql 中制作相同的 sha512,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8556181/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com