@peculiar/webcrypto
Version:
A WebCrypto Polyfill for NodeJS
46 lines (45 loc) • 1.5 kB
JavaScript
/**
* Copyright (c) 2019-2026, Peculiar Ventures
* SPDX-License-Identifier: MIT
*/
import { getCryptoKey, setCryptoKey } from "../storage.js";
import { EcPrivateKey } from "./private_key.js";
import { EcPublicKey } from "./public_key.js";
import { EcCrypto } from "./crypto.js";
import * as core from "webcrypto-core";
// src/mechs/ec/ec_dh.ts
var EcdhProvider = class extends core.EcdhProvider {
namedCurves = core.EcCurves.names;
async onGenerateKey(algorithm, extractable, keyUsages) {
const keys = await EcCrypto.generateKey({
...algorithm,
name: this.name
}, extractable, keyUsages);
return {
privateKey: setCryptoKey(keys.privateKey),
publicKey: setCryptoKey(keys.publicKey)
};
}
async onExportKey(format, key) {
return EcCrypto.exportKey(format, getCryptoKey(key));
}
async onImportKey(format, keyData, algorithm, extractable, keyUsages) {
const key = await EcCrypto.importKey(format, keyData, {
...algorithm,
name: this.name
}, extractable, keyUsages);
return setCryptoKey(key);
}
checkCryptoKey(key, keyUsage) {
super.checkCryptoKey(key, keyUsage);
const internalKey = getCryptoKey(key);
if (!(internalKey instanceof EcPrivateKey || internalKey instanceof EcPublicKey)) throw new TypeError("key: Is not EC CryptoKey");
}
async onDeriveBits(algorithm, baseKey, length) {
return await EcCrypto.deriveBits({
...algorithm,
public: getCryptoKey(algorithm.public)
}, getCryptoKey(baseKey), length);
}
};
export { EcdhProvider };