UNPKG

cerceis-lib

Version:

Contains list of quality of life functions that is written in TypeScript and es6

2,638 lines (2,635 loc) 27.4 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/sha256/index.ts var sha256_exports = {}; __export(sha256_exports, { Sha256: () => Sha256 }); module.exports = __toCommonJS(sha256_exports); // src/sha256/constants.ts var primeConst = [ [ "0", "1", "0", "0", "0", "0", "1", "0", "1", "0", "0", "0", "1", "0", "1", "0", "0", "0", "1", "0", "1", "1", "1", "1", "1", "0", "0", "1", "1", "0", "0", "0" ], [ "0", "1", "1", "1", "0", "0", "0", "1", "0", "0", "1", "1", "0", "1", "1", "1", "0", "1", "0", "0", "0", "1", "0", "0", "1", "0", "0", "1", "0", "0", "0", "1" ], [ "1", "0", "1", "1", "0", "1", "0", "1", "1", "1", "0", "0", "0", "0", "0", "0", "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "0", "0", "1", "1", "1", "1" ], [ "1", "1", "1", "0", "1", "0", "0", "1", "1", "0", "1", "1", "0", "1", "0", "1", "1", "1", "0", "1", "1", "0", "1", "1", "1", "0", "1", "0", "0", "1", "0", "1" ], [ "0", "0", "1", "1", "1", "0", "0", "1", "0", "1", "0", "1", "0", "1", "1", "0", "1", "1", "0", "0", "0", "0", "1", "0", "0", "1", "0", "1", "1", "0", "1", "1" ], [ "0", "1", "0", "1", "1", "0", "0", "1", "1", "1", "1", "1", "0", "0", "0", "1", "0", "0", "0", "1", "0", "0", "0", "1", "1", "1", "1", "1", "0", "0", "0", "1" ], [ "1", "0", "0", "1", "0", "0", "1", "0", "0", "0", "1", "1", "1", "1", "1", "1", "1", "0", "0", "0", "0", "0", "1", "0", "1", "0", "1", "0", "0", "1", "0", "0" ], [ "1", "0", "1", "0", "1", "0", "1", "1", "0", "0", "0", "1", "1", "1", "0", "0", "0", "1", "0", "1", "1", "1", "1", "0", "1", "1", "0", "1", "0", "1", "0", "1" ], [ "1", "1", "0", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", "1", "1", "1", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "0", "1", "1", "0", "0", "0" ], [ "0", "0", "0", "1", "0", "0", "1", "0", "1", "0", "0", "0", "0", "0", "1", "1", "0", "1", "0", "1", "1", "0", "1", "1", "0", "0", "0", "0", "0", "0", "0", "1" ], [ "0", "0", "1", "0", "0", "1", "0", "0", "0", "0", "1", "1", "0", "0", "0", "1", "1", "0", "0", "0", "0", "1", "0", "1", "1", "0", "1", "1", "1", "1", "1", "0" ], [ "0", "1", "0", "1", "0", "1", "0", "1", "0", "0", "0", "0", "1", "1", "0", "0", "0", "1", "1", "1", "1", "1", "0", "1", "1", "1", "0", "0", "0", "0", "1", "1" ], [ "0", "1", "1", "1", "0", "0", "1", "0", "1", "0", "1", "1", "1", "1", "1", "0", "0", "1", "0", "1", "1", "1", "0", "1", "0", "1", "1", "1", "0", "1", "0", "0" ], [ "1", "0", "0", "0", "0", "0", "0", "0", "1", "1", "0", "1", "1", "1", "1", "0", "1", "0", "1", "1", "0", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1", "0" ], [ "1", "0", "0", "1", "1", "0", "1", "1", "1", "1", "0", "1", "1", "1", "0", "0", "0", "0", "0", "0", "0", "1", "1", "0", "1", "0", "1", "0", "0", "1", "1", "1" ], [ "1", "1", "0", "0", "0", "0", "0", "1", "1", "0", "0", "1", "1", "0", "1", "1", "1", "1", "1", "1", "0", "0", "0", "1", "0", "1", "1", "1", "0", "1", "0", "0" ], [ "1", "1", "1", "0", "0", "1", "0", "0", "1", "0", "0", "1", "1", "0", "1", "1", "0", "1", "1", "0", "1", "0", "0", "1", "1", "1", "0", "0", "0", "0", "0", "1" ], [ "1", "1", "1", "0", "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "0", "0", "1", "0", "0", "0", "1", "1", "1", "1", "0", "0", "0", "0", "1", "1", "0" ], [ "0", "0", "0", "0", "1", "1", "1", "1", "1", "1", "0", "0", "0", "0", "0", "1", "1", "0", "0", "1", "1", "1", "0", "1", "1", "1", "0", "0", "0", "1", "1", "0" ], [ "0", "0", "1", "0", "0", "1", "0", "0", "0", "0", "0", "0", "1", "1", "0", "0", "1", "0", "1", "0", "0", "0", "0", "1", "1", "1", "0", "0", "1", "1", "0", "0" ], [ "0", "0", "1", "0", "1", "1", "0", "1", "1", "1", "1", "0", "1", "0", "0", "1", "0", "0", "1", "0", "1", "1", "0", "0", "0", "1", "1", "0", "1", "1", "1", "1" ], [ "0", "1", "0", "0", "1", "0", "1", "0", "0", "1", "1", "1", "0", "1", "0", "0", "1", "0", "0", "0", "0", "1", "0", "0", "1", "0", "1", "0", "1", "0", "1", "0" ], [ "0", "1", "0", "1", "1", "1", "0", "0", "1", "0", "1", "1", "0", "0", "0", "0", "1", "0", "1", "0", "1", "0", "0", "1", "1", "1", "0", "1", "1", "1", "0", "0" ], [ "0", "1", "1", "1", "0", "1", "1", "0", "1", "1", "1", "1", "1", "0", "0", "1", "1", "0", "0", "0", "1", "0", "0", "0", "1", "1", "0", "1", "1", "0", "1", "0" ], [ "1", "0", "0", "1", "1", "0", "0", "0", "0", "0", "1", "1", "1", "1", "1", "0", "0", "1", "0", "1", "0", "0", "0", "1", "0", "1", "0", "1", "0", "0", "1", "0" ], [ "1", "0", "1", "0", "1", "0", "0", "0", "0", "0", "1", "1", "0", "0", "0", "1", "1", "1", "0", "0", "0", "1", "1", "0", "0", "1", "1", "0", "1", "1", "0", "1" ], [ "1", "0", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "1", "0", "0", "1", "0", "0", "1", "1", "1", "1", "1", "0", "0", "1", "0", "0", "0" ], [ "1", "0", "1", "1", "1", "1", "1", "1", "0", "1", "0", "1", "1", "0", "0", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "0", "0", "1", "1", "1" ], [ "1", "1", "0", "0", "0", "1", "1", "0", "1", "1", "1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "1", "0", "1", "1", "1", "1", "1", "1", "0", "0", "1", "1" ], [ "1", "1", "0", "1", "0", "1", "0", "1", "1", "0", "1", "0", "0", "1", "1", "1", "1", "0", "0", "1", "0", "0", "0", "1", "0", "1", "0", "0", "0", "1", "1", "1" ], [ "0", "0", "0", "0", "0", "1", "1", "0", "1", "1", "0", "0", "1", "0", "1", "0", "0", "1", "1", "0", "0", "0", "1", "1", "0", "1", "0", "1", "0", "0", "0", "1" ], [ "0", "0", "0", "1", "0", "1", "0", "0", "0", "0", "1", "0", "1", "0", "0", "1", "0", "0", "1", "0", "1", "0", "0", "1", "0", "1", "1", "0", "0", "1", "1", "1" ], [ "0", "0", "1", "0", "0", "1", "1", "1", "1", "0", "1", "1", "0", "1", "1", "1", "0", "0", "0", "0", "1", "0", "1", "0", "1", "0", "0", "0", "0", "1", "0", "1" ], [ "0", "0", "1", "0", "1", "1", "1", "0", "0", "0", "0", "1", "1", "0", "1", "1", "0", "0", "1", "0", "0", "0", "0", "1", "0", "0", "1", "1", "1", "0", "0", "0" ], [ "0", "1", "0", "0", "1", "1", "0", "1", "0", "0", "1", "0", "1", "1", "0", "0", "0", "1", "1", "0", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "0", "0" ], [ "0", "1", "0", "1", "0", "0", "1", "1", "0", "0", "1", "1", "1", "0", "0", "0", "0", "0", "0", "0", "1", "1", "0", "1", "0", "0", "0", "1", "0", "0", "1", "1" ], [ "0", "1", "1", "0", "0", "1", "0", "1", "0", "0", "0", "0", "1", "0", "1", "0", "0", "1", "1", "1", "0", "0", "1", "1", "0", "1", "0", "1", "0", "1", "0", "0" ], [ "0", "1", "1", "1", "0", "1", "1", "0", "0", "1", "1", "0", "1", "0", "1", "0", "0", "0", "0", "0", "1", "0", "1", "0", "1", "0", "1", "1", "1", "0", "1", "1" ], [ "1", "0", "0", "0", "0", "0", "0", "1", "1", "1", "0", "0", "0", "0", "1", "0", "1", "1", "0", "0", "1", "0", "0", "1", "0", "0", "1", "0", "1", "1", "1", "0" ], [ "1", "0", "0", "1", "0", "0", "1", "0", "0", "1", "1", "1", "0", "0", "1", "0", "0", "0", "1", "0", "1", "1", "0", "0", "1", "0", "0", "0", "0", "1", "0", "1" ], [ "1", "0", "1", "0", "0", "0", "1", "0", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "1", "0", "0", "0", "1", "0", "1", "0", "0", "0", "0", "1" ], [ "1", "0", "1", "0", "1", "0", "0", "0", "0", "0", "0", "1", "1", "0", "1", "0", "0", "1", "1", "0", "0", "1", "1", "0", "0", "1", "0", "0", "1", "0", "1", "1" ], [ "1", "1", "0", "0", "0", "0", "1", "0", "0", "1", "0", "0", "1", "0", "1", "1", "1", "0", "0", "0", "1", "0", "1", "1", "0", "1", "1", "1", "0", "0", "0", "0" ], [ "1", "1", "0", "0", "0", "1", "1", "1", "0", "1", "1", "0", "1", "1", "0", "0", "0", "1", "0", "1", "0", "0", "0", "1", "1", "0", "1", "0", "0", "0", "1", "1" ], [ "1", "1", "0", "1", "0", "0", "0", "1", "1", "0", "0", "1", "0", "0", "1", "0", "1", "1", "1", "0", "1", "0", "0", "0", "0", "0", "0", "1", "1", "0", "0", "1" ], [ "1", "1", "0", "1", "0", "1", "1", "0", "1", "0", "0", "1", "1", "0", "0", "1", "0", "0", "0", "0", "0", "1", "1", "0", "0", "0", "1", "0", "0", "1", "0", "0" ], [ "1", "1", "1", "1", "0", "1", "0", "0", "0", "0", "0", "0", "1", "1", "1", "0", "0", "0", "1", "1", "0", "1", "0", "1", "1", "0", "0", "0", "0", "1", "0", "1" ], [ "0", "0", "0", "1", "0", "0", "0", "0", "0", "1", "1", "0", "1", "0", "1", "0", "1", "0", "1", "0", "0", "0", "0", "0", "0", "1", "1", "1", "0", "0", "0", "0" ], [ "0", "0", "0", "1", "1", "0", "0", "1", "1", "0", "1", "0", "0", "1", "0", "0", "1", "1", "0", "0", "0", "0", "0", "1", "0", "0", "0", "1", "0", "1", "1", "0" ], [ "0", "0", "0", "1", "1", "1", "1", "0", "0", "0", "1", "1", "0", "1", "1", "1", "0", "1", "1", "0", "1", "1", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0" ], [ "0", "0", "1", "0", "0", "1", "1", "1", "0", "1", "0", "0", "1", "0", "0", "0", "0", "1", "1", "1", "0", "1", "1", "1", "0", "1", "0", "0", "1", "1", "0", "0" ], [ "0", "0", "1", "1", "0", "1", "0", "0", "1", "0", "1", "1", "0", "0", "0", "0", "1", "0", "1", "1", "1", "1", "0", "0", "1", "0", "1", "1", "0", "1", "0", "1" ], [ "0", "0", "1", "1", "1", "0", "0", "1", "0", "0", "0", "1", "1", "1", "0", "0", "0", "0", "0", "0", "1", "1", "0", "0", "1", "0", "1", "1", "0", "0", "1", "1" ], [ "0", "1", "0", "0", "1", "1", "1", "0", "1", "1", "0", "1", "1", "0", "0", "0", "1", "0", "1", "0", "1", "0", "1", "0", "0", "1", "0", "0", "1", "0", "1", "0" ], [ "0", "1", "0", "1", "1", "0", "1", "1", "1", "0", "0", "1", "1", "1", "0", "0", "1", "1", "0", "0", "1", "0", "1", "0", "0", "1", "0", "0", "1", "1", "1", "1" ], [ "0", "1", "1", "0", "1", "0", "0", "0", "0", "0", "1", "0", "1", "1", "1", "0", "0", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "0", "0", "1", "1" ], [ "0", "1", "1", "1", "0", "1", "0", "0", "1", "0", "0", "0", "1", "1", "1", "1", "1", "0", "0", "0", "0", "0", "1", "0", "1", "1", "1", "0", "1", "1", "1", "0" ], [ "0", "1", "1", "1", "1", "0", "0", "0", "1", "0", "1", "0", "0", "1", "0", "1", "0", "1", "1", "0", "0", "0", "1", "1", "0", "1", "1", "0", "1", "1", "1", "1" ], [ "1", "0", "0", "0", "0", "1", "0", "0", "1", "1", "0", "0", "1", "0", "0", "0", "0", "1", "1", "1", "1", "0", "0", "0", "0", "0", "0", "1", "0", "1", "0", "0" ], [ "1", "0", "0", "0", "1", "1", "0", "0", "1", "1", "0", "0", "0", "1", "1", "1", "0", "0", "0", "0", "0", "0", "1", "0", "0", "0", "0", "0", "1", "0", "0", "0" ], [ "1", "0", "0", "1", "0", "0", "0", "0", "1", "0", "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "0", "1", "0" ], [ "1", "0", "1", "0", "0", "1", "0", "0", "0", "1", "0", "1", "0", "0", "0", "0", "0", "1", "1", "0", "1", "1", "0", "0", "1", "1", "1", "0", "1", "0", "1", "1" ], [ "1", "0", "1", "1", "1", "1", "1", "0", "1", "1", "1", "1", "1", "0", "0", "1", "1", "0", "1", "0", "0", "0", "1", "1", "1", "1", "1", "1", "0", "1", "1", "1" ], [ "1", "1", "0", "0", "0", "1", "1", "0", "0", "1", "1", "1", "0", "0", "0", "1", "0", "1", "1", "1", "1", "0", "0", "0", "1", "1", "1", "1", "0", "0", "1", "0" ] ]; var compressionHash = { "a": [ "0", "1", "1", "0", "1", "0", "1", "0", "0", "0", "0", "0", "1", "0", "0", "1", "1", "1", "1", "0", "0", "1", "1", "0", "0", "1", "1", "0", "0", "1", "1", "1" ], "b": [ "1", "0", "1", "1", "1", "0", "1", "1", "0", "1", "1", "0", "0", "1", "1", "1", "1", "0", "1", "0", "1", "1", "1", "0", "1", "0", "0", "0", "0", "1", "0", "1" ], "c": [ "0", "0", "1", "1", "1", "1", "0", "0", "0", "1", "1", "0", "1", "1", "1", "0", "1", "1", "1", "1", "0", "0", "1", "1", "0", "1", "1", "1", "0", "0", "1", "0" ], "d": [ "1", "0", "1", "0", "0", "1", "0", "1", "0", "1", "0", "0", "1", "1", "1", "1", "1", "1", "1", "1", "0", "1", "0", "1", "0", "0", "1", "1", "1", "0", "1", "0" ], "e": [ "0", "1", "0", "1", "0", "0", "0", "1", "0", "0", "0", "0", "1", "1", "1", "0", "0", "1", "0", "1", "0", "0", "1", "0", "0", "1", "1", "1", "1", "1", "1", "1" ], "f": [ "1", "0", "0", "1", "1", "0", "1", "1", "0", "0", "0", "0", "0", "1", "0", "1", "0", "1", "1", "0", "1", "0", "0", "0", "1", "0", "0", "0", "1", "1", "0", "0" ], "g": [ "0", "0", "0", "1", "1", "1", "1", "1", "1", "0", "0", "0", "0", "0", "1", "1", "1", "1", "0", "1", "1", "0", "0", "1", "1", "0", "1", "0", "1", "0", "1", "1" ], "h": [ "0", "1", "0", "1", "1", "0", "1", "1", "1", "1", "1", "0", "0", "0", "0", "0", "1", "1", "0", "0", "1", "1", "0", "1", "0", "0", "0", "1", "1", "0", "0", "1" ] }; // src/sha256/sha256.ts var Sha256 = (inputStr) => { const _toBinary = (text) => { let rs = ""; for (let i = 0; i < text.length; i++) { rs += text[i].charCodeAt(0).toString(2).padStart(8, "0"); } return rs; }; const _rotr = (arr, attempt = 1) => { let tmp = [...arr]; for (let i = 0; i < attempt; i++) { tmp = [tmp.pop(), ...tmp]; } return tmp; }; const _shr = (arr, attempt = 1) => { let tmp = [...arr]; for (let i = 0; i < attempt; i++) { tmp.pop(); tmp.unshift("0"); } return tmp; }; const _xor = (arrA, arrB, arrC) => { const tmp = []; arrA.forEach((bit, index) => { tmp[index] = String(Number(bit) ^ Number(arrB[index]) ^ Number(arrC[index])); }); return tmp; }; const _add = (arrA, arrB) => { const tmp = []; const len = arrA.length; let extra = 0; for (let i = arrA.length - 1; i >= 0; i--) { const bitValue = Number(arrA[i]) + Number(arrB[i]) + extra; tmp[i] = bitValue > 1 ? String(bitValue - 2) : String(bitValue); extra = bitValue > 1 ? 1 : 0; } return tmp; }; const _choice = (arrA, arrB, arrC) => { let tmp = []; arrA.forEach((bit, index) => { tmp[index] = bit === "1" ? arrB[index] : arrC[index]; }); return tmp; }; const _major = (arrA, arrB, arrC) => { let tmp = []; arrA.forEach((bit, index) => { const val = bit + arrB[index] + arrC[index]; if (val === "000" || val === "001" || val === "010" || val === "100") tmp[index] = "0"; else if (val === "111" || val === "110" || val === "101" || val === "011") tmp[index] = "1"; }); return tmp; }; const _sigZero = (arr) => { const rotr7 = _rotr(arr, 7); const rotr18 = _rotr(arr, 18); const shr3 = _shr(arr, 3); return _xor(rotr7, rotr18, shr3); }; const _sigOne = (arr) => { const rotr17 = _rotr(arr, 17); const rotr19 = _rotr(arr, 19); const shr10 = _shr(arr, 10); return _xor(rotr17, rotr19, shr10); }; const _uSigZero = (arr) => { const rotr2 = _rotr(arr, 2); const rotr13 = _rotr(arr, 13); const rotr22 = _rotr(arr, 22); return _xor(rotr2, rotr13, rotr22); }; const _uSigOne = (arr) => { const rotr6 = _rotr(arr, 6); const rotr11 = _rotr(arr, 11); const rotr25 = _rotr(arr, 25); return _xor(rotr6, rotr11, rotr25); }; const shiftCompressionMap = (compressionMap) => { compressionMap["h"] = [...compressionMap["g"]]; compressionMap["g"] = [...compressionMap["f"]]; compressionMap["f"] = [...compressionMap["e"]]; compressionMap["e"] = [...compressionMap["d"]]; compressionMap["d"] = [...compressionMap["c"]]; compressionMap["c"] = [...compressionMap["b"]]; compressionMap["b"] = [...compressionMap["a"]]; compressionMap["a"] = []; }; let messageBin = _toBinary(inputStr); const messageBlocks = []; const messageLengthBin = messageBin.length.toString(2).padStart(64, "0"); const blockCount = Math.ceil((messageBin.length + 1 + 64) / 512); const finalLength = blockCount * 512; messageBin += "1"; messageBin = messageBin.padEnd(finalLength - 64, "0"); messageBin += messageLengthBin; for (let i = 0; i < blockCount; i++) { let messageChunks = messageBin.slice(i * 512, i * 512 + 512); messageBlocks.push(messageChunks); } let finalCompression = { ...compressionHash }; messageBlocks.forEach((block, bi) => { let localCompression = { ...finalCompression }; const words = []; for (let i = 0; i < 16; i++) { words.push(block.slice(i * 32, i * 32 + 32).split("")); } while (words.length < 64) { words.push( _add( _add( _add(_sigOne(words[words.length - 2]), words[words.length - 7]), _sigZero(words[words.length - 15]) ), words[words.length - 16] ) ); } words.forEach((word, index) => { const t1 = _add( _add( _add( _add(_uSigOne(localCompression.e), _choice(localCompression.e, localCompression.f, localCompression.g)), localCompression.h ), primeConst[index] ), word ); const t2 = _add( _uSigZero(localCompression.a), _major(localCompression.a, localCompression.b, localCompression.c) ); shiftCompressionMap(localCompression); localCompression.a = _add(t1, t2); localCompression.e = _add(localCompression.e, t1); }); for (let id in localCompression) { if (bi === 0) finalCompression[id] = _add(compressionHash[id], localCompression[id]); else finalCompression[id] = _add(finalCompression[id], localCompression[id]); } }); let result = ""; for (let id in finalCompression) result += parseInt(finalCompression[id].join(""), 2).toString(16); return result; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Sha256 }); //# sourceMappingURL=index.js.map