Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import * as graphene from "graphene-pk11";
import { Convert } from "pvtsutils";
import * as core from "webcrypto-core";
import { Crypto } from "../../crypto";
import { CryptoKey } from "../../key";
import * as utils from "../../utils";
import { HmacCryptoKey } from "./key";
export class HmacProvider extends core.HmacProvider {
constructor(private crypto: Crypto) {
super();
}
public async onGenerateKey(algorithm: HmacKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise {
return new Promise((resolve, reject) => {
algorithm.length = algorithm.length
? algorithm.length
: this.getDefaultLength((algorithm.hash as Algorithm).name) >> 3;
const template = this.createTemplate(algorithm, extractable, keyUsages);
template.valueLen = algorithm.length << 3;
// PKCS11 generation
this.crypto.session.generateKey(graphene.KeyGenMechanism.GENERIC_SECRET, template, (err, aesKey) => {
import { JsonParser, JsonSerializer } from "@peculiar/json-schema";
import * as asmCrypto from "asmcrypto.js";
import { Convert } from "pvtsutils";
import * as core from "webcrypto-core";
import { nativeCrypto } from "../../native";
import { HmacCryptoKey } from "./key";
export class HmacProvider extends core.HmacProvider {
public async onGenerateKey(algorithm: HmacKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise {
const length = algorithm.length || this.getDefaultLength((algorithm.hash as Algorithm).name);
// get random bytes for key
const raw = nativeCrypto.getRandomValues(new Uint8Array(length >> 3));
const key = new HmacCryptoKey(algorithm, extractable, keyUsages, raw);
return key;
}
public async onSign(algorithm: Algorithm, key: HmacCryptoKey, data: ArrayBuffer): Promise {
let fn: typeof asmCrypto.HmacSha1 | typeof asmCrypto.HmacSha256 | typeof asmCrypto.HmacSha512;
switch (key.algorithm.hash.name.toUpperCase()) {
case "SHA-1":
import { JsonParser, JsonSerializer } from "@peculiar/json-schema";
import crypto from "crypto";
import * as core from "webcrypto-core";
import { HmacCryptoKey } from "./key";
export class HmacProvider extends core.HmacProvider {
public async onGenerateKey(algorithm: HmacKeyGenParams, extractable: boolean, keyUsages: KeyUsage[]): Promise {
const length = algorithm.length || this.getDefaultLength((algorithm.hash as Algorithm).name);
const key = new HmacCryptoKey();
key.algorithm = {
...algorithm as any,
name: this.name,
};
key.extractable = extractable;
key.usages = keyUsages;
key.data = crypto.randomBytes(length >> 3);
return key;
}
public async onSign(algorithm: Algorithm, key: HmacCryptoKey, data: ArrayBuffer): Promise {