sha1 hash from as3crypto differs from the one made with PHP

心不动则不痛 提交于 2019-12-03 21:02:17

The hexadecimal (you're converting it with Hex.toArray) value of 12345 is not the same as the string "12345".

You are converting a decimal number to a hexadecimal byte array and hashing it, and then comparing it to a hash of a string generated in PHP. These will never match.

If you absolutely need to compare two hex number together then a change to your PHP like this should probably work.

print "SHA:" . sha1(dechex(12345));

See the dechex PHP documentation for more.

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

The first additional Hex.fromString avoids your decimal conversion as others have mentioned.

Note: The as3corelib version is much simpler: as3corelib SHA1

var hashedString:String = SHA1.hash( srcString );

The PHP output is definitely correct. I tested it against MySQL's sha1 function:

mysql> select sha1('12345');
+------------------------------------------+
| sha1('12345')                            |
+------------------------------------------+
| 8cb2237d0679ca88db6464eac60da96345513964 |
+------------------------------------------+

The likely culprit is this - you're using Hex.toArray() on the input data in this line:

var src:ByteArray = Hex.toArray("12345");

When you need the original string to be in the byte array. I don't know AS3, though, so can't answer why your second attempt also failed.

var sha1:SHA1 = new SHA1(); 
var src:ByteArray = new ByteArray(); 
src.writeUTFBytes("12345"); 
trace( Hex.fromArray( sha1.hash( src ) ) );

using writeUTFBytes, this method write the string into bytesarray object without BOM.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!