UNPKG

@logismika/crypto

Version:
24 lines (23 loc) 1.3 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { BLOCK_SIZE } from "./consts"; export const toByte = (value) => { if (value < 0 || value >= 256) { throw new RangeError(`Byte value out of range. Value = ${value}`); } return value; }; export const getOutLength = (inLength) => Math.floor(inLength / BLOCK_SIZE) * BLOCK_SIZE + (inLength % BLOCK_SIZE === 0 ? 0 : BLOCK_SIZE); export const digestMessage = (value) => __awaiter(void 0, void 0, void 0, function* () { const encoder = new TextEncoder(); const data = encoder.encode(value); const hash = yield crypto.subtle.digest("SHA-256", data); return new Uint8Array(hash); });