nats
Version:
Node.js client for NATS, a lightweight, high-performance cloud native messaging system
365 lines • 12.4 kB
JavaScript
"use strict";
// deno-fmt-ignore-file
// deno-lint-ignore-file
// This code was bundled using `deno bundle` and it's not recommended to edit it manually
Object.defineProperty(exports, "__esModule", { value: true });
exports.sha256 = exports.SHA256 = exports.BYTES = void 0;
// deno bundle https://deno.land/x/sha256@v1.0.2/mod.ts
// The MIT License (MIT)
//
// Original work (c) Marco Paland (marco@paland.com) 2015-2018, PALANDesign Hannover, Germany
//
// Deno port Copyright (c) 2019 Noah Anabiik Schwarz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
function getLengths(b64) {
const len = b64.length;
let validLen = b64.indexOf("=");
if (validLen === -1) {
validLen = len;
}
const placeHoldersLen = validLen === len ? 0 : 4 - validLen % 4;
return [
validLen,
placeHoldersLen
];
}
function init(lookup, revLookup, urlsafe = false) {
function _byteLength(validLen, placeHoldersLen) {
return Math.floor((validLen + placeHoldersLen) * 3 / 4 - placeHoldersLen);
}
function tripletToBase64(num) {
return lookup[num >> 18 & 0x3f] + lookup[num >> 12 & 0x3f] + lookup[num >> 6 & 0x3f] + lookup[num & 0x3f];
}
function encodeChunk(buf, start, end) {
const out = new Array((end - start) / 3);
for (let i = start, curTriplet = 0; i < end; i += 3) {
out[curTriplet++] = tripletToBase64((buf[i] << 16) + (buf[i + 1] << 8) + buf[i + 2]);
}
return out.join("");
}
return {
byteLength(b64) {
return _byteLength.apply(null, getLengths(b64));
},
toUint8Array(b64) {
const [validLen, placeHoldersLen] = getLengths(b64);
const buf = new Uint8Array(_byteLength(validLen, placeHoldersLen));
const len = placeHoldersLen ? validLen - 4 : validLen;
let tmp;
let curByte = 0;
let i;
for (i = 0; i < len; i += 4) {
tmp = revLookup[b64.charCodeAt(i)] << 18 | revLookup[b64.charCodeAt(i + 1)] << 12 | revLookup[b64.charCodeAt(i + 2)] << 6 | revLookup[b64.charCodeAt(i + 3)];
buf[curByte++] = tmp >> 16 & 0xff;
buf[curByte++] = tmp >> 8 & 0xff;
buf[curByte++] = tmp & 0xff;
}
if (placeHoldersLen === 2) {
tmp = revLookup[b64.charCodeAt(i)] << 2 | revLookup[b64.charCodeAt(i + 1)] >> 4;
buf[curByte++] = tmp & 0xff;
}
else if (placeHoldersLen === 1) {
tmp = revLookup[b64.charCodeAt(i)] << 10 | revLookup[b64.charCodeAt(i + 1)] << 4 | revLookup[b64.charCodeAt(i + 2)] >> 2;
buf[curByte++] = tmp >> 8 & 0xff;
buf[curByte++] = tmp & 0xff;
}
return buf;
},
fromUint8Array(buf) {
const maxChunkLength = 16383;
const len = buf.length;
const extraBytes = len % 3;
const len2 = len - extraBytes;
const parts = new Array(Math.ceil(len2 / 16383) + (extraBytes ? 1 : 0));
let curChunk = 0;
let chunkEnd;
for (let i = 0; i < len2; i += maxChunkLength) {
chunkEnd = i + maxChunkLength;
parts[curChunk++] = encodeChunk(buf, i, chunkEnd > len2 ? len2 : chunkEnd);
}
let tmp;
if (extraBytes === 1) {
tmp = buf[len2];
parts[curChunk] = lookup[tmp >> 2] + lookup[tmp << 4 & 0x3f];
if (!urlsafe)
parts[curChunk] += "==";
}
else if (extraBytes === 2) {
tmp = buf[len2] << 8 | buf[len2 + 1] & 0xff;
parts[curChunk] = lookup[tmp >> 10] + lookup[tmp >> 4 & 0x3f] + lookup[tmp << 2 & 0x3f];
if (!urlsafe)
parts[curChunk] += "=";
}
return parts.join("");
}
};
}
const lookup = [];
const revLookup = [];
const code = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
for (let i = 0, l = code.length; i < l; ++i) {
lookup[i] = code[i];
revLookup[code.charCodeAt(i)] = i;
}
const { byteLength, toUint8Array, fromUint8Array } = init(lookup, revLookup, true);
const decoder = new TextDecoder();
const encoder = new TextEncoder();
function toHexString(buf) {
return buf.reduce((hex, __byte) => `${hex}${__byte < 16 ? "0" : ""}${__byte.toString(16)}`, "");
}
function fromHexString(hex) {
const len = hex.length;
if (len % 2 || !/^[0-9a-fA-F]+$/.test(hex)) {
throw new TypeError("Invalid hex string.");
}
hex = hex.toLowerCase();
const buf = new Uint8Array(Math.floor(len / 2));
const end = len / 2;
for (let i = 0; i < end; ++i) {
buf[i] = parseInt(hex.substr(i * 2, 2), 16);
}
return buf;
}
function decode(buf, encoding = "utf8") {
if (/^utf-?8$/i.test(encoding)) {
return decoder.decode(buf);
}
else if (/^base64$/i.test(encoding)) {
return fromUint8Array(buf);
}
else if (/^hex(?:adecimal)?$/i.test(encoding)) {
return toHexString(buf);
}
else {
throw new TypeError("Unsupported string encoding.");
}
}
function encode(str, encoding = "utf8") {
if (/^utf-?8$/i.test(encoding)) {
return encoder.encode(str);
}
else if (/^base64$/i.test(encoding)) {
return toUint8Array(str);
}
else if (/^hex(?:adecimal)?$/i.test(encoding)) {
return fromHexString(str);
}
else {
throw new TypeError("Unsupported string encoding.");
}
}
const BYTES = 32;
exports.BYTES = BYTES;
class SHA256 {
constructor() {
this.hashSize = 32;
this._buf = new Uint8Array(64);
this._K = new Uint32Array([
0x428a2f98,
0x71374491,
0xb5c0fbcf,
0xe9b5dba5,
0x3956c25b,
0x59f111f1,
0x923f82a4,
0xab1c5ed5,
0xd807aa98,
0x12835b01,
0x243185be,
0x550c7dc3,
0x72be5d74,
0x80deb1fe,
0x9bdc06a7,
0xc19bf174,
0xe49b69c1,
0xefbe4786,
0x0fc19dc6,
0x240ca1cc,
0x2de92c6f,
0x4a7484aa,
0x5cb0a9dc,
0x76f988da,
0x983e5152,
0xa831c66d,
0xb00327c8,
0xbf597fc7,
0xc6e00bf3,
0xd5a79147,
0x06ca6351,
0x14292967,
0x27b70a85,
0x2e1b2138,
0x4d2c6dfc,
0x53380d13,
0x650a7354,
0x766a0abb,
0x81c2c92e,
0x92722c85,
0xa2bfe8a1,
0xa81a664b,
0xc24b8b70,
0xc76c51a3,
0xd192e819,
0xd6990624,
0xf40e3585,
0x106aa070,
0x19a4c116,
0x1e376c08,
0x2748774c,
0x34b0bcb5,
0x391c0cb3,
0x4ed8aa4a,
0x5b9cca4f,
0x682e6ff3,
0x748f82ee,
0x78a5636f,
0x84c87814,
0x8cc70208,
0x90befffa,
0xa4506ceb,
0xbef9a3f7,
0xc67178f2
]);
this.init();
}
init() {
this._H = new Uint32Array([
0x6a09e667,
0xbb67ae85,
0x3c6ef372,
0xa54ff53a,
0x510e527f,
0x9b05688c,
0x1f83d9ab,
0x5be0cd19
]);
this._bufIdx = 0;
this._count = new Uint32Array(2);
this._buf.fill(0);
this._finalized = false;
return this;
}
update(msg, inputEncoding) {
if (msg === null) {
throw new TypeError("msg must be a string or Uint8Array.");
}
else if (typeof msg === "string") {
msg = encode(msg, inputEncoding);
}
for (let i = 0, len = msg.length; i < len; i++) {
this._buf[this._bufIdx++] = msg[i];
if (this._bufIdx === 64) {
this._transform();
this._bufIdx = 0;
}
}
const c = this._count;
if ((c[0] += msg.length << 3) < msg.length << 3) {
c[1]++;
}
c[1] += msg.length >>> 29;
return this;
}
digest(outputEncoding) {
if (this._finalized) {
throw new Error("digest has already been called.");
}
this._finalized = true;
const b = this._buf;
let idx = this._bufIdx;
b[idx++] = 0x80;
while (idx !== 56) {
if (idx === 64) {
this._transform();
idx = 0;
}
b[idx++] = 0;
}
const c = this._count;
b[56] = c[1] >>> 24 & 0xff;
b[57] = c[1] >>> 16 & 0xff;
b[58] = c[1] >>> 8 & 0xff;
b[59] = c[1] >>> 0 & 0xff;
b[60] = c[0] >>> 24 & 0xff;
b[61] = c[0] >>> 16 & 0xff;
b[62] = c[0] >>> 8 & 0xff;
b[63] = c[0] >>> 0 & 0xff;
this._transform();
const hash = new Uint8Array(32);
for (let i = 0; i < 8; i++) {
hash[(i << 2) + 0] = this._H[i] >>> 24 & 0xff;
hash[(i << 2) + 1] = this._H[i] >>> 16 & 0xff;
hash[(i << 2) + 2] = this._H[i] >>> 8 & 0xff;
hash[(i << 2) + 3] = this._H[i] >>> 0 & 0xff;
}
this.init();
return outputEncoding ? decode(hash, outputEncoding) : hash;
}
_transform() {
const h = this._H;
let h0 = h[0];
let h1 = h[1];
let h2 = h[2];
let h3 = h[3];
let h4 = h[4];
let h5 = h[5];
let h6 = h[6];
let h7 = h[7];
const w = new Uint32Array(16);
let i;
for (i = 0; i < 16; i++) {
w[i] = this._buf[(i << 2) + 3] | this._buf[(i << 2) + 2] << 8 | this._buf[(i << 2) + 1] << 16 | this._buf[i << 2] << 24;
}
for (i = 0; i < 64; i++) {
let tmp;
if (i < 16) {
tmp = w[i];
}
else {
let a = w[i + 1 & 15];
let b = w[i + 14 & 15];
tmp = w[i & 15] = (a >>> 7 ^ a >>> 18 ^ a >>> 3 ^ a << 25 ^ a << 14) + (b >>> 17 ^ b >>> 19 ^ b >>> 10 ^ b << 15 ^ b << 13) + w[i & 15] + w[i + 9 & 15] | 0;
}
tmp = tmp + h7 + (h4 >>> 6 ^ h4 >>> 11 ^ h4 >>> 25 ^ h4 << 26 ^ h4 << 21 ^ h4 << 7) + (h6 ^ h4 & (h5 ^ h6)) + this._K[i] | 0;
h7 = h6;
h6 = h5;
h5 = h4;
h4 = h3 + tmp;
h3 = h2;
h2 = h1;
h1 = h0;
h0 = tmp + (h1 & h2 ^ h3 & (h1 ^ h2)) + (h1 >>> 2 ^ h1 >>> 13 ^ h1 >>> 22 ^ h1 << 30 ^ h1 << 19 ^ h1 << 10) | 0;
}
h[0] = h[0] + h0 | 0;
h[1] = h[1] + h1 | 0;
h[2] = h[2] + h2 | 0;
h[3] = h[3] + h3 | 0;
h[4] = h[4] + h4 | 0;
h[5] = h[5] + h5 | 0;
h[6] = h[6] + h6 | 0;
h[7] = h[7] + h7 | 0;
}
}
exports.SHA256 = SHA256;
function sha256(msg, inputEncoding, outputEncoding) {
return new SHA256().update(msg, inputEncoding).digest(outputEncoding);
}
exports.sha256 = sha256;
//# sourceMappingURL=sha256.js.map