UNPKG

@microsoft/useragent-sdk

Version:

SDK for building decentralized identity wallets and enterprise agents.

85 lines (84 loc) 3.52 kB
import ISubtleCryptoExtension from './ISubtleCryptoExtension'; import { CryptoAlgorithm } from '../keyStore/IKeyStore'; import CryptoFactory from './CryptoFactory'; import PublicKey from '../keys/PublicKey'; import PrivateKey from '../keys/PrivateKey'; import { SubtleCrypto } from 'webcrypto-core'; /** * The class extends the @class SubtleCrypto with addtional methods. * Adds methods to work with key references. * Extends SubtleCrypto to work with JWK keys. */ export default class SubtleCryptoExtension extends SubtleCrypto implements ISubtleCryptoExtension { private keyStore; private cryptoFactory; constructor(cryptoFactory: CryptoFactory); /** * Generate a pairwise key for the algorithm * @param algorithm for the key * @param seedReference Reference to the seed * @param personaId Id for the persona * @param peerId Id for the peer * @param extractable True if key is exportable * @param keyops Key operations */ generatePairwiseKey(algorithm: EcKeyGenParams | RsaHashedKeyGenParams, seedReference: string, personaId: string, peerId: string): Promise<PrivateKey>; /** * Sign with a key referenced in the key store * @param algorithm used for signature * @param keyReference points to key in the key store * @param data to sign * @returns The signature in the requested algorithm */ signByKeyStore(algorithm: CryptoAlgorithm, keyReference: string, data: BufferSource): Promise<ArrayBuffer>; /** * format the signature output to DER format * @param elements Array of elements to encode in DER */ private static toDer; /** * Verify with JWK. * @param algorithm used for verification * @param jwk Json web key used to verify * @param signature to verify * @param payload which was signed */ verifyByJwk(algorithm: CryptoAlgorithm, jwk: JsonWebKey, signature: BufferSource, payload: BufferSource): Promise<boolean>; /** * format the signature output from DER format * @param signature to decode from DER */ private static fromDer; /** * Decrypt with a key referenced in the key store. * The referenced key must be a jwk key. * @param algorithm used for signature * @param keyReference points to key in the key store * @param cipher to decrypt */ decryptByKeyStore(algorithm: CryptoAlgorithm, keyReference: string, cipher: BufferSource): Promise<ArrayBuffer>; /** * Decrypt with JWK. * @param algorithm used for decryption * @param jwk Json web key to decrypt * @param cipher to decrypt */ decryptByJwk(algorithm: CryptoAlgorithm, jwk: JsonWebKey, cipher: BufferSource): Promise<ArrayBuffer>; /** * Encrypt with a jwk key referenced in the key store * @param algorithm used for encryption * @param jwk Json web key public key * @param data to encrypt */ encryptByJwk(algorithm: CryptoAlgorithm, jwk: PublicKey | JsonWebKey, data: BufferSource): Promise<ArrayBuffer>; /** * Normalize the algorithm so it can be used by underlying crypto. * @param algorithm Algorithm to be normalized */ static normalizeAlgorithm(algorithm: any): any; /** * Normalize the JWK parameters so it can be used by underlying crypto. * @param jwk Json web key to be normalized */ static normalizeJwk(jwk: any): any; }