@iota/kerl
Version:
IOTA-related cryptographic methods
101 lines • 2.99 kB
JavaScript
;
exports.__esModule = true;
/* tslint:disable variable-name no-conditional-assignment */
var CryptoJS = require("crypto-js");
require("../../typed-array");
var errors = require("./errors");
var word_converter_1 = require("./word-converter");
var BIT_HASH_LENGTH = 384;
var HASH_LENGTH = 243;
/**
* @class kerl
* @ignore
*/
var Kerl = /** @class */ (function () {
/**
* @constructor
* @ignore
*/
function Kerl() {
this.k = CryptoJS.algo.SHA3.create();
this.k.init({
outputLength: BIT_HASH_LENGTH
});
}
Kerl.prototype.initialize = function (state) {
/* empty */
};
/**
* Resets the internal state
*
* @method reset
*
* @ignore
*/
Kerl.prototype.reset = function () {
this.k.reset();
};
/**
* Absorbs trits given an offset and length
*
* @method absorb
*
* @ignore
*
* @param {Int8Array} trits
* @param {number} offset
* @param {number} length
**/
Kerl.prototype.absorb = function (trits, offset, length) {
if (length && length % 243 !== 0) {
throw new Error(errors.ILLEGAL_TRITS_LENGTH);
}
do {
var limit = length < Kerl.HASH_LENGTH ? length : Kerl.HASH_LENGTH;
var trit_state = trits.slice(offset, offset + limit);
offset += limit;
// convert trit state to words
var wordsToAbsorb = word_converter_1.tritsToWords(trit_state);
// absorb the trit stat as wordarray
this.k.update(CryptoJS.lib.WordArray.create(wordsToAbsorb));
} while ((length -= Kerl.HASH_LENGTH) > 0);
};
/**
* Squeezes trits given an offset and length
*
* @method squeeze
*
* @ignore
*
* @param {Int8Array} trits
* @param {number} offset
* @param {number} length
**/
Kerl.prototype.squeeze = function (trits, offset, length) {
if (length && length % 243 !== 0) {
throw new Error(errors.ILLEGAL_TRITS_LENGTH);
}
do {
// get the hash digest
var kCopy = this.k.clone();
var final = kCopy.finalize();
// Convert words to trits and then map it into the internal state
var trit_state = word_converter_1.wordsToTrits(final.words);
var i = 0;
var limit = length < Kerl.HASH_LENGTH ? length : Kerl.HASH_LENGTH;
while (i < limit) {
trits[offset++] = trit_state[i++];
}
this.reset();
for (i = 0; i < final.words.length; i++) {
final.words[i] = final.words[i] ^ 0xffffffff;
}
this.k.update(final);
} while ((length -= Kerl.HASH_LENGTH) > 0);
};
Kerl.BIT_HASH_LENGTH = BIT_HASH_LENGTH;
Kerl.HASH_LENGTH = HASH_LENGTH;
return Kerl;
}());
exports["default"] = Kerl;
//# sourceMappingURL=kerl.js.map