"use strict";
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.HMACUtilsImpl = void 0;
class HMACUtilsImpl {
    constructor(crypto) {
        this.recommendedKeyLengthBytes = 128;
        this.params = {
            name: 'HMAC',
            hash: 'SHA-512',
        };
        this._crypto = crypto;
    }
    generateKey() {
        return __awaiter(this, void 0, void 0, function* () {
            const generatedKey = yield this._crypto.subtle.generateKey(Object.assign({}, this.params), true, ['sign', 'verify']);
            const exportedKey = yield this._crypto.subtle.exportKey('raw', generatedKey);
            if (exportedKey.byteLength !== this.recommendedKeyLengthBytes) {
                throw new Error(`Generated key has unexpected length - expected ${this.recommendedKeyLengthBytes} bytes, got ${exportedKey.byteLength} bytes`);
            }
            return generatedKey;
        });
    }
    exportKey(key) {
        return __awaiter(this, void 0, void 0, function* () {
            return this._crypto.subtle.exportKey('raw', key);
        });
    }
    importKey(key, requireRecommendedKeySize) {
        return __awaiter(this, void 0, void 0, function* () {
            if (requireRecommendedKeySize != false && key.byteLength !== this.recommendedKeyLengthBytes) {
                throw new Error(`Key has unexpected length - expected ${this.recommendedKeyLengthBytes} bytes, got ${key.byteLength} bytes`);
            }
            return yield this._crypto.subtle.importKey('raw', key, Object.assign(Object.assign({}, this.params), { length: key.byteLength * 8 }), true, ['sign', 'verify']);
        });
    }
    sign(key, data) {
        return __awaiter(this, void 0, void 0, function* () {
            return this._crypto.subtle.sign(Object.assign({}, this.params), key, data);
        });
    }
    verify(key, data, signature) {
        return __awaiter(this, void 0, void 0, function* () {
            return this._crypto.subtle.verify(Object.assign({}, this.params), key, signature, data);
        });
    }
}
exports.HMACUtilsImpl = HMACUtilsImpl;
//# sourceMappingURL=HMACUtils.js.map