@nogsantos/hash
Version:
Sha1 and Md5 hash encoder for javascript
208 lines (197 loc) • 7.42 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Sha1 = exports.Sha1 = function () {
function Sha1() {
_classCallCheck(this, Sha1);
}
_createClass(Sha1, [{
key: "_rawToHex",
value: function _rawToHex(raw) {
var hex = "";
var hexChars = "0123456789abcdef";
for (var i = 0; i < raw.length; i++) {
var c = raw.charCodeAt(i);
hex += hexChars.charAt(c >>> 4 & 0x0f) + hexChars.charAt(c & 0x0f);
}
return hex;
}
}, {
key: "_sha1Raw",
value: function _sha1Raw(raw) {
return this._binaryToRaw(this._sha1Binary(this.rawToBinary(raw), raw.length * 8));
}
}, {
key: "_binaryToRaw",
value: function _binaryToRaw(bin) {
var raw = "";
for (var i = 0, il = bin.length * 32; i < il; i += 8) {
raw += String.fromCharCode(bin[i >> 5] >>> 24 - i % 32 & 0xff);
}
return raw;
}
}, {
key: "_sha1Binary",
value: function _sha1Binary(bin, len) {
bin[len >> 5] |= 0x80 << 24 - len % 32;
bin[(len + 64 >> 9 << 4) + 15] = len;
var w = new Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for (var i = 0, il = bin.length; i < il; i += 16) {
var _a = a;
var _b = b;
var _c = c;
var _d = d;
var _e = e;
for (var j = 0; j < 80; j++) {
if (j < 16) {
w[j] = bin[i + j];
} else {
w[j] = this._rotateLeft(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
}
var t = this._add(this._add(this._rotateLeft(a, 5), this._ft(j, b, c, d)), this._add(this._add(e, w[j]), this._kt(j)));
e = d;
d = c;
c = this._rotateLeft(b, 30);
b = a;
a = t;
}
a = this._add(a, _a);
b = this._add(b, _b);
c = this._add(c, _c);
d = this._add(d, _d);
e = this._add(e, _e);
}
return [a, b, c, d, e];
}
}, {
key: "_add",
value: function _add(x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return msw << 16 | lsw & 0xFFFF;
}
}, {
key: "_rotateLeft",
value: function _rotateLeft(n, count) {
return n << count | n >>> 32 - count;
}
}, {
key: "_ft",
value: function _ft(t, b, c, d) {
if (t < 20) {
return b & c | ~b & d;
} else if (t < 40) {
return b ^ c ^ d;
} else if (t < 60) {
return b & c | b & d | c & d;
} else {
return b ^ c ^ d;
}
}
}, {
key: "_kt",
value: function _kt(t) {
if (t < 20) {
return 1518500249;
} else if (t < 40) {
return 1859775393;
} else if (t < 60) {
return -1894007588;
} else {
return -899497514;
}
}
}, {
key: "rawToBinary",
value: function rawToBinary(raw) {
var binary = new Array(raw.length >> 2);
for (var i = 0, il = binary.length; i < il; i++) {
binary[i] = 0;
}
for (i = 0, il = raw.length * 8; i < il; i += 8) {
binary[i >> 5] |= (raw.charCodeAt(i / 8) & 0xFF) << 24 - i % 32;
}
return binary;
}
}, {
key: "stringToRaw",
value: function stringToRaw(string) {
var raw = "",
x,
y;
var i = -1;
var il = string.length;
while (++i < il) {
x = string.charCodeAt(i);
y = i + 1 < il ? string.charCodeAt(i + 1) : 0;
if (0xd800 <= x && x <= 0xdbff && 0xdc00 <= y && y <= 0xdfff) {
x = 0x10000 + ((x & 0x03ff) << 10) + (y & 0x03ff);
++i;
}
if (x <= 0x7f) {
raw += String.fromCharCode(x);
} else if (x <= 0x7ff) {
raw += String.fromCharCode(0xc0 | x >>> 6 & 0x1f, 0x80 | x & 0x3f);
} else if (x <= 0xffff) {
raw += String.fromCharCode(0xe0 | x >>> 12 & 0x0f, 0x80 | x >>> 6 & 0x3f, 0x80 | x & 0x3f);
} else if (x <= 0x1fffff) {
raw += String.fromCharCode(0xf0 | x >>> 18 & 0x07, 0x80 | x >>> 12 & 0x3f, 0x80 | x >>> 6 & 0x3f, 0x80 | x & 0x3f);
}
}
return raw;
}
}, {
key: "hmacRaw",
value: function hmacRaw(key, data) {
var binaryKey = this.rawToBinary(key);
if (binaryKey.length > 16) {
binaryKey = this._sha1Binary(binaryKey, key.length * 8);
}
var ipad = new Array(16);
var opad = new Array(16);
for (var i = 0; i < 16; i++) {
ipad[i] = binaryKey[i] ^ 0x36363636;
opad[i] = binaryKey[i] ^ 0x5c5c5c5c;
}
var hash = this._sha1Binary(ipad.concat(this.rawToBinary(data)), 512 + data.length * 8);
return this._binaryToRaw(this._sha1Binary(opad.concat(hash), 512 + 160));
}
}, {
key: "encode",
value: function encode(string) {
if (!string) {
return null;
}
return this._rawToHex(this._sha1Raw(this.stringToRaw(string)));
}
}, {
key: "hexToString",
value: function hexToString(hex) {
if (!hex) {
return null;
}
var str = '';
for (var i = 0, il = hex.length; i < il; i += 2) {
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
}
return str;
}
}, {
key: "sha1ToHex",
value: function sha1ToHex(value) {
if (!value) {
return null;
}
return this._rawToHex(this._sha1Raw(this.hexToString(value)));
}
}]);
return Sha1;
}();