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/packages/core/src/storage.ts

40 lines
925 B
TypeScript

import { ProviderCrypto } from "./provider";
export class ProviderStorage {
private items: { [algorithmName: string]: ProviderCrypto } = {};
public get(algorithmName: string): ProviderCrypto | null {
return this.items[algorithmName.toLowerCase()] || null;
}
public set(provider: ProviderCrypto) {
this.items[provider.name.toLowerCase()] = provider;
}
public removeAt(algorithmName: string) {
const provider = this.get(algorithmName.toLowerCase());
if (provider) {
delete this.items[algorithmName];
}
return provider;
}
public has(name: string) {
return !!this.get(name);
}
public get length() {
return Object.keys(this.items).length;
}
public get algorithms() {
const algorithms: string[] = [];
for (const key in this.items) {
const provider = this.items[key];
algorithms.push(provider.name);
}
return algorithms.sort();
}
}