Browse Source

tests and fixes for binary Md5/Sha1

Nicolas Cannasse 13 years ago
parent
commit
e5325e603e
3 changed files with 25 additions and 9 deletions
  1. 1 2
      std/haxe/crypto/Md5.hx
  2. 17 5
      std/haxe/crypto/Sha1.hx
  3. 7 2
      tests/unit/TestMisc.hx

+ 1 - 2
std/haxe/crypto/Md5.hx

@@ -45,8 +45,7 @@ class Md5 {
 		#if neko
 			return haxe.io.Bytes.ofData(make_md5(b.getData()));
 		#elseif php
-			throw "Not implemented";
-			return null;
+			return haxe.io.Bytes.ofData(untyped __call__("md5", b.getData(), true));
 		#else
 			var h = new Md5().doEncode(bytes2blks(b));
 			var out = haxe.io.Bytes.alloc(16);

+ 17 - 5
std/haxe/crypto/Sha1.hx

@@ -27,24 +27,34 @@ package haxe.crypto;
 class Sha1 {
 
 	public static function encode( s:String ) : String {
+		#if php
+		return untyped __call__("sha1", s);
+		#else
 		var sh = new Sha1();
 		var h = sh.doEncode(str2blks(s));
 		return sh.hex(h);
+		#end
 	}
 
 	public static function make( b : haxe.io.Bytes ) : haxe.io.Bytes {
+		#if php
+		return haxe.io.Bytes.ofData(untyped __call__("sha1", b.getData(), true));
+		#else
 		var h = new Sha1().doEncode(bytes2blks(b));
-		var out = haxe.io.Bytes.alloc(16);
+		var out = haxe.io.Bytes.alloc(20);
 		var p = 0;
-		for( i in 0...4 ) {
-			out.set(p++,h[i]&0xFF);
-			out.set(p++,(h[i]>>8)&0xFF);
-			out.set(p++,(h[i]>>16)&0xFF);
+		for( i in 0...5 ) {
 			out.set(p++,h[i]>>>24);
+			out.set(p++,(h[i]>>16)&0xFF);
+			out.set(p++,(h[i]>>8)&0xFF);
+			out.set(p++,h[i]&0xFF);
 		}
 		return out;
+		#end
 	}
 
+	#if !php
+
 	function new() {
 	}
 
@@ -171,4 +181,6 @@ class Sha1 {
 		return str;
 	}
 
+	#end
+
 }

+ 7 - 2
tests/unit/TestMisc.hx

@@ -281,14 +281,19 @@ class TestMisc extends Test {
 		eq( haxe.crypto.Md5.encode(""), "d41d8cd98f00b204e9800998ecf8427e" );
 		eq( haxe.crypto.Md5.encode("hello"), "5d41402abc4b2a76b9719d911017c592" );
 		// depending of ISO/UTF8 native
-		allow( haxe.crypto.Md5.encode("héllo"), ["1a722f7e6c801d9e470a10cb91ba406d","be50e8478cf24ff3595bc7307fb91b50"] );
+		allow( haxe.crypto.Md5.encode("héllo"), ["1a722f7e6c801d9e470a10cb91ba406d", "be50e8478cf24ff3595bc7307fb91b50"] );
+		
+		eq( haxe.io.Bytes.ofString("héllo").toHex(), "68c3a96c6c6f");
+		eq( haxe.crypto.Md5.make(haxe.io.Bytes.ofString("héllo")).toHex(), "be50e8478cf24ff3595bc7307fb91b50" );
 	}
 
 	function testSHA1() {
 		eq( haxe.crypto.Sha1.encode(""), "da39a3ee5e6b4b0d3255bfef95601890afd80709" );
 		eq( haxe.crypto.Sha1.encode("hello"), "aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d" );
 		// depending of ISO/UTF8 native
-		allow( haxe.crypto.Sha1.encode("héllo"), ["35b5ea45c5e41f78b46a937cc74d41dfea920890","028db752c14604d624e8b1c121d600c427b8a3ba"] );
+		allow( haxe.crypto.Sha1.encode("héllo"), ["028db752c14604d624e8b1c121d600c427b8a3ba","35b5ea45c5e41f78b46a937cc74d41dfea920890"] );
+		
+		eq( haxe.crypto.Sha1.make(haxe.io.Bytes.ofString("héllo")).toHex(), "35b5ea45c5e41f78b46a937cc74d41dfea920890" );
 	}
 	
 	function testBaseCode() {