dag-jose
Version:
Typescript implementation of the IPLD dag-jose format
60 lines • 1.87 kB
JavaScript
import { fromBase64url, toBase64url } from './utils.js';
export function fromSplit(split) {
const [protectedHeader, encrypted_key, iv, ciphertext, tag] = split;
const jwe = {
ciphertext,
iv,
protected: protectedHeader,
tag,
};
if (encrypted_key)
jwe.recipients = [{ encrypted_key }];
return jwe;
}
function encodeRecipient(recipient) {
const encRec = {};
if (recipient.encrypted_key)
encRec.encrypted_key = fromBase64url(recipient.encrypted_key);
if (recipient.header)
encRec.header = recipient.header;
return encRec;
}
export function encode(jwe) {
const encJwe = {
ciphertext: fromBase64url(jwe.ciphertext),
protected: fromBase64url(jwe.protected),
iv: fromBase64url(jwe.iv),
tag: fromBase64url(jwe.tag),
};
if (jwe.aad)
encJwe.aad = fromBase64url(jwe.aad);
if (jwe.recipients)
encJwe.recipients = jwe.recipients.map(encodeRecipient);
if (jwe.unprotected)
encJwe.unprotected = jwe.unprotected;
return encJwe;
}
function decodeRecipient(encoded) {
const recipient = {};
if (encoded.encrypted_key)
recipient.encrypted_key = toBase64url(encoded.encrypted_key);
if (encoded.header)
recipient.header = encoded.header;
return recipient;
}
export function decode(encoded) {
const jwe = {
ciphertext: toBase64url(encoded.ciphertext),
protected: toBase64url(encoded.protected),
iv: toBase64url(encoded.iv),
tag: toBase64url(encoded.tag),
};
if (encoded.aad)
jwe.aad = toBase64url(encoded.aad);
if (encoded.recipients)
jwe.recipients = encoded.recipients.map(decodeRecipient);
if (encoded.unprotected)
jwe.unprotected = encoded.unprotected;
return jwe;
}
//# sourceMappingURL=encryption.js.map