我只是为了练习而为javascript实现了一个sha256哈希函数,我想知道我的代码是否需要更多的改进。如果有的话,请告诉我哪一个可以简化。谢谢。
这是我的代码:
var sha256TOOLS = {};
//sha256 constant
sha256TOOLS.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];
//sha256 intial hashes
sha256TOOLS.h = [0x6a09e667,
0xbb67ae85,
0x3c6ef372,
0xa54ff53a,
0x510e527f,
0x9b05688c,
0x1f83d9ab,
0x5be0cd19];
sha256TOOLS.addFrontZero = function addFrontZero(str, fixedlength){
while(str.length < fixedlength){
str = 0 + str;
}
return str;
};
sha256TOOLS.rotr = function rotr(n,x){
return (((x >>> n) | (x << 32 - n)) >>> 0) % Math.pow(2,32);
};
sha256TOOLS.ch = function ch(x,y,z){
return (x & y) ^ (~x & z);
};
sha256TOOLS.maj = function maj(x,y,z){
return (x & y) ^ (x & z) ^ (y & z);
};
sha256TOOLS.sigma0 = function sigma0(x){
return this.rotr(2,x) ^ this.rotr(13,x) ^ this.rotr(22,x);
};
sha256TOOLS.sigma1 = function sigma1(x){
return this.rotr(6,x) ^ this.rotr(11,x) ^ this.rotr(25,x);
};
sha256TOOLS.omega0 = function omega0(x){
return this.rotr(7,x) ^ this.rotr(18,x) ^ (x >>> 3);
};
sha256TOOLS.omega1 = function omega1(x){
return this.rotr(17,x) ^ this.rotr(19,x) ^ (x >>> 10);
};
sha256TOOLS.mod = function mod(a,b){
var temp = a % b;
while(temp < 0){
temp += b;
}
return temp;
};
var sha256 = (sha256 || function sha256(string){
//copy constant values and initial hashes
var H = sha256TOOLS.h.slice();
var K = sha256TOOLS.k.slice();
//sha256 pre processing
//convert string to ascii code first then to binary
//////////////////////////////////////////////////////
var str =[];
for(var ia=0; ia<string.length; ia++){
var ta = (string.charCodeAt(ia)).toString(2);
ta = (ta.length < 8) ? sha256TOOLS.addFrontZero(ta,8) : ta;
str.push(ta);
}
//turn str into string
str = str.join("");
//////////////////////////////////////////////////////
//Padding the message
var zeroBits = sha256TOOLS.addFrontZero("",sha256TOOLS.mod(448-(str.length+1), 512));
var lengthBits = sha256TOOLS.addFrontZero((str.length).toString(2), 64);
str = str + "1" + zeroBits + lengthBits;
//////////////////////////////////////////////////////
//checking length off message
if(str.length > Math.pow(2,64)){
throw "message length greater than 2 ** 64";
}
//////////////////////////////////////////////////////
//parsing the message M into N 512 bit block
//////////////////////////////////////////////////////
var M = [];
for(var ib=0; ib<str.length; ib+=512){
var tempa = [];
for(var j=0; j < 512; j+=32){
tempa.push(str.substr(ib+j,32));
}
M.push(tempa);
}
//////////////////////////////////////////////////////
//main loop goes here!
var a, b, c, d, e, f, g, h, t1, t2;
for(var i=0; i<M.length; i++){
//Message schedule have length of 64
var W = [];
var temp;
//prepare for message diggest,, compression etc...
//////////////////////////////////////////////////////
for(var tb=0; tb<64; tb++){
if(tb < 16){
W.push(parseInt(M[i][tb], 2) % Math.pow(2,32));
}else{
temp = (sha256TOOLS.omega1(W[tb-2]) + W[tb-7] + sha256TOOLS.omega0(W[tb-15]) + W[tb-16]) % Math.pow(2,32);
W.push(temp);
}
}
//////////////////////////////////////////////////////
//real computation between
//ints mod 2 ** 32
//////////////////////////////////////////////////////
a = H[0];
b = H[1];
c = H[2];
d = H[3];
e = H[4];
f = H[5];
g = H[6];
h = H[7];
for(var t=0; t<64; t++){
t1 = (h + sha256TOOLS.sigma1(e) + sha256TOOLS.ch(e,f,g) + K[t] + W[t]) % Math.pow(2,32);
t2 = (sha256TOOLS.sigma0(a) + sha256TOOLS.maj(a,b,c)) % Math.pow(2,32);
h = g;
g = f;
f = e;
e = (d + t1) % Math.pow(2,32);
d = c;
c = b;
b = a;
a = (t1 + t2) % Math.pow(2,32);
}
H[0] = (a + H[0]) % Math.pow(2,32);
H[1] = (b + H[1]) % Math.pow(2,32);
H[2] = (c + H[2]) % Math.pow(2,32);
H[3] = (d + H[3]) % Math.pow(2,32);
H[4] = (e + H[4]) % Math.pow(2,32);
H[5] = (f + H[5]) % Math.pow(2,32);
H[6] = (g + H[6]) % Math.pow(2,32);
H[7] = (h + H[7]) % Math.pow(2,32);
}
//////////////////////////////////////////////////////
var output = "";
for(var ic=0; ic<8; ic++){
var tempb = (H[ic] >>> 0).toString(16);
output += (sha256TOOLS.addFrontZero(tempb, 8));
}
return output;
});发布于 2019-12-28 03:44:07
JavaScript字符串是Unicode。您的代码假定字符串中的字符小于256。如果一个字符超过256个,则产生的二进制编码字符串将是错误的长度,散列将失败。
DataView没有必要将数字转换为0和1的字符串。这种转换是一种巨大的CPU开销,占用RAM (每个字符16字节),还有许多额外的代码从该字符串转换而来。
使用类型化数组和DataView将字符串打包为32位整数。除了Uint8ClampedArray之外,数组在编写时将自动掩盖数字,因此不需要执行所有% (2 ** 32)操作。
JS中的最大字符串长度是2^{53},您所做的检查有点过分,因为仅仅为了构建一个大小的字符串,2^{64}需要花费很多年的时间在一台高端机器上。
String.padStart填充带有零的字符串。eg binStr = number.toString(2).padStart(32, "0");**而不是Math.pow。eg Math.pow(2, 32) === 2 ** 32const int32Mod = 2 ** 32来保持模块,而不是每次计算它。我从零开始,使用类型化数组和一些其他方法来加快速度。重写将在大约1/15的时间内创建一个散列,并且使用的内存要少得多。
我没有使用类语法来创建工具(这是不安全的),而是通过闭包封装了工具和散列函数,以防止截获要散列的任何数据。
使用Uint8Array的函数将屏蔽掉前8位Javascripts字符串字符。修改将很简单,以便允许哈希使用Uint16Array处理每个字符的完整16位。
const sha256 = (() => {
const stringFillArray = (str, arr, i = 0) => { while(i < str.length) { arr[i] = str.charCodeAt(i++) } }
const H = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
const 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];
const ch = (x, y, z) => (x & y) ^ (~x & z);
const maj = (x, y, z) => (x & y) ^ (x & z) ^ (y & z);
const sigma0 = x => (((x >>> 2) | (x << 30)) ^ ((x >>> 13) | (x << 19)) ^ ((x >>> 22) | (x << 10)));
const sigma1 = x => (((x >>> 6) | (x << 26)) ^ ((x >>> 11) | (x << 21)) ^ ((x >>> 25) | (x << 7)));
const omega0 = x => (((x >>> 7) | (x << 25)) ^ ((x >>> 18) | (x << 14)) ^ (x >>> 3));
const omega1 = x => (((x >>> 17) | (x << 15)) ^ ((x >>> 19) | (x << 13)) ^ (x >>> 10));
const buf32 = new Array(64);
const hTemp = new Int32Array(8);
const totals = new Int32Array(2);
const o1 = omega0, o2 = omega1, s1 = sigma0, s2 = sigma1, t = hTemp, b = buf32; // Aliases
const hashIt = string => {
var i = 0, j, result = [];
const hashed = new Uint32Array(H);
const packChunk = i => o2(b[i - 2]) + b[i - 7] + o1(b[i - 15]) + b[i - 16];
const hashVals = (i = 0) => {
while (i < 64) {
totals[0] = t[7] + s2(t[4]) + ch(t[4], t[5], t[6]) + K[i] + b[i++];
totals[1] = s1(t[0]) + maj(t[0], t[1], t[2]);
t[7] = t[6];
t[6] = t[5];
t[5] = t[4];
t[4] = t[3] + totals[0];
t[3] = t[2];
t[2] = t[1];
t[1] = t[0];
t[0] = totals[0] + totals[1];
}
};
const sumVals = (i = 0) => { while (i < 8) { hashed[i] = t[i] + hashed[i++] } };
const stringBuf = new ArrayBuffer(((string.length / 64 | 0) + 1) * 64);
const stringView = new DataView(stringBuf);
const bytes = new Uint8Array(stringBuf);
const words = new Int32Array(stringBuf);
stringFillArray(string, bytes);
bytes[string.length] = 0x80;
stringView.setUint32(bytes.length - 4, string.length * 8);
while (i < words.length) {
j = 0;
while (j < 16) { buf32[j] = stringView.getInt32((i + (j++)) * 4) }
while (j < 64) { buf32[j] = packChunk(j++) }
hTemp.set(hashed);
hashVals();
sumVals();
i += 16;
}
i = 0;
while (i < 8) { result[i] = hashed[i++].toString(16).padStart(8, "0") }
return result.join("");
};
return str => hashIt(str);
})();https://codereview.stackexchange.com/questions/234662
复制相似问题