来自 as3crypto 的 sha1 散列与用 PHP 制作的散列不同 [英] sha1 hash from as3crypto differs from the one made with PHP
问题描述
在 as3 中使用 as3crypto 从字符串 '12345' 生成 SHA1 哈希值,方法与示例中相同:
Make SHA1 hash from string '12345' with as3crypto in as3 the same way how it is done in there example:
var sha1:SHA1 = new SHA1;
var src:ByteArray = Hex.toArray("12345");
var digest:ByteArray = sha1.hash(src);
trace('SHA:' + Hex.fromArray(digest));
结果:ec60c0fd70d82a7785f6c9a02dbe16f2e40b1344
result : ec60c0fd70d82a7785f6c9a02dbe16f2e40b1344
从 PHP 中的相同字符串生成 SHA1:
Make SHA1 from the same string in PHP:
print "SHA:".sha1("12345");
结果:8cb2237d0679ca88db6464eac60da96345513964
result : 8cb2237d0679ca88db6464eac60da96345513964
如果我尝试其他工具来获取哈希,我会得到第二个结果,所以看起来 PHP 的结果是正确的.
If I try other tools to obtain hash I get the second result, so it looks like the result from PHP is correct.
问题:如何使用 as3crypto 获得相同的哈希值?
顺便说一句:在测试时,我发现 as3crypto 的另一种方式给了我另一个(错误?)结果:
BTW: when testing I found that another way with as3crypto gives me another (wrong?) result:
var src:ByteArray = new ByteArray();
src.writeUTF("12345");
var digest:ByteArray = sha1.hash(src);
trace('SHA:' + Hex.fromArray(digest));
结果:b98cfbc53daec4029895585ab198f7403d0d0506
result : b98cfbc53daec4029895585ab198f7403d0d0506
推荐答案
使用 as3crypto lib 匹配 php sha1 的正确方法是执行以下操作:
The correct way to match a php sha1 using as3crypto lib is to do the following:
var src:ByteArray = Hex.toArray(Hex.fromString(srcString));
var sha1:SHA1 = new SHA1();
var hashedString:String = Hex.fromArray(sha1.hash( src ));
第一个额外的 Hex.fromString 避免了其他人提到的十进制转换.
The first additional Hex.fromString avoids your decimal conversion as others have mentioned.
注意:as3corelib 版本要简单得多:as3corelib SHA1
Note: The as3corelib version is much simpler: as3corelib SHA1
var hashedString:String = SHA1.hash( srcString );
这篇关于来自 as3crypto 的 sha1 散列与用 PHP 制作的散列不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!