helpers.js (1298B)
1 import { encrypt as eceEncrypt } from "./ece.js" 2 3 export async function encrypt(data, p256dhKey, authKey) { 4 if (!(data instanceof Uint8Array)) { 5 throw new Error("Expecting Uint8Array for `data` parameter"); 6 } 7 8 const salt = crypto.getRandomValues(new Uint8Array(16)); 9 10 const keyPair = await crypto.subtle.generateKey({ name: 'ECDH', namedCurve: 'P-256' }, true, ["deriveBits"]); 11 const publicKey = new Uint8Array(await crypto.subtle.exportKey("raw", keyPair.publicKey)); 12 13 const body = await eceEncrypt(data, { 14 userAgentPublicKey: new Uint8Array(p256dhKey), 15 appServer: { 16 privateKey: keyPair.privateKey, 17 publicKey, 18 }, 19 salt, 20 authSecret: authKey, 21 }); 22 23 const headers = { 24 // https://datatracker.ietf.org/doc/html/rfc8291#section-4 25 // The Content-Encoding header field therefore has exactly one value, which is "aes128gcm". 26 'Content-Encoding': "aes128gcm", 27 // https://datatracker.ietf.org/doc/html/rfc8030#section-5.2 28 // An application server MUST include the TTL (Time-To-Live) header 29 // field in its request for push message delivery. The TTL header field 30 // contains a value in seconds that suggests how long a push message is 31 // retained by the push service. 32 TTL: 15, 33 }; 34 35 return { 36 body, 37 headers, 38 } 39 }