UNPKG

@beenotung/tslib

Version:
32 lines (31 loc) 933 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hashAsync = hashAsync; async function hashAsync(message, options = {}) { let algorithm = options.algorithm; if (!algorithm || algorithm == 'sha256') { algorithm = 'SHA-256'; } const outputFormat = options.outputFormat || 'hex'; const input = typeof message === 'string' ? new TextEncoder().encode(message) : message; const digest = await crypto.subtle.digest(algorithm, input); if (outputFormat == 'ArrayBuffer') { return digest; } const hash = new Uint8Array(digest); if (outputFormat == 'Uint8Array') { return hash; } let hex = ''; for (let i = 0; i < 32; i++) { const byte = hash[i]; const str = byte.toString(16); if (byte < 16) { hex += '0' + str; } else { hex += str; } } return hex; }