UNPKG

@confluentinc/schemaregistry

Version:
119 lines (118 loc) 4.43 kB
"use strict"; /** * Copyright 2020 Google LLC * SPDX-License-Identifier: Apache-2.0 */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.Hmac = void 0; exports.fromRawKey = fromRawKey; const invalid_arguments_exception_1 = require("./exception/invalid_arguments_exception"); const mac_1 = require("./mac"); const Bytes = __importStar(require("./bytes")); const Validators = __importStar(require("./validators")); const crypto = __importStar(require("crypto")); /** * The minimum tag size. * */ const MIN_TAG_SIZE_IN_BYTES = 10; /** * Implementation of HMAC. * */ class Hmac extends mac_1.Mac { /** * @param hash - accepted names are SHA-1, SHA-256 and SHA-512 * @param tagSize - the size of the tag */ constructor(hash, key, tagSize) { super(); this.hash = hash; this.key = key; this.tagSize = tagSize; } /** */ async computeMac(data) { Validators.requireUint8Array(data); const tag = await crypto.subtle.sign({ 'name': 'HMAC', 'hash': { 'name': this.hash } }, this.key, data); return new Uint8Array(tag.slice(0, this.tagSize)); } /** */ async verifyMac(tag, data) { Validators.requireUint8Array(tag); Validators.requireUint8Array(data); const computedTag = await this.computeMac(data); return Bytes.isEqual(tag, computedTag); } } exports.Hmac = Hmac; /** * @param hash - accepted names are SHA-1, SHA-256 and SHA-512 * @param tagSize - the size of the tag */ async function fromRawKey(hash, key, tagSize) { Validators.requireUint8Array(key); if (!Number.isInteger(tagSize)) { throw new invalid_arguments_exception_1.InvalidArgumentsException('invalid tag size, must be an integer'); } if (tagSize < MIN_TAG_SIZE_IN_BYTES) { throw new invalid_arguments_exception_1.InvalidArgumentsException('tag too short, must be at least ' + MIN_TAG_SIZE_IN_BYTES + ' bytes'); } switch (hash) { case 'SHA-1': if (tagSize > 20) { throw new invalid_arguments_exception_1.InvalidArgumentsException('tag too long, must not be larger than 20 bytes'); } break; case 'SHA-256': if (tagSize > 32) { throw new invalid_arguments_exception_1.InvalidArgumentsException('tag too long, must not be larger than 32 bytes'); } break; case 'SHA-512': if (tagSize > 64) { throw new invalid_arguments_exception_1.InvalidArgumentsException('tag too long, must not be larger than 64 bytes'); } break; default: throw new invalid_arguments_exception_1.InvalidArgumentsException(hash + ' is not supported'); } // TODO(b/115974209): Add check that key.length > 16. const cryptoKey = await crypto.subtle.importKey('raw', key, { 'name': 'HMAC', 'hash': { 'name': hash }, 'length': key.length * 8 }, false, ['sign', 'verify']); return new Hmac(hash, cryptoKey, tagSize); }