Skip to content

Commit

Permalink
Remove debug info. Fix Scrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
flashultra committed Nov 11, 2023
1 parent bdeb1f8 commit 780d2d5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 31 deletions.
17 changes: 1 addition & 16 deletions src/haxe/crypto/Hmac.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -103,29 +101,16 @@ 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) {
Ko.set(i, key.get(i) ^ 0x5c);
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);
}
}
26 changes: 13 additions & 13 deletions src/haxe/crypto/SCrypt.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<Int>(data.length >>> 2);
var b = new Vector<Int32>(data.length >>> 2);
for (i in 0...b.length) {
b[i] = bytesToInt32(data, i * 4);
}

var xbuf = new Vector<Int>(mfwords);
var vbuf = new Vector<Int>(N * mfwords);
var xtbuf = new Vector<Int>(16);
var xbuf = new Vector<Int32>(mfwords);
var vbuf = new Vector<Int32>(N * mfwords);
var xtbuf = new Vector<Int32>(16);
for (i in 0...p) {
sMix(vbuf, b, i * mfwords, xbuf, xtbuf, mfwords, N, r);
}
Expand All @@ -59,13 +59,13 @@ class SCrypt {
return pbkdf2.encode(password, output, 1, dkLen);
}

private function sMix(vbuf:Vector<Int>, output:Vector<Int>, outputOffset:Int, xbuf:Vector<Int>, xtbuf:Vector<Int>, mfwords:Int, N:Int, r:Int):Void {
private function sMix(vbuf:Vector<Int32>, output:Vector<Int32>, outputOffset:Int32, xbuf:Vector<Int32>, xtbuf:Vector<Int32>, 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);
Expand All @@ -76,9 +76,9 @@ class SCrypt {
}
}

private function blockMix(b:Vector<Int>, bOffset:Int, output:Vector<Int>, outputOffset:Int, mfwords:Int, r:Int, xtbuf:Vector<Int>):Void {
private function blockMix(b:Vector<Int32>, bOffset:Int32, output:Vector<Int32>, outputOffset:Int32, mfwords:Int32, r:Int32, xtbuf:Vector<Int32>):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;
Expand All @@ -87,13 +87,13 @@ class SCrypt {
}
}

private static inline function xor(a:Vector<Int>, aOffset:Int, b:Vector<Int>, bOffset:Int, output:Vector<Int>, outputOffset:Int, outputLength:Int):Void {
private static inline function xor(a:Vector<Int32>, aOffset:Int32, b:Vector<Int32>, bOffset:Int32, output:Vector<Int32>, 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);
}

Expand Down
2 changes: 0 additions & 2 deletions tests/src/unit/crypto/SCryptTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down

0 comments on commit 780d2d5

Please sign in to comment.