diff --git a/src/haxe/crypto/Hmac.hx b/src/haxe/crypto/Hmac.hx index 2e5c586..a7d5e10 100644 --- a/src/haxe/crypto/Hmac.hx +++ b/src/haxe/crypto/Hmac.hx @@ -44,8 +44,6 @@ class Hmac { var method:HashMethod; var blockSize:Int; var length:Int; - - public static var debug:Bool = false; public function new(hashMethod:HashMethod) { if (hashMethod != null) @@ -103,10 +101,7 @@ class Hmac { key = doHash(key); } key = nullPad(key, blockSize); - if (debug) { - trace("key: " + key.toHex()); - trace("msg: " + msg.toHex()); - } + var Ki = Bytes.alloc(key.length + msg.length); var Ko = Bytes.alloc(key.length + length); for (i in 0...key.length) { @@ -114,18 +109,8 @@ class Hmac { Ki.set(i, key.get(i) ^ 0x36); } // hash(Ko + hash(Ki + message)) - if (debug) { - trace("1) " + Ki.toHex()); - trace("1) " + Ko.toHex()); - } Ki.blit(key.length, msg, 0, msg.length); - if (debug) { - trace("2) " + Ki.toHex()); - } Ko.blit(key.length, doHash(Ki), 0, length); - if (debug) { - trace("2) " + Ko.toHex()); - } return doHash(Ko); } } diff --git a/src/haxe/crypto/SCrypt.hx b/src/haxe/crypto/SCrypt.hx index bcc2cbd..e0a9980 100644 --- a/src/haxe/crypto/SCrypt.hx +++ b/src/haxe/crypto/SCrypt.hx @@ -23,7 +23,7 @@ class SCrypt { * @param dkLen - output size * @return the generated key */ - public function hash(password:Bytes, salt:Bytes, N:Int, r:Int, p:Int, dkLen:Int):Bytes { + public function hash(password:Bytes, salt:Bytes, N:Int32, r:Int32, p:Int32, dkLen:Int):Bytes { if (password == null) throw "Password must not be null"; if (salt == null) @@ -35,18 +35,18 @@ class SCrypt { if (dkLen < 1) throw "dKlen must be > 0"; - var mflen:Int = 128 * r; - var mfwords:Int = mflen >>> 2; + var mflen:Int32 = 128 * r; + var mfwords:Int32 = mflen >>> 2; var data = pbkdf2.encode(password, salt, 1, p * mflen); - var b = new Vector(data.length >>> 2); + var b = new Vector(data.length >>> 2); for (i in 0...b.length) { b[i] = bytesToInt32(data, i * 4); } - var xbuf = new Vector(mfwords); - var vbuf = new Vector(N * mfwords); - var xtbuf = new Vector(16); + var xbuf = new Vector(mfwords); + var vbuf = new Vector(N * mfwords); + var xtbuf = new Vector(16); for (i in 0...p) { sMix(vbuf, b, i * mfwords, xbuf, xtbuf, mfwords, N, r); } @@ -59,13 +59,13 @@ class SCrypt { return pbkdf2.encode(password, output, 1, dkLen); } - private function sMix(vbuf:Vector, output:Vector, outputOffset:Int, xbuf:Vector, xtbuf:Vector, mfwords:Int, N:Int, r:Int):Void { + private function sMix(vbuf:Vector, output:Vector, outputOffset:Int32, xbuf:Vector, xtbuf:Vector, mfwords:Int32, N:Int32, r:Int32):Void { Vector.blit(output, outputOffset, vbuf, 0, mfwords); for (i in 1...N) { blockMix(vbuf, (i - 1) * mfwords, vbuf, i * mfwords, mfwords, r, xtbuf); } blockMix(vbuf, (N - 1) * mfwords, output, outputOffset, mfwords, r, xtbuf); - var j:Int = 0; + var j:Int32 = 0; for (i in 0...(N >> 1)) { j = (output.get(outputOffset + mfwords - 16) & (N - 1)) * mfwords; xor(output, outputOffset, vbuf, j, output, outputOffset, mfwords); @@ -76,9 +76,9 @@ class SCrypt { } } - private function blockMix(b:Vector, bOffset:Int, output:Vector, outputOffset:Int, mfwords:Int, r:Int, xtbuf:Vector):Void { + private function blockMix(b:Vector, bOffset:Int32, output:Vector, outputOffset:Int32, mfwords:Int32, r:Int32, xtbuf:Vector):Void { var x = b; - var offset = bOffset + mfwords - 16; + var offset:Int32 = bOffset + mfwords - 16; for (i in 0...(r << 1)) { xor(x, offset, b, bOffset + i * 16, xtbuf, 0, 16); offset = outputOffset + ((i & 1) * r + (i >> 1)) * 16; @@ -87,13 +87,13 @@ class SCrypt { } } - private static inline function xor(a:Vector, aOffset:Int, b:Vector, bOffset:Int, output:Vector, outputOffset:Int, outputLength:Int):Void { + private static inline function xor(a:Vector, aOffset:Int32, b:Vector, bOffset:Int32, output:Vector, outputOffset:Int32, outputLength:Int32):Void { for (i in 0...outputLength) { output.set(outputOffset + i, a.get(aOffset + i) ^ b.get(bOffset + i)); } } - public static inline function isPowerOf2(x:Int):Bool { + public static inline function isPowerOf2(x:Int32):Bool { return ((x & (x - 1)) == 0); } diff --git a/tests/src/unit/crypto/SCryptTest.hx b/tests/src/unit/crypto/SCryptTest.hx index eb97324..e33a15d 100644 --- a/tests/src/unit/crypto/SCryptTest.hx +++ b/tests/src/unit/crypto/SCryptTest.hx @@ -47,14 +47,12 @@ class SCryptTest extends Test { public function test():Void { trace("SCrypt with " + plainText.length + " keys"); - haxe.crypto.Hmac.debug = true; var time = Timer.stamp(); var scrypt:SCrypt = new SCrypt(); for (i in 0...plainText.length) { var data = scrypt.hash(Bytes.ofHex(plainText[i]), Bytes.ofHex(salts[i]), costParams[i], blockSizes[i], parallelizationParams[i], dkLen[i]); eq(data.toHex().toUpperCase(), derivedKeys[i]); } - haxe.crypto.Hmac.debug = false; time = Timer.stamp() - time; trace("Finished : " + time + " seconds"); }