@logismika/crypto
Version:
Crypto algorithms library
30 lines (29 loc) • 1.56 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.digestMessage = exports.getOutLength = exports.toByte = void 0;
const consts_1 = require("./consts");
const toByte = (value) => {
if (value < 0 || value >= 256) {
throw new RangeError(`Byte value out of range. Value = ${value}`);
}
return value;
};
exports.toByte = toByte;
const getOutLength = (inLength) => Math.floor(inLength / consts_1.BLOCK_SIZE) * consts_1.BLOCK_SIZE + (inLength % consts_1.BLOCK_SIZE === 0 ? 0 : consts_1.BLOCK_SIZE);
exports.getOutLength = getOutLength;
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);
});
exports.digestMessage = digestMessage;