Refactor src/crypto/hash/*.js to use import & export

This commit is contained in:
Tankred Hase 2016-02-05 09:30:24 +07:00
parent 19a97bf117
commit 3aed324d51
4 changed files with 1593 additions and 1596 deletions

View File

@ -8,13 +8,14 @@
'use strict';
var sha = require('./sha.js'),
asmCrypto = require('asmcrypto-lite'),
Rusha = require('rusha'),
rusha = new Rusha(),
md5 = require('./md5.js'),
ripemd = require('./ripe-md.js'),
util = require('../../util.js'),
import sha from './sha.js';
import asmCrypto from 'asmcrypto-lite';
import Rusha from 'rusha';
import md5 from './md5.js';
import ripemd from './ripe-md.js';
import util from '../../util.js';
const rusha = new Rusha(),
nodeCrypto = util.getNodeCrypto(),
Buffer = util.getNodeBuffer();

View File

@ -19,17 +19,17 @@
'use strict';
var util = require('../../util.js');
import util from '../../util.js';
/**
* MD5 hash
* @param {String} entree string to hash
*/
module.exports = function (entree) {
export default function(entree) {
var hex = md5(util.Uint8Array2str(entree));
var bin = util.str2Uint8Array(util.hex2bin(hex));
return bin;
};
}
function md5cycle(x, k) {
var a = x[0],

View File

@ -28,7 +28,7 @@
* @module crypto/hash/ripe-md
*/
var util = require('../../util.js');
import util from '../../util.js';
var RMDsize = 160;
var X = [];
@ -289,7 +289,7 @@ function RMD(message) {
}
function RMDstring(message) {
export default function RMDstring(message) {
var hashcode = RMD(util.Uint8Array2str(message));
var retString = "";
@ -299,5 +299,3 @@ function RMDstring(message) {
return util.str2Uint8Array(retString);
}
module.exports = RMDstring;

View File

@ -19,12 +19,11 @@
* 1 = SHA-1, 2 = SHA-224/SHA-256, 4 = SHA-384/SHA-512
*/
"use strict";
var SUPPORTED_ALGS = 4 | 2 | 1;
(function (global)
{
"use strict";
/**
/**
* Int_64 is a object for 2 32-bit numbers emulating a 64-bit number
*
* @private
@ -33,13 +32,13 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {number} msint_32 The most significant 32-bits of a 64-bit number
* @param {number} lsint_32 The least significant 32-bits of a 64-bit number
*/
function Int_64(msint_32, lsint_32)
{
function Int_64(msint_32, lsint_32)
{
this.highOrder = msint_32;
this.lowOrder = lsint_32;
}
}
/**
/**
* Convert a string to an array of big-endian words
*
* @private
@ -50,8 +49,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
function str2binb(str, utfType)
{
function str2binb(str, utfType)
{
var bin = [], codePnt, binArr = [], byteCnt = 0, i, j, offset;
if ("UTF8" === utfType)
@ -123,9 +122,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
}
return {"value" : bin, "binLen" : byteCnt * 8};
}
}
/**
/**
* Convert a hex string to an array of big-endian words
*
* @private
@ -134,8 +133,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
function hex2binb(str)
{
function hex2binb(str)
{
var bin = [], length = str.length, i, num, offset;
if (0 !== (length % 2))
@ -162,9 +161,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
return {"value" : bin, "binLen" : length * 4};
}
}
/**
/**
* Convert a string of raw bytes to an array of big-endian words
*
* @private
@ -173,8 +172,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
function bytes2binb(str)
{
function bytes2binb(str)
{
var bin = [], codePnt, i, offset;
for (i = 0; i < str.length; i += 1)
@ -190,9 +189,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
return {"value" : bin, "binLen" : str.length * 8};
}
}
/**
/**
* Convert a Uint8Array of raw bytes to an array of big-endian 32-bit words
*
* @private
@ -201,8 +200,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* "value" contains the output array and "binLen" is the binary
* length of "value"
*/
function typed2binb(array)
{
function typed2binb(array)
{
var bin = [], octet, i, offset;
@ -219,9 +218,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
return {"value" : bin, "binLen" : array.length * 8};
}
}
/**
/**
* Convert a base-64 string to an array of big-endian words
*
* @private
@ -230,8 +229,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* "value" contains the output number array and "binLen" is the binary
* length of "value"
*/
function b642binb(str)
{
function b642binb(str)
{
var retVal = [], byteCnt = 0, index, i, j, tmpInt, strPart, firstEqual, offset,
b64Tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@ -271,9 +270,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
return {"value" : retVal, "binLen" : byteCnt * 8};
}
}
/**
/**
* Convert an array of big-endian words to a hex string.
*
* @private
@ -284,8 +283,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @return {string} Hexidecimal representation of the parameter in string
* form
*/
function binb2hex(binarray, formatOpts)
{
function binb2hex(binarray, formatOpts)
{
var hex_tab = "0123456789abcdef", str = "",
length = binarray.length * 4, i, srcByte;
@ -298,9 +297,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
return (formatOpts["outputUpper"]) ? str.toUpperCase() : str;
}
}
/**
/**
* Convert an array of big-endian words to a base-64 string
*
* @private
@ -311,8 +310,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @return {string} Base-64 encoded representation of the parameter in
* string form
*/
function binb2b64(binarray, formatOpts)
{
function binb2b64(binarray, formatOpts)
{
var str = "", length = binarray.length * 4, i, j, triplet, offset, int1, int2,
b64Tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@ -338,9 +337,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
}
return str;
}
}
/**
/**
* Convert an array of big-endian words to raw bytes string
*
* @private
@ -350,8 +349,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @return {string} Raw bytes representation of the parameter in string
* form
*/
function binb2bytes(binarray, formatOpts)
{
function binb2bytes(binarray, formatOpts)
{
var str = "", length = binarray.length * 4, i, srcByte;
for (i = 0; i < length; i += 1)
@ -361,9 +360,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
return str;
}
}
/**
/**
* Convert an array of big-endian words to raw bytes Uint8Array
*
* @private
@ -372,8 +371,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {!Object} formatOpts Unused Hash list
* @return {Uint8Array} Raw bytes representation of the parameter
*/
function binb2typed(binarray, formatOpts)
{
function binb2typed(binarray, formatOpts)
{
var length = binarray.length * 4;
var arr = new Uint8Array(length), i;
@ -383,9 +382,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
return arr;
}
}
/**
/**
* Validate hash list containing output formatting options, ensuring
* presence of every option or adding the default value
*
@ -395,8 +394,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @return {{outputUpper : boolean, b64Pad : string}} Validated hash list
* containing output formatting options
*/
function getOutputOpts(outputOpts)
{
function getOutputOpts(outputOpts)
{
var retVal = {"outputUpper" : false, "b64Pad" : "="};
try
@ -425,9 +424,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
return retVal;
}
}
/**
/**
* The 32-bit implementation of circular rotate left
*
* @private
@ -435,12 +434,12 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {number} n The number of bits to shift
* @return {number} The x shifted circularly by n bits
*/
function rotl_32(x, n)
{
function rotl_32(x, n)
{
return (x << n) | (x >>> (32 - n));
}
}
/**
/**
* The 32-bit implementation of circular rotate right
*
* @private
@ -448,12 +447,12 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {number} n The number of bits to shift
* @return {number} The x shifted circularly by n bits
*/
function rotr_32(x, n)
{
function rotr_32(x, n)
{
return (x >>> n) | (x << (32 - n));
}
}
/**
/**
* The 64-bit implementation of circular rotate right
*
* @private
@ -461,8 +460,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {number} n The number of bits to shift
* @return {Int_64} The x shifted circularly by n bits
*/
function rotr_64(x, n)
{
function rotr_64(x, n)
{
var retVal = null, tmp = new Int_64(x.highOrder, x.lowOrder);
if (32 >= n)
@ -481,9 +480,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
return retVal;
}
}
/**
/**
* The 32-bit implementation of shift right
*
* @private
@ -491,12 +490,12 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {number} n The number of bits to shift
* @return {number} The x shifted by n bits
*/
function shr_32(x, n)
{
function shr_32(x, n)
{
return x >>> n;
}
}
/**
/**
* The 64-bit implementation of shift right
*
* @private
@ -504,8 +503,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {number} n The number of bits to shift
* @return {Int_64} The x shifted by n bits
*/
function shr_64(x, n)
{
function shr_64(x, n)
{
var retVal = null;
if (32 >= n)
@ -524,9 +523,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
return retVal;
}
}
/**
/**
* The 32-bit implementation of the NIST specified Parity function
*
* @private
@ -535,12 +534,12 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {number} z The third 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function parity_32(x, y, z)
{
function parity_32(x, y, z)
{
return x ^ y ^ z;
}
}
/**
/**
* The 32-bit implementation of the NIST specified Ch function
*
* @private
@ -549,12 +548,12 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {number} z The third 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function ch_32(x, y, z)
{
function ch_32(x, y, z)
{
return (x & y) ^ (~x & z);
}
}
/**
/**
* The 64-bit implementation of the NIST specified Ch function
*
* @private
@ -563,15 +562,15 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {Int_64} z The third 64-bit integer argument
* @return {Int_64} The NIST specified output of the function
*/
function ch_64(x, y, z)
{
function ch_64(x, y, z)
{
return new Int_64(
(x.highOrder & y.highOrder) ^ (~x.highOrder & z.highOrder),
(x.lowOrder & y.lowOrder) ^ (~x.lowOrder & z.lowOrder)
);
}
}
/**
/**
* The 32-bit implementation of the NIST specified Maj function
*
* @private
@ -580,12 +579,12 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {number} z The third 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function maj_32(x, y, z)
{
function maj_32(x, y, z)
{
return (x & y) ^ (x & z) ^ (y & z);
}
}
/**
/**
* The 64-bit implementation of the NIST specified Maj function
*
* @private
@ -594,8 +593,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {Int_64} z The third 64-bit integer argument
* @return {Int_64} The NIST specified output of the function
*/
function maj_64(x, y, z)
{
function maj_64(x, y, z)
{
return new Int_64(
(x.highOrder & y.highOrder) ^
(x.highOrder & z.highOrder) ^
@ -604,116 +603,116 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
(x.lowOrder & z.lowOrder) ^
(y.lowOrder & z.lowOrder)
);
}
}
/**
/**
* The 32-bit implementation of the NIST specified Sigma0 function
*
* @private
* @param {number} x The 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function sigma0_32(x)
{
function sigma0_32(x)
{
return rotr_32(x, 2) ^ rotr_32(x, 13) ^ rotr_32(x, 22);
}
}
/**
/**
* The 64-bit implementation of the NIST specified Sigma0 function
*
* @private
* @param {Int_64} x The 64-bit integer argument
* @return {Int_64} The NIST specified output of the function
*/
function sigma0_64(x)
{
function sigma0_64(x)
{
var rotr28 = rotr_64(x, 28), rotr34 = rotr_64(x, 34),
rotr39 = rotr_64(x, 39);
return new Int_64(
rotr28.highOrder ^ rotr34.highOrder ^ rotr39.highOrder,
rotr28.lowOrder ^ rotr34.lowOrder ^ rotr39.lowOrder);
}
}
/**
/**
* The 32-bit implementation of the NIST specified Sigma1 function
*
* @private
* @param {number} x The 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function sigma1_32(x)
{
function sigma1_32(x)
{
return rotr_32(x, 6) ^ rotr_32(x, 11) ^ rotr_32(x, 25);
}
}
/**
/**
* The 64-bit implementation of the NIST specified Sigma1 function
*
* @private
* @param {Int_64} x The 64-bit integer argument
* @return {Int_64} The NIST specified output of the function
*/
function sigma1_64(x)
{
function sigma1_64(x)
{
var rotr14 = rotr_64(x, 14), rotr18 = rotr_64(x, 18),
rotr41 = rotr_64(x, 41);
return new Int_64(
rotr14.highOrder ^ rotr18.highOrder ^ rotr41.highOrder,
rotr14.lowOrder ^ rotr18.lowOrder ^ rotr41.lowOrder);
}
}
/**
/**
* The 32-bit implementation of the NIST specified Gamma0 function
*
* @private
* @param {number} x The 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function gamma0_32(x)
{
function gamma0_32(x)
{
return rotr_32(x, 7) ^ rotr_32(x, 18) ^ shr_32(x, 3);
}
}
/**
/**
* The 64-bit implementation of the NIST specified Gamma0 function
*
* @private
* @param {Int_64} x The 64-bit integer argument
* @return {Int_64} The NIST specified output of the function
*/
function gamma0_64(x)
{
function gamma0_64(x)
{
var rotr1 = rotr_64(x, 1), rotr8 = rotr_64(x, 8), shr7 = shr_64(x, 7);
return new Int_64(
rotr1.highOrder ^ rotr8.highOrder ^ shr7.highOrder,
rotr1.lowOrder ^ rotr8.lowOrder ^ shr7.lowOrder
);
}
}
/**
/**
* The 32-bit implementation of the NIST specified Gamma1 function
*
* @private
* @param {number} x The 32-bit integer argument
* @return {number} The NIST specified output of the function
*/
function gamma1_32(x)
{
function gamma1_32(x)
{
return rotr_32(x, 17) ^ rotr_32(x, 19) ^ shr_32(x, 10);
}
}
/**
/**
* The 64-bit implementation of the NIST specified Gamma1 function
*
* @private
* @param {Int_64} x The 64-bit integer argument
* @return {Int_64} The NIST specified output of the function
*/
function gamma1_64(x)
{
function gamma1_64(x)
{
var rotr19 = rotr_64(x, 19), rotr61 = rotr_64(x, 61),
shr6 = shr_64(x, 6);
@ -721,9 +720,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
rotr19.highOrder ^ rotr61.highOrder ^ shr6.highOrder,
rotr19.lowOrder ^ rotr61.lowOrder ^ shr6.lowOrder
);
}
}
/**
/**
* Add two 32-bit integers, wrapping at 2^32. This uses 16-bit operations
* internally to work around bugs in some JS interpreters.
*
@ -732,15 +731,15 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {number} b The second 32-bit integer argument to be added
* @return {number} The sum of a + b
*/
function safeAdd_32_2(a, b)
{
function safeAdd_32_2(a, b)
{
var lsw = (a & 0xFFFF) + (b & 0xFFFF),
msw = (a >>> 16) + (b >>> 16) + (lsw >>> 16);
return ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF);
}
}
/**
/**
* Add four 32-bit integers, wrapping at 2^32. This uses 16-bit operations
* internally to work around bugs in some JS interpreters.
*
@ -751,16 +750,16 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {number} d The fourth 32-bit integer argument to be added
* @return {number} The sum of a + b + c + d
*/
function safeAdd_32_4(a, b, c, d)
{
function safeAdd_32_4(a, b, c, d)
{
var lsw = (a & 0xFFFF) + (b & 0xFFFF) + (c & 0xFFFF) + (d & 0xFFFF),
msw = (a >>> 16) + (b >>> 16) + (c >>> 16) + (d >>> 16) +
(lsw >>> 16);
return ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF);
}
}
/**
/**
* Add five 32-bit integers, wrapping at 2^32. This uses 16-bit operations
* internally to work around bugs in some JS interpreters.
*
@ -772,17 +771,17 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {number} e The fifth 32-bit integer argument to be added
* @return {number} The sum of a + b + c + d + e
*/
function safeAdd_32_5(a, b, c, d, e)
{
function safeAdd_32_5(a, b, c, d, e)
{
var lsw = (a & 0xFFFF) + (b & 0xFFFF) + (c & 0xFFFF) + (d & 0xFFFF) +
(e & 0xFFFF),
msw = (a >>> 16) + (b >>> 16) + (c >>> 16) + (d >>> 16) +
(e >>> 16) + (lsw >>> 16);
return ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF);
}
}
/**
/**
* Add two 64-bit integers, wrapping at 2^64. This uses 16-bit operations
* internally to work around bugs in some JS interpreters.
*
@ -791,8 +790,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {Int_64} y The second 64-bit integer argument to be added
* @return {Int_64} The sum of x + y
*/
function safeAdd_64_2(x, y)
{
function safeAdd_64_2(x, y)
{
var lsw, msw, lowOrder, highOrder;
lsw = (x.lowOrder & 0xFFFF) + (y.lowOrder & 0xFFFF);
@ -804,9 +803,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
highOrder = ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF);
return new Int_64(highOrder, lowOrder);
}
}
/**
/**
* Add four 64-bit integers, wrapping at 2^64. This uses 16-bit operations
* internally to work around bugs in some JS interpreters.
*
@ -817,8 +816,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {Int_64} d The fouth 64-bit integer argument to be added
* @return {Int_64} The sum of a + b + c + d
*/
function safeAdd_64_4(a, b, c, d)
{
function safeAdd_64_4(a, b, c, d)
{
var lsw, msw, lowOrder, highOrder;
lsw = (a.lowOrder & 0xFFFF) + (b.lowOrder & 0xFFFF) +
@ -834,9 +833,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
highOrder = ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF);
return new Int_64(highOrder, lowOrder);
}
}
/**
/**
* Add five 64-bit integers, wrapping at 2^64. This uses 16-bit operations
* internally to work around bugs in some JS interpreters.
*
@ -848,8 +847,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {Int_64} e The fouth 64-bit integer argument to be added
* @return {Int_64} The sum of a + b + c + d + e
*/
function safeAdd_64_5(a, b, c, d, e)
{
function safeAdd_64_5(a, b, c, d, e)
{
var lsw, msw, lowOrder, highOrder;
lsw = (a.lowOrder & 0xFFFF) + (b.lowOrder & 0xFFFF) +
@ -869,9 +868,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
highOrder = ((msw & 0xFFFF) << 16) | (lsw & 0xFFFF);
return new Int_64(highOrder, lowOrder);
}
}
/**
/**
* Calculates the SHA-1 hash of the string set at instantiation
*
* @private
@ -881,8 +880,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @return {Array.<number>} The array of integers representing the SHA-1
* hash of message
*/
function coreSHA1(message, messageLen)
{
function coreSHA1(message, messageLen)
{
var W = [], a, b, c, d, e, T, ch = ch_32, parity = parity_32,
maj = maj_32, rotl = rotl_32, safeAdd_2 = safeAdd_32_2, i, t,
safeAdd_5 = safeAdd_32_5, appendedMessageLength, offset,
@ -953,9 +952,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
return H;
}
}
/**
/**
* Calculates the desired SHA-2 hash of the string set at instantiation
*
* @private
@ -966,8 +965,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @return {Array.<number>} The array of integers representing the SHA-2
* hash of message
*/
function coreSHA2(message, messageLen, variant)
{
function coreSHA2(message, messageLen, variant)
{
var a, b, c, d, e, f, g, h, T1, T2, H, numRounds, lengthPosition, i, t,
binaryStringInc, binaryStringMult, safeAdd_2, safeAdd_4, safeAdd_5,
gamma0, gamma1, sigma0, sigma1, ch, maj, Int, W = [], int1, int2, offset,
@ -1220,9 +1219,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
}
return retVal;
}
}
/**
/**
* jsSHA is the workhorse of the library. Instantiate it with the string to
* be hashed as the parameter
*
@ -1234,8 +1233,8 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
* @param {string=} encoding The text encoding to use to encode the source
* string
*/
var jsSHA = function(srcString, inputFormat, encoding)
{
var jsSHA = function(srcString, inputFormat, encoding)
{
var strBinLen = 0, strToHash = [0], utfType = '', srcConvertRet = null;
utfType = encoding || "UTF8";
@ -1573,9 +1572,9 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
return formatFunc(retVal, getOutputOpts(outputFormatOpts));
};
};
};
module.exports = {
export default {
/** SHA1 hash */
sha1: function(str) {
var shaObj = new jsSHA(str, "TYPED", "UTF8");
@ -1602,5 +1601,4 @@ var SUPPORTED_ALGS = 4 | 2 | 1;
var shaObj = new jsSHA(str, "TYPED", "UTF8");
return shaObj.getHash("SHA-512", "TYPED");
}
};
}(this));
};