(function() { var c = cryptojs; var c_lib = c.lib; var wordarray = c_lib.wordarray; var hasher = c_lib.hasher; var c_algo = c.algo; var w = []; var sm3 = c_algo.sm3 = hasher.extend({ _doreset: function() { //this._hash = new wordarray.init([0x7380166f, 0x4914b2b9, 0x172442d7, 0xda8a0600, 0xa96f30bc, 0x163138aa, 0xe38dee4d, 0xb0fb0e4e]); this._hash = new wordarray.init([1937774191, 1226093241, 388252375, -628488704, -1452330820, 372324522, -477237683, -1325724082]); }, _doprocessblock: function(m, offset) { var h = this._hash.words; var a = h[0]; var b = h[1]; var c = h[2]; var d = h[3]; var e = h[4]; for (var i = 0; i < 80; i++) { if (i < 16) { w[i] = m[offset + i] | 0 } else { var n = w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]; w[i] = (n << 1) | (n >>> 31) } var t = ((a << 5) | (a >>> 27)) + e + w[i]; if (i < 20) { t += ((b & c) | (~b & d)) + 0x5a827999 } else if (i < 40) { t += (b ^ c ^ d) + 0x6ed9eba1 } else if (i < 60) { t += ((b & c) | (b & d) | (c & d)) - 0x70e44324 } else { t += (b ^ c ^ d) - 0x359d3e2a } e = d; d = c; c = (b << 30) | (b >>> 2); b = a; a = t } h[0] = (h[0] + a) | 0; h[1] = (h[1] + b) | 0; h[2] = (h[2] + c) | 0; h[3] = (h[3] + d) | 0; h[4] = (h[4] + e) | 0 }, _dofinalize: function() { var data = this._data; var datawords = data.words; var nbitstotal = this._ndatabytes * 8; var nbitsleft = data.sigbytes * 8; datawords[nbitsleft >>> 5] |= 0x80 << (24 - nbitsleft % 32); datawords[(((nbitsleft + 64) >>> 9) << 4) + 14] = math.floor(nbitstotal / 0x100000000); datawords[(((nbitsleft + 64) >>> 9) << 4) + 15] = nbitstotal; data.sigbytes = datawords.length * 4; this._process(); return this._hash }, clone: function() { var clone = hasher.clone.call(this); clone._hash = this._hash.clone(); return clone } }); c.sm3 = hasher._createhelper(sm3); c.hmacsm3 = hasher._createhmachelper(sm3) } ()); function sm3digest() { this.byte_length = 64; this.xbuf = new array(); this.xbufoff = 0; this.bytecount = 0; this.digest_length = 32; //this.v0 = [0x7380166f,0x4914b2b9,0x172442d7,0xda8a0600,0xa96f30bc,0x163138aa,0xe38dee4d,0xb0fb0e4e]; // this.v0 = [0x7380166f, 0x4914b2b9, 0x172442d7,0xda8a0600,0xa96f30bc,0x163138aa,0xe38dee4d,0xb0fb0e4e]; // this.v0 = [0x7380166f, 0x4914b2b9, 0x172442d7, -628488704, -1452330820, 0x163138aa, -477237683, -1325724082]; this.v0 = [1937774191, 1226093241, 388252375, -628488704, -1452330820, 372324522, -477237683, -1325724082]; this.v = new array(8); this.v_ = new array(8); this.x0 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; this.x = new array(68); this.xoff = 0; this.t_00_15 = 0x79cc4519; this.t_16_63 = 0x7a879d8a; if (arguments.length > 0) { this.initdigest(arguments[0]) } else { this.init() } } sm3digest.prototype = { init: function() { this.xbuf = new array(4); this.reset() }, initkey:function(key) { this.v0=key; this.xbuf = new array(4); this.reset() }, initdigest: function(t) { this.xbuf = new array(t.xbuf.length); array.copy(t.xbuf, 0, this.xbuf, 0, t.xbuf.length); this.xbufoff = t.xbufoff; this.bytecount = t.bytecount; array.copy(t.x, 0, this.x, 0, t.x.length); this.xoff = t.xoff; array.copy(t.v, 0, this.v, 0, t.v.length) }, getdigestsize: function() { return this.digest_length }, reset: function() { this.bytecount = 0; this.xbufoff = 0; array.clear(this.xbuf, 0, this.xbuf.length); array.copy(this.v0, 0, this.v, 0, this.v0.length); this.xoff = 0; array.copy(this.x0, 0, this.x, 0, this.x0.length) }, getbytelength: function() { return this.byte_length }, processblock: function() { var i; var ww = this.x; var ww_ = new array(64); for (i = 16; i < 68; i++) { ww[i] = this.p1(ww[i - 16] ^ ww[i - 9] ^ (this.rotate(ww[i - 3], 15))) ^ (this.rotate(ww[i - 13], 7)) ^ ww[i - 6] } for (i = 0; i < 64; i++) { ww_[i] = ww[i] ^ ww[i + 4] } var vv = this.v; var vv_ = this.v_; array.copy(vv, 0, vv_, 0, this.v0.length); var ss1, ss2, tt1, tt2, aaa; for (i = 0; i < 16; i++) { aaa = this.rotate(vv_[0], 12); ss1 = int32.parse(int32.parse(aaa + vv_[4]) + this.rotate(this.t_00_15, i)); ss1 = this.rotate(ss1, 7); ss2 = ss1 ^ aaa; tt1 = int32.parse(int32.parse(this.ff_00_15(vv_[0], vv_[1], vv_[2]) + vv_[3]) + ss2) + ww_[i]; tt2 = int32.parse(int32.parse(this.gg_00_15(vv_[4], vv_[5], vv_[6]) + vv_[7]) + ss1) + ww[i]; vv_[3] = vv_[2]; vv_[2] = this.rotate(vv_[1], 9); vv_[1] = vv_[0]; vv_[0] = tt1; vv_[7] = vv_[6]; vv_[6] = this.rotate(vv_[5], 19); vv_[5] = vv_[4]; vv_[4] = this.p0(tt2) } for (i = 16; i < 64; i++) { aaa = this.rotate(vv_[0], 12); ss1 = int32.parse(int32.parse(aaa + vv_[4]) + this.rotate(this.t_16_63, i)); ss1 = this.rotate(ss1, 7); ss2 = ss1 ^ aaa; tt1 = int32.parse(int32.parse(this.ff_16_63(vv_[0], vv_[1], vv_[2]) + vv_[3]) + ss2) + ww_[i]; tt2 = int32.parse(int32.parse(this.gg_16_63(vv_[4], vv_[5], vv_[6]) + vv_[7]) + ss1) + ww[i]; vv_[3] = vv_[2]; vv_[2] = this.rotate(vv_[1], 9); vv_[1] = vv_[0]; vv_[0] = tt1; vv_[7] = vv_[6]; vv_[6] = this.rotate(vv_[5], 19); vv_[5] = vv_[4]; vv_[4] = this.p0(tt2) } for (i = 0; i < 8; i++) { vv[i] ^= int32.parse(vv_[i]) } this.xoff = 0; array.copy(this.x0, 0, this.x, 0, this.x0.length) }, processword: function(in_renamed, inoff) { var n = in_renamed[inoff] << 24; n |= (in_renamed[++inoff] & 0xff) << 16; n |= (in_renamed[++inoff] & 0xff) << 8; n |= (in_renamed[++inoff] & 0xff); this.x[this.xoff] = n; if (++this.xoff == 16) { this.processblock() } }, processlength: function(bitlength) { if (this.xoff > 14) { this.processblock() } this.x[14] = (this.urshiftlong(bitlength, 32)); this.x[15] = (bitlength & (0xffffffff)) }, inttobigendian: function(n, bs, off) { bs[off] = int32.parsebyte(this.urshift(n, 24)); bs[++off] = int32.parsebyte(this.urshift(n, 16)); bs[++off] = int32.parsebyte(this.urshift(n, 8)); bs[++off] = int32.parsebyte(n) }, dofinal: function(out_renamed, outoff) { this.finish(); for (var i = 0; i < 8; i++) { this.inttobigendian(this.v[i], out_renamed, outoff + i * 4) } this.reset(); return this.digest_length }, update: function(input) { this.xbuf[this.xbufoff++] = input; if (this.xbufoff == this.xbuf.length) { this.processword(this.xbuf, 0); this.xbufoff = 0 } this.bytecount++ }, blockupdate: function(input, inoff, length) { while ((this.xbufoff != 0) && (length > 0)) { this.update(input[inoff]); inoff++; length-- } while (length > this.xbuf.length) { this.processword(input, inoff); inoff += this.xbuf.length; length -= this.xbuf.length; this.bytecount += this.xbuf.length } while (length > 0) { this.update(input[inoff]); inoff++; length-- } }, finish: function() { var bitlength = (this.bytecount << 3); this.update((128)); while (this.xbufoff != 0) this.update((0)); this.processlength(bitlength); this.processblock() }, rotate: function(x, n) { return (x << n) | (this.urshift(x, (32 - n))) }, p0: function(x) { return ((x) ^ this.rotate((x), 9) ^ this.rotate((x), 17)) }, p1: function(x) { return ((x) ^ this.rotate((x), 15) ^ this.rotate((x), 23)) }, ff_00_15: function(x, y, z) { return (x ^ y ^ z) }, ff_16_63: function(x, y, z) { return ((x & y) | (x & z) | (y & z)) }, gg_00_15: function(x, y, z) { return (x ^ y ^ z) }, gg_16_63: function(x, y, z) { return ((x & y) | (~x & z)) }, urshift: function(number, bits) { if (number > int32.maxvalue || number < int32.minvalue) { number = int32.parse(number) } if (number >= 0) { return number >> bits } else { return (number >> bits) + (2 << ~bits) } }, urshiftlong: function(number, bits) { var returnv; var big = new biginteger(); big.fromint(number); if (big.signum() >= 0) { returnv = big.shiftright(bits).intvalue() } else { var bigadd = new biginteger(); bigadd.fromint(2); var shiftleftbits = ~bits; var shiftleftnumber = ''; if (shiftleftbits < 0) { var shiftrightbits = 64 + shiftleftbits; for (var i = 0; i < shiftrightbits; i++) { shiftleftnumber += '0' } var shiftleftnumberbigadd = new biginteger(); shiftleftnumberbigadd.fromint(number >> bits); var shiftleftnumberbig = new biginteger("10" + shiftleftnumber, 2); shiftleftnumber = shiftleftnumberbig.toradix(10); var r = shiftleftnumberbig.add(shiftleftnumberbigadd); returnv = r.toradix(10) } else { shiftleftnumber = bigadd.shiftleft((~bits)).intvalue(); returnv = (number >> bits) + shiftleftnumber } } return returnv }, getz: function(g, pubkeyhex) { var userid = cryptojs.enc.utf8.parse("1234567812345678"); var len = userid.words.length * 4 * 8; this.update((len >> 8 & 0x00ff)); this.update((len & 0x00ff)); var useridwords = this.getwords(userid.tostring()); this.blockupdate(useridwords, 0, useridwords.length); var awords = this.getwords(g.curve.a.tobiginteger().toradix(16)); var bwords = this.getwords(g.curve.b.tobiginteger().toradix(16)); var gxwords = this.getwords(g.getx().tobiginteger().toradix(16)); var gywords = this.getwords(g.gety().tobiginteger().toradix(16)); var pxwords = this.getwords(pubkeyhex.substr(0, 64)); var pywords = this.getwords(pubkeyhex.substr(64, 64)); this.blockupdate(awords, 0, awords.length); this.blockupdate(bwords, 0, bwords.length); this.blockupdate(gxwords, 0, gxwords.length); this.blockupdate(gywords, 0, gywords.length); this.blockupdate(pxwords, 0, pxwords.length); this.blockupdate(pywords, 0, pywords.length); var md = new array(this.getdigestsize()); this.dofinal(md, 0); return md }, getwords: function(hexstr) { var words = []; var hexstrlength = hexstr.length; for (var i = 0; i < hexstrlength; i += 2) { words[words.length] = parseint(hexstr.substr(i, 2), 16) } return words }, gethex: function(arr) { var words = []; var j = 0; for (var i = 0; i < arr.length * 2; i += 2) { words[i >>> 3] |= parseint(arr[j]) << (24 - (i % 8) * 4); j++ } var wordarray = new cryptojs.lib.wordarray.init(words, arr.length); return wordarray } }; array.clear = function(destinationarray, destinationindex, length) { for (elm in destinationarray) { destinationarray[elm] = null } }; array.copy = function(sourcearray, sourceindex, destinationarray, destinationindex, length) { var clonearray = sourcearray.slice(sourceindex, sourceindex + length); for (var i = 0; i < clonearray.length; i++) { destinationarray[destinationindex] = clonearray[i]; destinationindex++ } }; window.int32 = { minvalue: -parseint('10000000000000000000000000000000', 2), maxvalue: parseint('1111111111111111111111111111111', 2), parse: function(n) { if (n < this.minvalue) { var biginteger = new number( - n); var bigintegerradix = biginteger.tostring(2); var subbigintegerradix = bigintegerradix.substr(bigintegerradix.length - 31, 31); var rebigintegerradix = ''; for (var i = 0; i < subbigintegerradix.length; i++) { var subbigintegerradixitem = subbigintegerradix.substr(i, 1); rebigintegerradix += subbigintegerradixitem == '0' ? '1': '0' } var result = parseint(rebigintegerradix, 2); return (result + 1) } else if (n > this.maxvalue) { var biginteger = number(n); var bigintegerradix = biginteger.tostring(2); var subbigintegerradix = bigintegerradix.substr(bigintegerradix.length - 31, 31); var rebigintegerradix = ''; for (var i = 0; i < subbigintegerradix.length; i++) { var subbigintegerradixitem = subbigintegerradix.substr(i, 1); rebigintegerradix += subbigintegerradixitem == '0' ? '1': '0' } var result = parseint(rebigintegerradix, 2); return - (result + 1) } else { return n } }, parsebyte: function(n) { if (n < 0) { var biginteger = new number( - n); var bigintegerradix = biginteger.tostring(2); var subbigintegerradix = bigintegerradix.substr(bigintegerradix.length - 8, 8); var rebigintegerradix = ''; for (var i = 0; i < subbigintegerradix.length; i++) { var subbigintegerradixitem = subbigintegerradix.substr(i, 1); rebigintegerradix += subbigintegerradixitem == '0' ? '1': '0' } var result = parseint(rebigintegerradix, 2); return (result + 1) } else if (n > 255) { var biginteger = number(n); var bigintegerradix = biginteger.tostring(2); return parseint(bigintegerradix.substr(bigintegerradix.length - 8, 8), 2) } else { return n } } };