소스 검색

move Md5 and Sha1 to haxe.crypto + use native ops instead of Int32

Nicolas Cannasse 13 년 전
부모
커밋
55331b8107
4개의 변경된 파일247개의 추가작업 그리고 165개의 파일을 삭제
  1. 0 152
      std/haxe/SHA1.hx
  2. 10 0
      std/haxe/crypto/Adler32.hx
  3. 63 13
      std/haxe/crypto/Md5.hx
  4. 174 0
      std/haxe/crypto/Sha1.hx

+ 0 - 152
std/haxe/SHA1.hx

@@ -1,152 +0,0 @@
-/*
- * Copyright (c) 2005-2010, The haXe Project Contributors
- * All rights reserved.
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   - Redistributions of source code must retain the above copyright
- *     notice, this list of conditions and the following disclaimer.
- *   - Redistributions in binary form must reproduce the above copyright
- *     notice, this list of conditions and the following disclaimer in the
- *     documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- */
-package haxe;
-import haxe.Int32;
-
-class SHA1 {
-
-	static var hex_chr = "0123456789abcdef";
-
-	inline static function newInt32( left:Int, right:Int ) : Int32 {
-		var result = Int32.ofInt(left);
-		result = Int32.shl(result, 16);
-		return Int32.add(result, Int32.ofInt(right));
-	}
-
-	public static function encode( s:String ) : String {
-		var x = str2blks_SHA1( s );
-		var w = new Array<Int32>();
-
-		var a = newInt32(0x6745, 0x2301);
-		var b = newInt32(0xEFCD, 0xAB89);
-		var c = newInt32(0x98BA, 0xDCFE);
-		var d = newInt32(0x1032, 0x5476);
-		var e = newInt32(0xC3D2, 0xE1F0);
-
-		var i = 0;
-		while( i < x.length ) {
-			var olda = a;
-			var oldb = b;
-			var oldc = c;
-			var oldd = d;
-			var olde = e;
-
-			var j = 0;
-			while( j < 80 ) {
-				if(j < 16)
-					w[j] = x[i + j];
-				else
-					w[j] = rol(Int32.xor(Int32.xor(Int32.xor(w[j-3], w[j-8]), w[j-14]), w[j-16]), 1);
-				var t = add(add(rol(a, 5), ft(Int32.ofInt(j), b, c, d)), add(add(e, w[j]), kt(Int32.ofInt(j))));
-				e = d;
-				d = c;
-				c = rol(b, 30);
-				b = a;
-				a = t;
-
-				j++;
-			}
-			a = add( a, olda );
-			b = add( b, oldb );
-			c = add( c, oldc );
-			d = add( d, oldd );
-			e = add( e, olde );
-			i += 16;
-		}
-		return hex(a) + hex(b) + hex(c) + hex(d) + hex(e);
-	}
-
-	static function hex( num : Int32 ) : String {
-		var str = "";
-		var j = 7;
-		while( j >= 0 ) {
-			str += hex_chr.charAt( Int32.toInt( Int32.and((Int32.shr(num, j*4)), Int32.ofInt(0x0F)) ) );
-			j--;
-		}
-		return str;
-	}
-
-	/**
-		Convert a string to a sequence of 16-word blocks, stored as an array.
-		Append padding bits and the length, as described in the SHA1 standard.
-	 */
-	static function str2blks_SHA1( s :String ) : Array<Int32> {
-		var nblk = ((s.length + 8) >> 6) + 1;
-		var blks = new Array<Int32>();
-
-		for (i in 0...nblk*16)
-			blks[i] = Int32.ofInt(0);
-		for (i in 0...s.length){
-			var p = i >> 2;
-			var c = Int32.ofInt(s.charCodeAt(i));
-			blks[p] = Int32.or(blks[p], Int32.shl(c, (24 - (i % 4) * 8)));
-		}
-		var i = s.length;
-		var p = i >> 2;
-		blks[p] = Int32.or(blks[p], Int32.shl(Int32.ofInt(0x80), (24 - (i % 4) * 8)));
-		blks[nblk * 16 - 1] = Int32.ofInt(s.length * 8);
-		return blks;
-	}
-
-	/**
-		Add integers, wrapping at 2^32.
-	 */
-	static function add( x : Int32, y : Int32 ) : Int32 {
-		var lsw = Int32.add(Int32.and(x, Int32.ofInt(0xFFFF)), Int32.and(y, Int32.ofInt(0xFFFF)));
-		var msw = Int32.add(Int32.add(Int32.shr(x, 16), Int32.shr(y, 16)), Int32.shr(lsw, 16));
-		return Int32.or(Int32.shl(msw, 16), Int32.and(lsw, Int32.ofInt(0xFFFF)));
-	}
-
-	/**
-		Bitwise rotate a 32-bit number to the left
-	 */
-	static function rol( num : Int32, cnt : Int ) : Int32 {
-		return Int32.or(Int32.shl(num, cnt), Int32.ushr(num, (32 - cnt)));
-	}
-
-	/**
-		Perform the appropriate triplet combination function for the current iteration
-	*/
-	static function ft( t : Int32, b : Int32, c : Int32, d : Int32 ) : Int32 {
-		if (Int32.compare(t, Int32.ofInt(20)) <0) return Int32.or(Int32.and(b, c), Int32.and((Int32.complement(b)), d));
-		if (Int32.compare(t, Int32.ofInt(40)) <0) return Int32.xor(Int32.xor(b, c), d);
-		if (Int32.compare(t, Int32.ofInt(60)) <0) return Int32.or(Int32.or(Int32.and(b, c), Int32.and(b, d)), Int32.and(c, d));
-		return Int32.xor(Int32.xor(b, c), d);
-	}
-
-	/**
-		Determine the appropriate additive constant for the current iteration
-	*/
-	static function kt( t : Int32 ) : Int32 {
-		if (Int32.compare(t,Int32.ofInt(20)) < 0)
-			return newInt32(0x5A82, 0x7999);
-		if (Int32.compare(t,Int32.ofInt(40)) < 0)
-			return newInt32(0x6ed9, 0xeba1);
-		if (Int32.compare(t,Int32.ofInt(60)) < 0)
-			return newInt32(0x8f1b, 0xbcdc);
-		return newInt32(0xca62, 0xc1d6);
-	}
-
-}

