我有一个纯文本的用户表,并将其迁移到成员(member)提供商。
使用 ColdFusion(当前系统),我成功地对一个用户的密码(测试用户)进行了哈希处理,并且它完全匹配。但现在后续用户不匹配。我做错了什么。
<cfscript>
theEncoding = "UTF-16LE";
thePassword = "dtD3v310p3r!";
base64Salt = "JZjdzUXREM0A7DPI3FV3iQ==";
theSalt = charsetEncode( binaryDecode(base64Salt, "base64"), theEncoding );
theHash = hash(theSalt & thePassword, "SHA1", theEncoding);
// hash always returns hex. convert it to base64 so it matches DNN
theBase64Hash = binaryEncode(binaryDecode(theHash, "hex"), "base64");
WriteOutput("<br />theBase64Hash= "& theBase64Hash &"<br/>");
WriteOutput("DBPassword= 5khDDMmoFtW+j99r/whE/TjyIUo= <br />");
theEncoding = "UTF-16LE";
thePassword = "DT!@12";
base64Salt = "+muo6gAmjvvyy5doTdjyaA==";
theSalt = charsetEncode( binaryDecode(base64Salt, "base64"), theEncoding );
theHash = hash(theSalt & thePassword, "SHA1", theEncoding);
// hash always returns hex. convert it to base64 so it matches DNN
theBase64Hash = binaryEncode(binaryDecode(theHash, "hex"), "base64");
WriteOutput("<br />theBase64Hash= "& theBase64Hash &"<br/>");
WriteOutput("DBPassword= nfcqQBgeAm0Dp1oGZI0O70Y6DvA= <br />");
</cfscript>
第一个100%有效。但第二个没有。
第二个生成的哈希值为 86SrPKXW5xywDYoC8MVy0q259sQ=
最佳答案
嗯..我认为当两个值连接时可能会出现问题。散列实际上应该使用字节数组,like with the encrypt version ,但不幸的是CF9的hash()函数不支持它 - 只支持字符串。 (虽然文档很少,但 CF11 中支持它)。我不确定 CF9 是否有纯粹的 CF 解决方法。但是,同时您可以直接使用 java:
<cfscript>
thePassword = "DT!@12";
base64Salt = "+muo6gAmjvvyy5doTdjyaA==";
// extract bytes of the salt and password
saltBytes = binaryDecode(base64Salt, "base64");
passBytes = charsetDecode(thePassword, "UTF-16LE" );
// next combine the bytes. note, the returned arrays are immutable,
// so we cannot use the standard CF tricks to merge them
ArrayUtils = createObject("java", "org.apache.commons.lang.ArrayUtils");
dataBytes = ArrayUtils.addAll( saltBytes, passBytes );
// hash binary using java
MessageDigest = createObject("java", "java.security.MessageDigest").getInstance("SHA-1");
MessageDigest.update(dataBytes);
theBase64Hash = binaryEncode(MessageDigest.digest(), "base64");
WriteOutput("<br />theBase64Hash= "& theBase64Hash &"<br/>");
WriteOutput("DBPassword= nfcqQBgeAm0Dp1oGZI0O70Y6DvA= <br />");
</cfscript>
更新:
经过进一步研究,我认为不存在纯粹的 CF 解决方案。 UTF-16LE 编码只是问题的一部分。另一个问题是,DNN 单独解码每个字符串,这可能会产生与解码为单个字符串时不同的字节(请参见下面的比较)。对于您的第二个密码来说确实如此,这就是最终哈希值不同的原因。由于 hash
不接受字节数组,因此我认为它不是完成这项工作的正确工具。 MessageDigest
是正确的选择。
字节数组比较
old| new |
1 | -6 | -6 |
2 | 107 | 107 |
3 | -88 | -88 |
4 | -22 | -22 |
5 | 0 | 0 |
6 | 38 | 38 |
7 | -114 | -114 |
8 | -5 | -5 |
9 | -14 | -14 |
10 | -53 | -53 |
11 | -105 | -105 |
12 | 104 | 104 |
13 | -3 | 77 | **
14 | -1 | -40 | **
15 | 68 | -14 | **
16 | 0 | 104 | **
17 | 84 | 68 | **
18 | 0 | 0 |
19 | 33 | 84 | **
20 | 0 | 0 |
21 | 64 | 33 | **
22 | 0 | 0 |
23 | 49 | 64 | **
24 | 0 | 0 |
25 | 50 | 49 | **
26 | 0 | 0 |
27 | | 50 | **
28 | | 0 | **
- 旧 => charsetDecode(theSalt & thePassword, "UTF-16LE")
- new => ArrayUtils.addAll( saltBytes, passBytes );
关于sql-server-2005 - 所有用户的成员身份 SHA1 哈希值不相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17111641/