import { Binary } from '../binary.js'; import { type AlgorithmUrn } from '../ciphers/algorithms.js'; export type EncryptResult = { /** Encrypted payload. */ payload: Binary; /** Auth tag, if generated/ */ authTag?: Binary; }; export type DecryptResult = { payload: Binary; }; /** * PEM formatted keypair. */ export type PemKeyPair = { publicKey: string; privateKey: string; }; /** * The minimum acceptable asymetric key size, currently 2^11. */ export const MIN_ASYMMETRIC_KEY_SIZE_BITS = 2048; export type AnyKeyPair = PemKeyPair | CryptoKeyPair; export type CryptoService = { /** Track which crypto implementation we are using */ name: string; /** Default algorithm identifier. */ method: AlgorithmUrn; /** Convert or narrow from AnyKeyPair to PemKeyPair */ cryptoToPemPair: (keys: AnyKeyPair) => Promise; /** * Try to decrypt content with the default or handed algorithm. Throws on * most failure, if auth tagging is implemented for example. */ decrypt: ( payload: Binary, key: Binary, iv: Binary, algorithm?: AlgorithmUrn, authTag?: Binary ) => Promise; decryptWithPrivateKey: (encryptedPayload: Binary, privateKey: string) => Promise; /** * Encrypt content with the default or handed algorithm. */ encrypt: ( payload: Binary, key: Binary, iv: Binary, algorithm?: AlgorithmUrn ) => Promise; encryptWithPublicKey: (payload: Binary, publicKey: string) => Promise; /** Get length random bytes as a hex-encoded string. */ generateInitializationVector: (length?: number) => Promise; /** Get length random bytes as a hex-encoded string. */ generateKey: (length?: number) => Promise; /** * Generate an RSA key pair * @param size in bits, defaults to a reasonable size for the default method */ generateKeyPair: (size?: number) => Promise; generateSigningKeyPair: () => Promise; /** * Create an HMAC SHA256 hash */ hmac: (key: string, content: string) => Promise; randomBytes: (byteLength: number) => Promise; /** Compute the hex-encoded SHA hash of a UTF-16 encoded string. */ sha256: (content: string) => Promise; };