+ 10 - 0
std/haxe/crypto/Adler32.hx

@@ -36,6 +36,10 @@ class Adler32 {
 		a2 = 0;
 		a2 = 0;
 	}
 	}
 
 
+	public function get() {
+		return (a2 << 16) | a1;
+	}
+
 	public function update( b : haxe.io.Bytes, pos, len ) {
 	public function update( b : haxe.io.Bytes, pos, len ) {
 		var a1 = a1, a2 = a2;
 		var a1 = a1, a2 = a2;
 		for( p in pos...pos + len ) {
 		for( p in pos...pos + len ) {
@@ -62,4 +66,10 @@ class Adler32 {
 		return a;
 		return a;
 	}
 	}
 
 
+	public static function make( b : haxe.io.Bytes ) {
+		var a = new Adler32();
+		a.update(b,0,b.length);
+		return a.get();
+	}
+
 }
 }

+ 63 - 13
std/haxe/Md5.hx → std/haxe/crypto/Md5.hx

@@ -22,7 +22,7 @@
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  * DAMAGE.
  * DAMAGE.
  */
  */
-package haxe;
+package haxe.crypto;
 
 
 /**
 /**
 	Creates a MD5 of a String.
 	Creates a MD5 of a String.
@@ -35,7 +35,29 @@ class Md5 {
 		#elseif php
 		#elseif php
 			return untyped __call__("md5", s);
 			return untyped __call__("md5", s);
 		#else
 		#else
-			return new Md5().doEncode(s);
+			var m = new Md5();
+			var h = m.doEncode(str2blks(s));
+			return m.hex(h);
+		#end
+	}
+
+	public static function make( b : haxe.io.Bytes ) : haxe.io.Bytes {
+		#if neko
+			return haxe.io.Bytes.ofData(make_md5(b.getData()));
+		#elseif php
+			throw "Not implemented";
+			return null;
+		#else
+			var h = new Md5().doEncode(bytes2blks(b));
+			var out = haxe.io.Bytes.alloc(16);
+			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);
+				out.set(p++,h[i]>>>24);
+			}
+			return out;
 		#end
 		#end
 	}
 	}
 
 
@@ -79,26 +101,55 @@ class Md5 {
 		return (msw << 16) | (lsw & 0xFFFF);
 		return (msw << 16) | (lsw & 0xFFFF);
 	}
 	}
 
 
-	function rhex( num ){
+	function hex( a : Array<Int> ){
 		var str = "";
 		var str = "";
 		var hex_chr = "0123456789abcdef";
 		var hex_chr = "0123456789abcdef";
-		for( j in 0...4 ){
-			str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) +
-						 hex_chr.charAt((num >> (j * 8)) & 0x0F);
-		}
+		for( num in a )
+			for( j in 0...4 )
+				str += hex_chr.charAt((num >> (j * 8 + 4)) & 0x0F) +
+							 hex_chr.charAt((num >> (j * 8)) & 0x0F);
 		return str;
 		return str;
 	}
 	}
 
 
-	function str2blks( str : String ){
+	static function bytes2blks( b : haxe.io.Bytes ){
+		var nblk = ((b.length + 8) >> 6) + 1;
+		var blks = new Array();
+
+		//preallocate size
+		var blksSize = nblk * 16;
+		#if (neko || cs || cpp || java)
+		blks[blksSize - 1] = 0;
+		#end
+
+		#if !(cpp || cs) //C++ and C# will already initialize them with zeroes.
+		for( i in 0...blksSize ) blks[i] = 0;
+		#end
+
+		var i = 0;
+		while( i < b.length ) {
+			blks[i >> 2] |= b.get(i) << ((((b.length << 3) + i) & 3) << 3);
+			i++;
+		}
+		blks[i >> 2] |= 0x80 << (((b.length * 8 + i) % 4) * 8);
+		var l = b.length * 8;
+		var k = nblk * 16 - 2;
+		blks[k] = (l & 0xFF);
+		blks[k] |= ((l >>> 8) & 0xFF) << 8;
+		blks[k] |= ((l >>> 16) & 0xFF) << 16;
+		blks[k] |= ((l >>> 24) & 0xFF) << 24;
+		return blks;
+	}
+
+	static function str2blks( str : String ){
 		var nblk = ((str.length + 8) >> 6) + 1;
 		var nblk = ((str.length + 8) >> 6) + 1;
 		var blks = new Array();
 		var blks = new Array();
-		
+
 		//preallocate size
 		//preallocate size
 		var blksSize = nblk * 16;
 		var blksSize = nblk * 16;
 		#if (neko || cs || cpp || java)
 		#if (neko || cs || cpp || java)
 		blks[blksSize - 1] = 0;
 		blks[blksSize - 1] = 0;
 		#end
 		#end
-		
+
 		#if !(cpp || cs) //C++ and C# will already initialize them with zeroes.
 		#if !(cpp || cs) //C++ and C# will already initialize them with zeroes.
 		for( i in 0...blksSize ) blks[i] = 0;
 		for( i in 0...blksSize ) blks[i] = 0;
 		#end
 		#end
@@ -142,9 +193,8 @@ class Md5 {
 		return cmn(bitXOR(c, bitOR(b, (~d))), a, b, x, s, t);
 		return cmn(bitXOR(c, bitOR(b, (~d))), a, b, x, s, t);
 	}
 	}
 
 
-	function doEncode( str:String ) : String {
+	function doEncode( x : Array<Int> ) : Array<Int> {
 
 
-		var x = str2blks(str);
 		var a =  1732584193;
 		var a =  1732584193;
 		var b = -271733879;
 		var b = -271733879;
 		var c = -1732584194;
 		var c = -1732584194;
@@ -232,7 +282,7 @@ class Md5 {
 
 
 			i += 16;
 			i += 16;
 		}
 		}
-		return rhex(a) + rhex(b) + rhex(c) + rhex(d);
+		return [a,b,c,d];
 	}
 	}
 
 
 	#end
 	#end

+ 174 - 0
std/haxe/crypto/Sha1.hx

@@ -0,0 +1,174 @@
+/*
+ * Copyright (c) 2005-2010, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package haxe.crypto;
+
+class Sha1 {
+
+	public static function encode( s:String ) : String {
+		var sh = new Sha1();
+		var h = sh.doEncode(str2blks(s));
+		return sh.hex(h);
+	}
+
+	public static function make( b : haxe.io.Bytes ) : haxe.io.Bytes {
+		var h = new Sha1().doEncode(bytes2blks(b));
+		var out = haxe.io.Bytes.alloc(16);
+		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);
+			out.set(p++,h[i]>>>24);
+		}
+		return out;
+	}
+
+	function new() {
+	}
+
+	function doEncode( x : Array<Int> ) : Array<Int> {
+		var w = new Array<Int>();
+
+		var a = 0x67452301;
+		var b = 0xEFCDAB89;
+		var c = 0x98BADCFE;
+		var d = 0x10325476;
+		var e = 0xC3D2E1F0;
+
+		var i = 0;
+		while( i < x.length ) {
+			var olda = a;
+			var oldb = b;
+			var oldc = c;
+			var oldd = d;
+			var olde = e;
+
+			var j = 0;
+			while( j < 80 ) {
+				if(j < 16)
+					w[j] = x[i + j];
+				else
+					w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
+				var t = rol(a, 5) + ft(j, b, c, d) + e + w[j] + kt(j);
+				e = d;
+				d = c;
+				c = rol(b, 30);
+				b = a;
+				a = t;
+				j++;
+			}
+			a += olda;
+			b += oldb;
+			c += oldc;
+			d += oldd;
+			e += olde;
+			i += 16;
+		}
+		return [a,b,c,d,e];
+	}
+
+
+	/*
+		Convert a string to a sequence of 16-word blocks, stored as an array.
+		Append padding bits and the length, as described in the SHA1 standard.
+	 */
+	static function str2blks( s :String ) : Array<Int> {
+		var nblk = ((s.length + 8) >> 6) + 1;
+		var blks = new Array<Int>();
+
+		for (i in 0...nblk*16)
+			blks[i] = 0;
+		for (i in 0...s.length){
+			var p = i >> 2;
+			blks[p] |= s.charCodeAt(i) << (24 - ((i & 3) << 3));
+		}
+		var i = s.length;
+		var p = i >> 2;
+		blks[p] |= 0x80 << (24 - ((i & 3) << 3));
+		blks[nblk * 16 - 1] = s.length * 8;
+		return blks;
+	}
+
+	static function bytes2blks( b : haxe.io.Bytes ) : Array<Int> {
+		var nblk = ((b.length + 8) >> 6) + 1;
+		var blks = new Array<Int>();
+
+		for (i in 0...nblk*16)
+			blks[i] = 0;
+		for (i in 0...b.length){
+			var p = i >> 2;
+			blks[p] |= b.get(i) << (24 - ((i & 3) << 3));
+		}
+		var i = b.length;
+		var p = i >> 2;
+		blks[p] |= 0x80 << (24 - ((i & 3) << 3));
+		blks[nblk * 16 - 1] = b.length * 8;
+		return blks;
+	}
+
+	/**
+		Bitwise rotate a 32-bit number to the left
+	 */
+	inline function rol( num : Int, cnt : Int ) : Int {
+		return (num << cnt) | (num >>> (32 - cnt));
+	}
+
+	/**
+		Perform the appropriate triplet combination function for the current iteration
+	*/
+	function ft( t : Int, b : Int, c : Int, d : Int ) : Int {
+		if ( t < 20 ) return (b & c) | ((~b) & d);
+		if ( t < 40 ) return b ^ c ^ d;
+		if ( t < 60 ) return (b & c) | (b & d) | (c & d);
+		return b ^ c ^ d;
+	}
+
+	/**
+		Determine the appropriate additive constant for the current iteration
+	*/
+	function kt( t : Int ) : Int {
+		if( t < 20)
+			return 0x5A827999;
+		if ( t < 40)
+			return 0x6ED9EBA1;
+		if (t < 60)
+			return 0x8F1BBCDC;
+		return 0xCA62C1D6;
+	}
+
+	function hex( a : Array<Int> ){
+		var str = "";
+		var hex_chr = "0123456789abcdef";
+		for( num in a ) {
+			var j = 7;
+			while( j >= 0 ) {
+				str += hex_chr.charAt( (num >>> (j<<2)) & 0xF );
+				j--;
+			}
+		}
+		return str;
+	}
+
+}