From fbd46b3f3f6732d8d3cc5f02f4cf752a57f9a1f5 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Tue, 3 Jan 2023 23:47:33 -0800 Subject: [PATCH] refactoring contexts --- net/web/src/util/sealUtil.js | 74 ++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 net/web/src/util/sealUtil.js diff --git a/net/web/src/util/sealUtil.js b/net/web/src/util/sealUtil.js new file mode 100644 index 00000000..4431b516 --- /dev/null +++ b/net/web/src/util/sealUtil.js @@ -0,0 +1,74 @@ +import CryptoJS from 'crypto-js'; +import { JSEncrypt } from 'jsencrypt' + +export function isUnsealed(subject, sealKey) { + const { seals } = JSON.parse(subject); + for (let i = 0; i < seals?.length; i++) { + if (seals[i].publicKey === sealKey.public) { + return sealKey.private != null; + } + } + return false; +} + +export function getContentKey(subject, sealKey) { + for (let i = 0; i < seal?.length; i++) { + if (seal[i].publicKey === sealKey.public) { + let crypto = new JSEncrypt(); + crypto.setPrivateKey(sealKey.private); + return crypto.decrypt(seal.sealedKey); + + } + } + throw new Error("unsealKey not available"); +} + +export function encryptChannelSubject(subject, publicKeys) { + const key = CryptoJS.lib.WordArray.random(256 / 8); + const iv = CryptoJS.lib.WordArray.random(128 / 8); + const encrypted = CryptoJS.AES.encrypt(JSON.stringify({ subject }), key, { iv: iv }); + const subjectEncrypted = encrypted.ciphertext.toString(CryptoJS.enc.Base64) + const subjectIv = iv.toString(); + const keyHex = key.toString(); + + let seals = []; + let crypto = new JSEncrypt(); + publicKeys.forEach(key => { + crypto.setPublicKey(key); + const sealedKey = crypto.encrypt(keyHex); + seals.push({ publicKey, sealedKey }); + }); + + return { subjectEncrypted, subjectIv, seals }; +} + +export function decryptChannelSubject(subject, sealKey) { + const { subjectEncrypted, subjectIv, seals } = JSON.parse(subject); + const contentKey = getContentKey(seals, sealKey); + const iv = CryptoJS.enc.Hex.parse(subjectIv); + const key = CryptoJS.enc.Hex.parse(contentKey); + const enc = CryptoJS.enc.Base64.parse(subjectEncrypted); + const cipher = CryptoJS.lib.CipherParams.create({ ciphertext: enc, iv: iv }); + const dec = CryptoJS.AES.decrypt(cipher, key, { iv: iv }); + return JSON.parse(dec.toString(CryptoJS.enc.Utf8)); +} + +export function encryptTopicSubject(subject, contentKey) { + const iv = CryptoJS.lib.WordArray.random(128 / 8); + const key = CryptoJS.enc.Hex.parse(contentKey); + const encrypted = CryptoJS.AES.encrypt(JSON.stringify({ subject }), key, { iv: iv }); + const messageEncrypted = encrypted.ciphertext.toString(CryptoJS.enc.Base64) + const messageIv = iv.toString(); + return { messageEncrypted, messageIv }; +} + +export function decryptTopicSubject(subject, contentKey) { + const { messageEncrypted, messageIv } = JSON.parse(subject); + const iv = CryptoJS.enc.Hex.parse(messageIv); + const key = CryptoJS.enc.Hex.parse(contentKey); + const enc = CryptoJS.enc.Base64.parse(messageEncrypted); + let cipher = CryptoJS.lib.CipherParams.create({ ciphertext: enc, iv: iv }); + const dec = CryptoJS.AES.decrypt(cipher, key, { iv: iv }); + return JSON.parse(dec.toString(CryptoJS.enc.Utf8)); +} +