UNPKG

blockstack

Version:

The Blockstack Javascript library for authentication, identity, and storage.

46 lines 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const cryptoUtils_1 = require("./cryptoUtils"); class NodeCryptoHmacSha256 { constructor(createHmac) { this.createHmac = createHmac; } digest(key, data) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const result = this.createHmac('sha256', key) .update(data) .digest(); return Promise.resolve(result); }); } } exports.NodeCryptoHmacSha256 = NodeCryptoHmacSha256; class WebCryptoHmacSha256 { constructor(subtleCrypto) { this.subtleCrypto = subtleCrypto; } digest(key, data) { return tslib_1.__awaiter(this, void 0, void 0, function* () { const cryptoKey = yield this.subtleCrypto.importKey('raw', key, { name: 'HMAC', hash: 'SHA-256' }, true, ['sign']); const sig = yield this.subtleCrypto.sign( // The `hash` is only specified for non-compliant browsers like Edge. { name: 'HMAC', hash: 'SHA-256' }, cryptoKey, data); return Buffer.from(sig); }); } } exports.WebCryptoHmacSha256 = WebCryptoHmacSha256; function createHmacSha256() { return tslib_1.__awaiter(this, void 0, void 0, function* () { const cryptoLib = yield cryptoUtils_1.getCryptoLib(); if (cryptoLib.name === 'subtleCrypto') { return new WebCryptoHmacSha256(cryptoLib.lib); } else { return new NodeCryptoHmacSha256(cryptoLib.lib.createHmac); } }); } exports.createHmacSha256 = createHmacSha256; //# sourceMappingURL=hmacSha256.js.map