@tbd54566975/crypto
Advanced tools
Comparing version 0.1.6-alpha-20230713-d1dda95 to 0.1.6-alpha-20230728-527e57a
@@ -32,3 +32,2 @@ /** | ||
export declare class AesCtr { | ||
#private; | ||
/** | ||
@@ -75,3 +74,10 @@ * Decrypts the provided data using AES-CTR. | ||
}): Promise<ArrayBuffer>; | ||
/** | ||
* A private method to import a raw key for use with the Web Crypto API. | ||
* | ||
* @param key - The raw key material. | ||
* @returns A Promise that resolves to a CryptoKey. | ||
*/ | ||
private static importKey; | ||
} | ||
//# sourceMappingURL=aes-ctr.d.ts.map |
@@ -33,3 +33,2 @@ /** | ||
export declare class AesGcm { | ||
#private; | ||
/** | ||
@@ -80,3 +79,10 @@ * Decrypts the provided data using AES-GCM. | ||
}): Promise<ArrayBuffer>; | ||
/** | ||
* A private method to import a raw key for use with the Web Crypto API. | ||
* | ||
* @param key - The raw key material. | ||
* @returns A Promise that resolves to a CryptoKey. | ||
*/ | ||
private static importKey; | ||
} | ||
//# sourceMappingURL=aes-gcm.d.ts.map |
@@ -36,4 +36,9 @@ import type { BufferKeyPair } from '../types/index.js'; | ||
export declare class Secp256k1 { | ||
#private; | ||
/** | ||
* A private static field containing a map of hash algorithm names to their | ||
* corresponding hash functions. The map is used in the 'sign' and 'verify' | ||
* methods to get the specified hash function. | ||
*/ | ||
private static hashAlgorithms; | ||
/** | ||
* Generates a secp256k1 key pair. | ||
@@ -40,0 +45,0 @@ * |
@@ -27,3 +27,4 @@ import type { ManagedKey, SignOptions, CryptoManager, ImportableKey, VerifyOptions, DecryptOptions, EncryptOptions, ManagedKeyPair, GenerateKeyType, DeriveBitsOptions, ImportableKeyPair, GenerateKeyOptions, KeyManagementSystem, GenerateKeyOptionTypes } from '../types/index.js'; | ||
export declare class KeyManager implements CryptoManager { | ||
#private; | ||
private kms; | ||
private keyStore; | ||
constructor(options: KeyManagerOptions); | ||
@@ -44,3 +45,5 @@ decrypt(options: DecryptOptions): Promise<ArrayBuffer>; | ||
verify(options: VerifyOptions): Promise<boolean>; | ||
private getKms; | ||
private useLocalKms; | ||
} | ||
//# sourceMappingURL=key-manager.d.ts.map |
@@ -13,3 +13,3 @@ import type { ManagedKeyStore, ManagedKey, ManagedKeyPair } from '../types/index.js'; | ||
export declare class KeyManagerStore implements ManagedKeyStore<string, ManagedKey | ManagedKeyPair> { | ||
#private; | ||
private store; | ||
constructor(options: { | ||
@@ -16,0 +16,0 @@ store: MemoryStore<string, ManagedKey | ManagedKeyPair>; |
@@ -14,3 +14,3 @@ import type { ManagedKeyStore, ManagedKey, ManagedKeyPair, ManagedPrivateKey } from '../types/index.js'; | ||
export declare class KmsKeyStore implements ManagedKeyStore<string, ManagedKey | ManagedKeyPair> { | ||
#private; | ||
private keyStore; | ||
constructor(keyStore: MemoryStore<string, ManagedKey | ManagedKeyPair>); | ||
@@ -39,3 +39,3 @@ deleteKey({ id }: { | ||
export declare class KmsPrivateKeyStore implements ManagedKeyStore<string, ManagedPrivateKey> { | ||
#private; | ||
private keyStore; | ||
constructor(keyStore: MemoryStore<string, ManagedPrivateKey>); | ||
@@ -42,0 +42,0 @@ deleteKey({ id }: { |
@@ -8,3 +8,6 @@ import type { AlgorithmImplementations } from './supported-algorithms.js'; | ||
export declare class LocalKms implements KeyManagementSystem { | ||
#private; | ||
private name; | ||
private keyStore; | ||
private privateKeyStore; | ||
private supportedAlgorithms; | ||
constructor(kmsName: string, keyStore: KmsKeyStore, privateKeyStore: KmsPrivateKeyStore, options?: KmsOptions); | ||
@@ -22,3 +25,7 @@ decrypt(options: DecryptOptions): Promise<ArrayBuffer>; | ||
verify(options: VerifyOptions): Promise<boolean>; | ||
private getAlgorithm; | ||
private registerSupportedAlgorithms; | ||
private toCryptoKey; | ||
private toManagedKey; | ||
} | ||
//# sourceMappingURL=kms-local.d.ts.map |
export type * from './types.js'; | ||
export type * from './types/index.js'; | ||
export * as ed25519 from './ed25519.js'; | ||
export type * from './types/index.js'; | ||
export * from './utils-new.js'; | ||
@@ -5,0 +5,0 @@ export * from './kms-local/index.js'; |
{ | ||
"name": "@tbd54566975/crypto", | ||
"version": "0.1.6-alpha-20230713-d1dda95", | ||
"version": "0.1.6-alpha-20230728-527e57a", | ||
"description": "TBD crypto library", | ||
"type": "module", | ||
"main": "./dist/cjs/main.cjs", | ||
"module": "./dist/esm/main.mjs", | ||
"main": "./dist/cjs/main.js", | ||
"module": "./dist/esm/main.js", | ||
"types": "./dist/types/main.d.ts", | ||
"scripts": { | ||
"build": "rimraf dist && node build/bundles.js && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json && tsc", | ||
"clean": "rimraf dist tests/compiled", | ||
"build:esm": "rimraf dist/esm dist/types && npx tsc -p tsconfig.json", | ||
"build:cjs": "rimraf dist/cjs && tsc -p tsconfig.cjs.json && echo '{\"type\": \"commonjs\"}' > ./dist/cjs/package.json", | ||
"build:browser": "rimraf dist/browser.mjs dist/browser.js && node build/bundles.js", | ||
"build": "npm run clean && npm run build:esm && npm run build:cjs && npm run build:browser", | ||
"lint": "eslint . --ext .ts --max-warnings 0", | ||
"lint:fix": "eslint . --ext .ts --fix", | ||
"test:node": "rimraf __tests__ && tsc -p tsconfig.test.json && c8 mocha", | ||
"test:node": "rimraf tests/compiled && tsc -p tests/tsconfig.json && c8 mocha", | ||
"test:browser": "karma start karma.conf.cjs" | ||
@@ -44,27 +48,11 @@ }, | ||
".": { | ||
"import": "./dist/esm/main.mjs", | ||
"require": "./dist/cjs/main.cjs", | ||
"import": "./dist/esm/main.js", | ||
"require": "./dist/cjs/main.js", | ||
"types": "./dist/types/main.d.ts" | ||
}, | ||
"./browser": { | ||
"import": "./dist/browser.mjs", | ||
"require": "./dist/browser.js", | ||
"types": "./dist/types/main.d.ts" | ||
}, | ||
"./electron": { | ||
"import": "./dist/esm/main.mjs", | ||
"require": "./dist/electron/main.cjs", | ||
"types": "./dist/types/main.d.ts" | ||
} | ||
}, | ||
"browser": { | ||
"./dist/esm/main.mjs": "./dist/browser.mjs", | ||
"./dist/cjs/main.cjs": "./dist/browser.js", | ||
"types": "./dist/types/main.d.ts" | ||
"node:crypto": false | ||
}, | ||
"react-native": { | ||
"./dist/esm/main.mjs": "./dist/esm/main.mjs", | ||
"./dist/cjs/main.cjs": "./dist/esm/main.mjs", | ||
"types": "./dist/types/main.d.ts" | ||
}, | ||
"react-native": "./dist/esm/main.js", | ||
"keywords": [ | ||
@@ -115,8 +103,7 @@ "cryptography", | ||
"mocha": "10.2.0", | ||
"node-stdlib-browser": "1.2.0", | ||
"rimraf": "4.4.0", | ||
"sinon": "15.0.2", | ||
"source-map-loader": "4.0.1", | ||
"typescript": "5.0.4" | ||
"typescript": "5.1.6" | ||
} | ||
} |
@@ -52,3 +52,3 @@ import { crypto } from '@noble/hashes/crypto'; | ||
const webCryptoKey = await this.#importKey(key); | ||
const webCryptoKey = await this.importKey(key); | ||
@@ -82,3 +82,3 @@ const ciphertext = await crypto.subtle.decrypt( | ||
const webCryptoKey = await this.#importKey(key); | ||
const webCryptoKey = await this.importKey(key); | ||
@@ -118,3 +118,3 @@ const plaintext = await crypto.subtle.encrypt( | ||
*/ | ||
static async #importKey(key: ArrayBuffer): Promise<CryptoKey> { | ||
private static async importKey(key: ArrayBuffer): Promise<CryptoKey> { | ||
return crypto.subtle.importKey( | ||
@@ -121,0 +121,0 @@ 'raw', |
@@ -55,3 +55,3 @@ import { crypto } from '@noble/hashes/crypto'; | ||
const webCryptoKey = await this.#importKey(key); | ||
const webCryptoKey = await this.importKey(key); | ||
@@ -87,3 +87,3 @@ const algorithm = (additionalData === undefined) | ||
const webCryptoKey = await this.#importKey(key); | ||
const webCryptoKey = await this.importKey(key); | ||
@@ -123,3 +123,3 @@ const algorithm = (additionalData === undefined) | ||
*/ | ||
static async #importKey(key: ArrayBuffer): Promise<CryptoKey> { | ||
private static async importKey(key: ArrayBuffer): Promise<CryptoKey> { | ||
return crypto.subtle.importKey( | ||
@@ -126,0 +126,0 @@ 'raw', |
@@ -47,3 +47,3 @@ import type { BufferKeyPair } from '../types/index.js'; | ||
*/ | ||
static #hashAlgorithms: Record<string, HashFunction> = { | ||
private static hashAlgorithms: Record<string, HashFunction> = { | ||
'SHA-256': sha256 | ||
@@ -157,3 +157,3 @@ }; | ||
// Generate a digest of the data using the specified hash function. | ||
const hashFunction = this.#hashAlgorithms[hash]; | ||
const hashFunction = this.hashAlgorithms[hash]; | ||
const digest = hashFunction(dataU8A); | ||
@@ -201,3 +201,3 @@ | ||
// Generate a digest of the data using the specified hash function. | ||
const hashFunction = this.#hashAlgorithms[hash]; | ||
const hashFunction = this.hashAlgorithms[hash]; | ||
const digest = hashFunction(dataU8A); | ||
@@ -204,0 +204,0 @@ |
@@ -53,12 +53,12 @@ import type { | ||
// KMS name to KeyManagementSystem mapping | ||
#kms: Map<string, KeyManagementSystem>; | ||
private kms: Map<string, KeyManagementSystem>; | ||
// Store for managed key metadata. | ||
#keyStore: KeyManagerStore; | ||
private keyStore: KeyManagerStore; | ||
constructor(options: KeyManagerOptions) { | ||
checkRequiredProperty({ property: 'store', inObject: options }); | ||
this.#keyStore = options.store; | ||
this.keyStore = options.store; | ||
options.kms ??= this.#useLocalKms(); | ||
this.#kms = new Map(Object.entries(options.kms)) ; | ||
options.kms ??= this.useLocalKms(); | ||
this.kms = new Map(Object.entries(options.kms)) ; | ||
} | ||
@@ -76,3 +76,3 @@ | ||
const kmsName = key.kms; | ||
const kms = this.#getKms(kmsName); | ||
const kms = this.getKms(kmsName); | ||
@@ -95,3 +95,3 @@ const keyId = key.id; | ||
const kmsName = ownKeyPair.privateKey.kms; | ||
const kms = this.#getKms(kmsName); | ||
const kms = this.getKms(kmsName); | ||
@@ -114,3 +114,3 @@ const ownKeyId = ownKeyPair.privateKey.id; | ||
const kmsName = key.kms; | ||
const kms = this.#getKms(kmsName); | ||
const kms = this.getKms(kmsName); | ||
@@ -126,3 +126,3 @@ const keyId = key.id; | ||
const kms = this.#getKms(kmsName); | ||
const kms = this.getKms(kmsName); | ||
@@ -132,3 +132,3 @@ const keyOrKeyPair = await kms.generateKey(generateKeyOptions); | ||
// Store the ManagedKey or ManagedKeyPair in KeyManager's key store. | ||
await this.#keyStore.importKey({ key: keyOrKeyPair }); | ||
await this.keyStore.importKey({ key: keyOrKeyPair }); | ||
@@ -139,3 +139,3 @@ return keyOrKeyPair; | ||
async getKey(options: { keyRef: string; }): Promise<ManagedKey | ManagedKeyPair | undefined> { | ||
const keyOrKeyPair = this.#keyStore.getKey({ id: options.keyRef }); | ||
const keyOrKeyPair = this.keyStore.getKey({ id: options.keyRef }); | ||
return keyOrKeyPair; | ||
@@ -148,3 +148,3 @@ } | ||
const kmsName = ('privateKey' in options) ? options.privateKey.kms : options.kms; | ||
const kms = this.#getKms(kmsName); | ||
const kms = this.getKms(kmsName); | ||
@@ -154,3 +154,3 @@ const importedKeyOrKeyPair = await kms.importKey(options); | ||
// Store the ManagedKey or ManagedKeyPair in KeyManager's key store. | ||
await this.#keyStore.importKey({ key: importedKeyOrKeyPair }); | ||
await this.keyStore.importKey({ key: importedKeyOrKeyPair }); | ||
@@ -161,3 +161,3 @@ return importedKeyOrKeyPair; | ||
listKms() { | ||
return Array.from(this.#kms.keys()); | ||
return Array.from(this.kms.keys()); | ||
} | ||
@@ -175,3 +175,3 @@ | ||
const kmsName = keyPair.privateKey.kms; | ||
const kms = this.#getKms(kmsName); | ||
const kms = this.getKms(kmsName); | ||
@@ -194,3 +194,3 @@ const keyId = keyPair.privateKey.id; | ||
const kmsName = keyPair.publicKey.kms; | ||
const kms = this.#getKms(kmsName); | ||
const kms = this.getKms(kmsName); | ||
@@ -203,8 +203,8 @@ const keyId = keyPair.publicKey.id; | ||
#getKms(name: string | undefined): KeyManagementSystem { | ||
private getKms(name: string | undefined): KeyManagementSystem { | ||
// For developer convenience, if a KMS name isn't specified and KeyManager only has | ||
// one KMS defined, use it. Otherwise, an exception will be thrown. | ||
name ??= (this.#kms.size === 1) ? this.#kms.keys().next().value : ''; | ||
name ??= (this.kms.size === 1) ? this.kms.keys().next().value : ''; | ||
const kms = this.#kms.get(name!); | ||
const kms = this.kms.get(name!); | ||
@@ -218,3 +218,3 @@ if (!kms) { | ||
#useLocalKms(): KmsMap { | ||
private useLocalKms(): KmsMap { | ||
// Instantiate local in-memory store for KMS key metadata and public keys. | ||
@@ -221,0 +221,0 @@ const kmsMemoryStore = new MemoryStore<string, ManagedKey | ManagedKeyPair>(); |
@@ -17,11 +17,11 @@ import type { ManagedKeyStore, ManagedKey, ManagedKeyPair } from '../types/index.js'; | ||
export class KeyManagerStore implements ManagedKeyStore<string, ManagedKey | ManagedKeyPair> { | ||
#store: MemoryStore<string, ManagedKey | ManagedKeyPair>; | ||
private store: MemoryStore<string, ManagedKey | ManagedKeyPair>; | ||
constructor(options: { store: MemoryStore<string, ManagedKey | ManagedKeyPair> }) { | ||
this.#store = options.store; | ||
this.store = options.store; | ||
} | ||
async deleteKey({ id }: { id: string }) { | ||
if (await this.#store.has(id)) { | ||
await this.#store.delete(id); | ||
if (await this.store.has(id)) { | ||
await this.store.delete(id); | ||
return true; | ||
@@ -34,3 +34,3 @@ } else { | ||
async getKey({ id }: { id: string }): Promise<ManagedKey | ManagedKeyPair | undefined> { | ||
return this.#store.get(id); | ||
return this.store.get(id); | ||
} | ||
@@ -40,3 +40,3 @@ | ||
const id = isManagedKeyPair(key) ? key.publicKey!.id : key.id; | ||
if (await this.#store.has(id)) { | ||
if (await this.store.has(id)) { | ||
throw new Error(`Key with ID already exists: '${id}'`); | ||
@@ -47,3 +47,3 @@ } | ||
const clonedKey = structuredClone(key); | ||
await this.#store.set(id, clonedKey ); | ||
await this.store.set(id, clonedKey ); | ||
@@ -54,4 +54,4 @@ return true; | ||
async listKeys(): Promise<Array<ManagedKey | ManagedKeyPair>> { | ||
return this.#store.list(); | ||
return this.store.list(); | ||
} | ||
} |
@@ -19,11 +19,11 @@ import type { ManagedKeyStore, ManagedKey, ManagedKeyPair, ManagedPrivateKey } from '../types/index.js'; | ||
export class KmsKeyStore implements ManagedKeyStore<string, ManagedKey | ManagedKeyPair> { | ||
#keyStore: MemoryStore<string, ManagedKey | ManagedKeyPair>; | ||
private keyStore: MemoryStore<string, ManagedKey | ManagedKeyPair>; | ||
constructor(keyStore: MemoryStore<string, ManagedKey | ManagedKeyPair>) { | ||
this.#keyStore = keyStore; | ||
this.keyStore = keyStore; | ||
} | ||
async deleteKey({ id }: { id: string }) { | ||
if (await this.#keyStore.has(id)) { | ||
await this.#keyStore.delete(id); | ||
if (await this.keyStore.has(id)) { | ||
await this.keyStore.delete(id); | ||
return true; | ||
@@ -36,3 +36,3 @@ } else { | ||
async getKey({ id }: { id: string }): Promise<ManagedKey | ManagedKeyPair | undefined> { | ||
return this.#keyStore.get(id); | ||
return this.keyStore.get(id); | ||
} | ||
@@ -49,3 +49,3 @@ | ||
if (await this.#keyStore.has(id)) { | ||
if (await this.keyStore.has(id)) { | ||
throw new Error(`Key with ID already exists: '${id}'`); | ||
@@ -56,3 +56,3 @@ } | ||
const clonedKey = structuredClone(key); | ||
await this.#keyStore.set(id, clonedKey); | ||
await this.keyStore.set(id, clonedKey); | ||
return id; | ||
@@ -62,3 +62,3 @@ } | ||
async listKeys(): Promise<Array<ManagedKey | ManagedKeyPair>> { | ||
return this.#keyStore.list(); | ||
return this.keyStore.list(); | ||
} | ||
@@ -78,11 +78,11 @@ } | ||
export class KmsPrivateKeyStore implements ManagedKeyStore<string, ManagedPrivateKey> { | ||
#keyStore: MemoryStore<string, ManagedPrivateKey>; | ||
private keyStore: MemoryStore<string, ManagedPrivateKey>; | ||
constructor(keyStore: MemoryStore<string, ManagedPrivateKey>) { | ||
this.#keyStore = keyStore; | ||
this.keyStore = keyStore; | ||
} | ||
async deleteKey({ id }: { id: string }) { | ||
if (await this.#keyStore.has(id)) { | ||
await this.#keyStore.delete(id); | ||
if (await this.keyStore.has(id)) { | ||
await this.keyStore.delete(id); | ||
return true; | ||
@@ -95,3 +95,3 @@ } else { | ||
async getKey({ id }: { id: string }): Promise<ManagedPrivateKey | undefined> { | ||
return this.#keyStore.get(id); | ||
return this.keyStore.get(id); | ||
} | ||
@@ -108,3 +108,3 @@ | ||
clonedKey.id = randomUuid(); | ||
await this.#keyStore.set(clonedKey.id, clonedKey); | ||
await this.keyStore.set(clonedKey.id, clonedKey); | ||
@@ -115,4 +115,4 @@ return clonedKey.id; | ||
async listKeys(): Promise<Array<ManagedPrivateKey>> { | ||
return this.#keyStore.list(); | ||
return this.keyStore.list(); | ||
} | ||
} |
@@ -34,15 +34,15 @@ import type { RequireOnly } from '@tbd54566975/common'; | ||
export class LocalKms implements KeyManagementSystem { | ||
#name: string; | ||
#keyStore: KmsKeyStore; | ||
#privateKeyStore: KmsPrivateKeyStore; | ||
#supportedAlgorithms: Map<string, AlgorithmImplementation> = new Map(); | ||
private name: string; | ||
private keyStore: KmsKeyStore; | ||
private privateKeyStore: KmsPrivateKeyStore; | ||
private supportedAlgorithms: Map<string, AlgorithmImplementation> = new Map(); | ||
constructor(kmsName: string, keyStore: KmsKeyStore, privateKeyStore: KmsPrivateKeyStore, options: KmsOptions = {}) { | ||
this.#name = kmsName; | ||
this.#keyStore = keyStore; | ||
this.#privateKeyStore = privateKeyStore; | ||
this.name = kmsName; | ||
this.keyStore = keyStore; | ||
this.privateKeyStore = privateKeyStore; | ||
// Merge the default and custom algorithms and register with the KMS. | ||
const cryptoAlgorithms = {...defaultAlgorithms, ...options.cryptoAlgorithms}; | ||
this.#registerSupportedAlgorithms(cryptoAlgorithms); | ||
this.registerSupportedAlgorithms(cryptoAlgorithms); | ||
} | ||
@@ -57,10 +57,10 @@ | ||
if (isManagedKey(key)) { | ||
const privateManagedKey = await this.#privateKeyStore.getKey({ id: key.id }); | ||
const privateManagedKey = await this.privateKeyStore.getKey({ id: key.id }); | ||
if (privateManagedKey !== undefined) { | ||
// Construct a CryptoKey object from the key metadata and private key material. | ||
const privateCryptoKey = this.#toCryptoKey({ ...key, material: privateManagedKey.material }); | ||
const privateCryptoKey = this.toCryptoKey({ ...key, material: privateManagedKey.material }); | ||
// Decrypt the data. | ||
const cryptoAlgorithm = this.#getAlgorithm(algorithm); | ||
const cryptoAlgorithm = this.getAlgorithm(algorithm); | ||
const plaintext = cryptoAlgorithm.decrypt({ algorithm, key: privateCryptoKey, data }); | ||
@@ -82,10 +82,10 @@ | ||
if (isManagedKeyPair(ownKeyPair)) { | ||
const privateManagedKey = await this.#privateKeyStore.getKey({ id: ownKeyPair.privateKey.id }); | ||
const privateManagedKey = await this.privateKeyStore.getKey({ id: ownKeyPair.privateKey.id }); | ||
if (privateManagedKey !== undefined) { | ||
// Construct a CryptoKey object from the key metadata and private key material. | ||
const privateCryptoKey = this.#toCryptoKey({ ...ownKeyPair.privateKey, material: privateManagedKey.material }); | ||
const privateCryptoKey = this.toCryptoKey({ ...ownKeyPair.privateKey, material: privateManagedKey.material }); | ||
// Derive the shared secret. | ||
const cryptoAlgorithm = this.#getAlgorithm(algorithm); | ||
const cryptoAlgorithm = this.getAlgorithm(algorithm); | ||
const sharedSecret = cryptoAlgorithm.deriveBits({ algorithm, baseKey: privateCryptoKey, length: length ?? null }); | ||
@@ -107,10 +107,10 @@ | ||
if (isManagedKey(key)) { | ||
const privateManagedKey = await this.#privateKeyStore.getKey({ id: key.id }); | ||
const privateManagedKey = await this.privateKeyStore.getKey({ id: key.id }); | ||
if (privateManagedKey !== undefined) { | ||
// Construct a CryptoKey object from the key metadata and private key material. | ||
const privateCryptoKey = this.#toCryptoKey({ ...key, material: privateManagedKey.material }); | ||
const privateCryptoKey = this.toCryptoKey({ ...key, material: privateManagedKey.material }); | ||
// Encrypt the data. | ||
const cryptoAlgorithm = this.#getAlgorithm(algorithm); | ||
const cryptoAlgorithm = this.getAlgorithm(algorithm); | ||
const ciphertext = cryptoAlgorithm.encrypt({ algorithm, key: privateCryptoKey, data }); | ||
@@ -129,3 +129,3 @@ | ||
// Get crypto algorithm implementation. | ||
const cryptoAlgorithm = this.#getAlgorithm(algorithm); | ||
const cryptoAlgorithm = this.getAlgorithm(algorithm); | ||
@@ -140,14 +140,14 @@ // Generate the key. | ||
const privateKeyType = cryptoKey.privateKey.type as Web5Crypto.PrivateKeyType; | ||
const id = await this.#privateKeyStore.importKey({ key: { material: cryptoKey.privateKey.handle, type: privateKeyType} }); | ||
const privateKey = this.#toManagedKey({ ...cryptoKey.privateKey, id, alias, metadata }); | ||
const publicKey = this.#toManagedKey({ ...cryptoKey.publicKey, material: cryptoKey.publicKey.handle, id, alias, metadata }); | ||
const id = await this.privateKeyStore.importKey({ key: { material: cryptoKey.privateKey.handle, type: privateKeyType} }); | ||
const privateKey = this.toManagedKey({ ...cryptoKey.privateKey, id, alias, metadata }); | ||
const publicKey = this.toManagedKey({ ...cryptoKey.publicKey, material: cryptoKey.publicKey.handle, id, alias, metadata }); | ||
managedKeyOrKeyPair = { privateKey, publicKey } as GenerateKeyType<T>; | ||
} else { | ||
const keyType = cryptoKey.type as Web5Crypto.PrivateKeyType; | ||
const id = await this.#privateKeyStore.importKey({ key: { material: cryptoKey.handle, type: keyType } }); | ||
managedKeyOrKeyPair = this.#toManagedKey({ ...cryptoKey, id, alias, metadata }) as GenerateKeyType<T>; | ||
const id = await this.privateKeyStore.importKey({ key: { material: cryptoKey.handle, type: keyType } }); | ||
managedKeyOrKeyPair = this.toManagedKey({ ...cryptoKey, id, alias, metadata }) as GenerateKeyType<T>; | ||
} | ||
// Store the ManagedKey or ManagedKeyPair in the KMS key store. | ||
await this.#keyStore.importKey({ key: managedKeyOrKeyPair }); | ||
await this.keyStore.importKey({ key: managedKeyOrKeyPair }); | ||
@@ -158,3 +158,3 @@ return managedKeyOrKeyPair; | ||
async getKey(options: { keyRef: string }): Promise<ManagedKey | ManagedKeyPair | undefined> { | ||
const keyOrKeyPair = this.#keyStore.getKey({ id: options.keyRef }); | ||
const keyOrKeyPair = this.keyStore.getKey({ id: options.keyRef }); | ||
return keyOrKeyPair; | ||
@@ -176,8 +176,8 @@ } | ||
publicKey.material = Convert.bufferSource(publicKey.material).toArrayBuffer(); | ||
const id = await this.#privateKeyStore.importKey({ key: { material: privateKey.material, type: privateKey.type } }); | ||
const id = await this.privateKeyStore.importKey({ key: { material: privateKey.material, type: privateKey.type } }); | ||
const managedKeyPair = { | ||
privateKey : this.#toManagedKey({ ...privateKey, material: undefined, id }), | ||
publicKey : this.#toManagedKey({ ...publicKey, material: publicKey.material, id }) | ||
privateKey : this.toManagedKey({ ...privateKey, material: undefined, id }), | ||
publicKey : this.toManagedKey({ ...publicKey, material: publicKey.material, id }) | ||
}; | ||
await this.#keyStore.importKey({ key: managedKeyPair }); | ||
await this.keyStore.importKey({ key: managedKeyPair }); | ||
return managedKeyPair; | ||
@@ -192,5 +192,5 @@ } | ||
material = Convert.bufferSource(material).toArrayBuffer(); | ||
const id = await this.#privateKeyStore.importKey({ key: { material, type: keyType } }); | ||
const privateManagedKey = this.#toManagedKey({ ...options, material: undefined, id }); | ||
await this.#keyStore.importKey({ key: privateManagedKey }); | ||
const id = await this.privateKeyStore.importKey({ key: { material, type: keyType } }); | ||
const privateManagedKey = this.toManagedKey({ ...options, material: undefined, id }); | ||
await this.keyStore.importKey({ key: privateManagedKey }); | ||
return privateManagedKey; | ||
@@ -203,4 +203,4 @@ } | ||
material = Convert.bufferSource(material).toArrayBuffer(); | ||
const privateManagedKey = this.#toManagedKey({ ...options, material, id: 'placeholder' }); | ||
privateManagedKey.id = await this.#keyStore.importKey({ key: privateManagedKey }); | ||
const privateManagedKey = this.toManagedKey({ ...options, material, id: 'placeholder' }); | ||
privateManagedKey.id = await this.keyStore.importKey({ key: privateManagedKey }); | ||
return privateManagedKey; | ||
@@ -213,5 +213,5 @@ } | ||
material = Convert.bufferSource(material).toArrayBuffer(); | ||
const id = await this.#privateKeyStore.importKey({ key: { material, type: keyType } }); | ||
const secretManagedKey = this.#toManagedKey({ ...options, material: undefined, id }); | ||
await this.#keyStore.importKey({ key: secretManagedKey }); | ||
const id = await this.privateKeyStore.importKey({ key: { material, type: keyType } }); | ||
const secretManagedKey = this.toManagedKey({ ...options, material: undefined, id }); | ||
await this.keyStore.importKey({ key: secretManagedKey }); | ||
return secretManagedKey; | ||
@@ -232,10 +232,10 @@ } | ||
if (isManagedKeyPair(keyPair)) { | ||
const privateManagedKey = await this.#privateKeyStore.getKey({ id: keyPair.privateKey.id }); | ||
const privateManagedKey = await this.privateKeyStore.getKey({ id: keyPair.privateKey.id }); | ||
if (privateManagedKey !== undefined) { | ||
// Construct a CryptoKey object from the key metadata and private key material. | ||
const privateCryptoKey = this.#toCryptoKey({ ...keyPair.privateKey, material: privateManagedKey.material }); | ||
const privateCryptoKey = this.toCryptoKey({ ...keyPair.privateKey, material: privateManagedKey.material }); | ||
// Sign the data. | ||
const cryptoAlgorithm = this.#getAlgorithm(algorithm); | ||
const cryptoAlgorithm = this.getAlgorithm(algorithm); | ||
const signature = cryptoAlgorithm.sign({ algorithm, key: privateCryptoKey, data }); | ||
@@ -258,6 +258,6 @@ | ||
// Construct a CryptoKey object from the key metadata and private key material. | ||
const publicCryptoKey = this.#toCryptoKey({ ...keyPair.publicKey }); | ||
const publicCryptoKey = this.toCryptoKey({ ...keyPair.publicKey }); | ||
// Verify the signature and data. | ||
const cryptoAlgorithm = this.#getAlgorithm(algorithm); | ||
const cryptoAlgorithm = this.getAlgorithm(algorithm); | ||
const isValid = cryptoAlgorithm.verify({ algorithm, key: publicCryptoKey, signature, data }); | ||
@@ -271,5 +271,5 @@ | ||
#getAlgorithm(algorithmIdentifier: Web5Crypto.AlgorithmIdentifier): CryptoAlgorithm { | ||
private getAlgorithm(algorithmIdentifier: Web5Crypto.AlgorithmIdentifier): CryptoAlgorithm { | ||
checkRequiredProperty({ property: 'name', inObject: algorithmIdentifier }); | ||
const algorithm = this.#supportedAlgorithms.get(algorithmIdentifier.name.toUpperCase()); | ||
const algorithm = this.supportedAlgorithms.get(algorithmIdentifier.name.toUpperCase()); | ||
@@ -283,11 +283,11 @@ if (algorithm === undefined) { | ||
#registerSupportedAlgorithms(cryptoAlgorithms: AlgorithmImplementations): void { | ||
private registerSupportedAlgorithms(cryptoAlgorithms: AlgorithmImplementations): void { | ||
for (const [name, implementation] of Object.entries(cryptoAlgorithms)) { | ||
// Add the algorithm name and its implementation to the supported algorithms map, | ||
// upper-cased to allow for case-insensitive. | ||
this.#supportedAlgorithms.set(name.toUpperCase(), implementation); | ||
this.supportedAlgorithms.set(name.toUpperCase(), implementation); | ||
} | ||
} | ||
#toCryptoKey(managedKey: ManagedKey): Web5Crypto.CryptoKey { | ||
private toCryptoKey(managedKey: ManagedKey): Web5Crypto.CryptoKey { | ||
if (!managedKey.material) { | ||
@@ -308,3 +308,3 @@ throw new Error(`Required property missing: 'material'`); | ||
#toManagedKey(options: Omit<Web5Crypto.CryptoKey, 'handle'> & RequireOnly<ManagedKey, 'id'>): ManagedKey { | ||
private toManagedKey(options: Omit<Web5Crypto.CryptoKey, 'handle'> & RequireOnly<ManagedKey, 'id'>): ManagedKey { | ||
const managedKey: ManagedKey = { | ||
@@ -315,3 +315,3 @@ id : options.id, | ||
extractable : options.extractable, | ||
kms : this.#name, | ||
kms : this.name, | ||
material : (options.type === 'public') ? options.material : undefined, | ||
@@ -318,0 +318,0 @@ metadata : options.metadata, |
export type * from './types.js'; | ||
export type * from './types/index.js'; | ||
export * as ed25519 from './ed25519.js'; | ||
export type * from './types/index.js'; | ||
export * from './utils-new.js'; | ||
@@ -7,0 +7,0 @@ export * from './kms-local/index.js'; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 2 instances in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
29
208
6
1
2181075
11368