( serialized: string, scheme: Scheme, keys: EnvelopePrivateKeySlots, aad?: string )
| 94 | } |
| 95 | |
| 96 | export function decryptKeyedEnvelope<Scheme extends string>( |
| 97 | serialized: string, |
| 98 | scheme: Scheme, |
| 99 | keys: EnvelopePrivateKeySlots, |
| 100 | aad?: string |
| 101 | ): string { |
| 102 | const envelope = parseKeyedEnvelope(serialized, scheme); |
| 103 | const key = selectPrivateKey(envelope.keyId, keys); |
| 104 | |
| 105 | if (!key.privateKeyPem) { |
| 106 | throw new EncryptionConfigurationError( |
| 107 | `Private key is not configured for keyed envelope key ID: ${envelope.keyId}` |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | return decryptWithPrivateKey(envelope.ciphertext, key.privateKeyPem, aad); |
| 112 | } |
| 113 | |
| 114 | function selectPrivateKey(keyId: string, keys: EnvelopePrivateKeySlots): EnvelopePrivateKeySlot { |
| 115 | const decryptSlot = keys.decrypt?.find(slot => slot.keyId === keyId); |
no test coverage detected