From 4b01cedb70ba633aad6d86ed83bc26522fa52dd4 Mon Sep 17 00:00:00 2001 From: evilaliv3 Date: Fri, 13 May 2016 23:01:30 +0200 Subject: [PATCH] Address main allocation issues https://github.com/dchest/scrypt-async-js/issues/25 --- scrypt-async.js | 86 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/scrypt-async.js b/scrypt-async.js index ac8a647..9cce9bb 100644 --- a/scrypt-async.js +++ b/scrypt-async.js @@ -32,26 +32,31 @@ function scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encoding) { 'use strict'; - function SHA256(m) { - /** @const */ var K = [ - 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, - 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, - 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, - 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, - 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, - 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, - 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, - 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, - 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, - 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, - 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, - 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, - 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 - ]; + var w = new Array(64); + + var outerKey = new Array(64); + + /** @const */ var K = [ + 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, + 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, + 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, + 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, + 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, + 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, + 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, + 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, + 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, + 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, + 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, + 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, + 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 + ]; + + var sha_result = new Array(32); + function SHA256(m) { var h0 = 0x6a09e667, h1 = 0xbb67ae85, h2 = 0x3c6ef372, h3 = 0xa54ff53a, - h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19, - w = new Array(64); + h4 = 0x510e527f, h5 = 0x9b05688c, h6 = 0x1f83d9ab, h7 = 0x5be0cd19; function blocks(p) { var off = 0, len = p.length; @@ -128,16 +133,40 @@ function scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encodin blocks(p); - return [ - (h0>>>24) & 0xff, (h0>>>16) & 0xff, (h0>>>8) & 0xff, (h0>>>0) & 0xff, - (h1>>>24) & 0xff, (h1>>>16) & 0xff, (h1>>>8) & 0xff, (h1>>>0) & 0xff, - (h2>>>24) & 0xff, (h2>>>16) & 0xff, (h2>>>8) & 0xff, (h2>>>0) & 0xff, - (h3>>>24) & 0xff, (h3>>>16) & 0xff, (h3>>>8) & 0xff, (h3>>>0) & 0xff, - (h4>>>24) & 0xff, (h4>>>16) & 0xff, (h4>>>8) & 0xff, (h4>>>0) & 0xff, - (h5>>>24) & 0xff, (h5>>>16) & 0xff, (h5>>>8) & 0xff, (h5>>>0) & 0xff, - (h6>>>24) & 0xff, (h6>>>16) & 0xff, (h6>>>8) & 0xff, (h6>>>0) & 0xff, - (h7>>>24) & 0xff, (h7>>>16) & 0xff, (h7>>>8) & 0xff, (h7>>>0) & 0xff - ]; + sha_result[0] = (h0>>>24) & 0xff; + sha_result[1] = (h0>>>16) & 0xff; + sha_result[2] = (h0>>>8) & 0xff; + sha_result[3] = (h0>>>0) & 0xff; + sha_result[4] = (h1>>>24) & 0xff; + sha_result[5] = (h1>>>16) & 0xff; + sha_result[6] = (h1>>>8) & 0xff; + sha_result[7] = (h1>>>0) & 0xff; + sha_result[8] = (h2>>>24) & 0xff; + sha_result[9] = (h2>>>16) & 0xff; + sha_result[10] = (h2>>>8) & 0xff; + sha_result[11] = (h2>>>0) & 0xff; + sha_result[12] = (h3>>>24) & 0xff; + sha_result[13] = (h3>>>16) & 0xff; + sha_result[14] = (h3>>>8) & 0xff; + sha_result[15] = (h3>>>0) & 0xff; + sha_result[16] = (h4>>>24) & 0xff; + sha_result[17] = (h4>>>16) & 0xff; + sha_result[18] = (h4>>>8) & 0xff; + sha_result[19] = (h4>>>0) & 0xff; + sha_result[20] = (h5>>>24) & 0xff; + sha_result[21] = (h5>>>16) & 0xff; + sha_result[22] = (h5>>>8) & 0xff; + sha_result[23] = (h5>>>0) & 0xff; + sha_result[24] = (h6>>>24) & 0xff; + sha_result[25] = (h6>>>16) & 0xff; + sha_result[26] = (h6>>>8) & 0xff; + sha_result[27] = (h6>>>0) & 0xff; + sha_result[28] = (h7>>>24) & 0xff; + sha_result[29] = (h7>>>16) & 0xff; + sha_result[30] = (h7>>>8) & 0xff; + sha_result[31] = (h7>>>0) & 0xff; + + return sha_result; } function PBKDF2_HMAC_SHA256_OneIter(password, salt, dkLen) { @@ -146,7 +175,6 @@ function scrypt(password, salt, logN, r, dkLen, interruptStep, callback, encodin var i, innerLen = 64 + salt.length + 4, inner = new Array(innerLen), - outerKey = new Array(64), dk = []; // inner = (password ^ ipad) || salt || counter