Coldfusion 和 java 加密函数

发布于 2024-11-01 10:17:43 字数 604 浏览 0 评论 0原文

在尝试将用户语音的令牌生成器从java转换为coldfusion时,我注意到java中的哈希函数执行coldfusion中的哈希函数:

String salted = "63bfb29835aedc55aae944e7cc9a202dmbdevsite";
byte[] hash = DigestUtils.sha(salted);

给出= [-19, -18, 7, 92, -121, 13, 88, 68, - 84, 61, -77, -20, -85, -102, -102, -62, -70, 45, -16, 18]

<cfset Salted="63bfb29835aedc55aae944e7cc9a202dmbdevsite" />
<cfset hash=Hash(Salted,"SHA") />
<cfset arrBytes = hash.GetBytes() />

给出= 69686969485553675655486853565252656751686651696765665765576567506665506870484950

谁能解释一下?

谢谢

while trying to translate token generator for uservoice from java to coldfusion, I noticed the the hash function in java does the one in coldfusion :

String salted = "63bfb29835aedc55aae944e7cc9a202dmbdevsite";
byte[] hash = DigestUtils.sha(salted);

gives = [-19, -18, 7, 92, -121, 13, 88, 68, -84, 61, -77, -20, -85, -102, -102, -62, -70, 45, -16, 18]

<cfset Salted="63bfb29835aedc55aae944e7cc9a202dmbdevsite" />
<cfset hash=Hash(Salted,"SHA") />
<cfset arrBytes = hash.GetBytes() />

gives = 69686969485553675655486853565252656751686651696765665765576567506665506870484950

Can anyone explain this ?

Thanks

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

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

发布评论

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

评论(2

如日中天 2024-11-08 10:17:43

您实际上得到了相同的结果,但是输出的编码不同。对于 Java,它是一个字节数组,需要注意的是 byte 是有符号的。对于 ColdFusion,您将获得十六进制,由于某种原因,每个十六进制字符以十进制格式输出。如果您查看 http://asciitable.com/ 并将十进制数字映射到其字符(例如 69 到 E, 68 到 D,48 到 0),您会得到:

EDEE075C870D5844AC3DB3ECAB9A9AC2BA2DF012

哈希结果通常存储为十六进制。如果将 Java 版本编码为十六进制,您将得到相同的结果:

byte[] bytes = { -19, -18, 7, 92, -121, 13, 88, 68, -84, 61, -77, -20,
        -85, -102, -102, -62, -70, 45, -16, 18 };

StringBuilder sb = new StringBuilder(2 * hash.length);
for (byte b : hash) {
    sb.append("0123456789ABCDEF".charAt((b & 0xF0) >> 4));
    sb.append("0123456789ABCDEF".charAt((b & 0x0F)));
}
String hex = sb.toString();
System.out.println(hex);

You are actually getting the same result, however the outputs are encoded differently. For Java it's a byte array, and it's important to note that byte is signed. For ColdFusion you're getting hex that for some reason is outputted in decimal format for each hex character. If you look at http://asciitable.com/ and map the decimal numbers to their characters (e.g. 69 to E, 68 to D, 48 to 0), you get:

EDEE075C870D5844AC3DB3ECAB9A9AC2BA2DF012

Hashed results are often stored as hex. If you encode the Java version into hex, you'll get the same:

byte[] bytes = { -19, -18, 7, 92, -121, 13, 88, 68, -84, 61, -77, -20,
        -85, -102, -102, -62, -70, 45, -16, 18 };

StringBuilder sb = new StringBuilder(2 * hash.length);
for (byte b : hash) {
    sb.append("0123456789ABCDEF".charAt((b & 0xF0) >> 4));
    sb.append("0123456789ABCDEF".charAt((b & 0x0F)));
}
String hex = sb.toString();
System.out.println(hex);
堇色安年 2024-11-08 10:17:43

您可以使用 BinaryDecode 来获取与 Java Hash 相同的字节数组。

<cfset Salted="63bfb29835aedc55aae944e7cc9a202dmbdevsite" />
<cfset hash = Hash(Salted,"SHA") />
<cfset arrBytes = BinaryDecode(hash, "hex") />

You can use BinaryDecode to get the same byte array as the Java Hash.

<cfset Salted="63bfb29835aedc55aae944e7cc9a202dmbdevsite" />
<cfset hash = Hash(Salted,"SHA") />
<cfset arrBytes = BinaryDecode(hash, "hex") />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文