This repository has been archived on 2023-04-04. You can view files and clone it, but cannot push or open issues or pull requests.
webcrypto/src/mechs/storage.ts

27 lines
616 B
TypeScript

import * as core from "webcrypto-core";
import { CryptoKey as InternalCryptoKey } from "../keys";
const keyStorage = new WeakMap<core.CryptoKey, InternalCryptoKey>();
export function getCryptoKey(key: core.CryptoKey) {
const res = keyStorage.get(key);
if (!res) {
throw new core.OperationError("Cannot get CryptoKey from secure storage");
}
return res;
}
export function setCryptoKey(value: InternalCryptoKey) {
const key = core.CryptoKey.create(
value.algorithm,
value.type,
value.extractable,
value.usages
);
Object.freeze(key);
keyStorage.set(key, value);
return key;
}