diff --git a/src/create.js b/src/create.js index 7f73870..7fd0d68 100644 --- a/src/create.js +++ b/src/create.js @@ -1,9 +1,17 @@ -import { BASE_URL } from "./constants.js"; import { createGist } from "./database.js"; // keep these vars on frontend as its needed to decrypt import { ALGORITHM, ALGORITHM_PARAMS, KEY_USAGE } from "../static/subtle.mjs"; -export const encrypt = async (decrypted) => { +/** + * @typedef {Object} CipherObject + * @property {Uint8Array} iv + * @property {string|undefined} k + * @property {ArrayBuffer} cipherText + * + * @param {string} plainText + * @return {Promise} {iv, k, cipherText} + */ +export const encrypt = async (plainText) => { const iv = crypto.getRandomValues(new Uint8Array(16)); const key = await crypto.subtle.generateKey( ALGORITHM_PARAMS, @@ -14,14 +22,22 @@ export const encrypt = async (decrypted) => { const cipherText = await crypto.subtle.encrypt( { name: ALGORITHM, iv }, key, - decrypted + plainText ); return { iv, k, cipherText }; }; -export const create = async (buffer) => { - const { iv, k, cipherText } = await encrypt(buffer); +/** + * @typedef {Object} CreateObject + * @property {string} uuid + * @property {string} k + * + * @param {string} lainText + * @return {Promise} {uuid, k} + */ +export const create = async (plainText) => { + const { iv, k, cipherText } = await encrypt(plainText); const uuid = createGist(iv, cipherText); return { uuid, k }; diff --git a/src/database.js b/src/database.js index 422030c..dca8769 100644 --- a/src/database.js +++ b/src/database.js @@ -13,6 +13,11 @@ db.exec(` ); `); +/** + * @param {Uint8Array} iv + * @param {ArrayBuffer} cipherText + * @return {string} uuid + */ export const createGist = (iv, cipherText) => { const uuid = nanoid(22); @@ -24,6 +29,14 @@ export const createGist = (iv, cipherText) => { return uuid; }; +/** + * @typedef {Object} PartialGist + * @property {string} iv + * @property {ArrayBuffer} cipherText + * + * @param {string} uuid + * @return {PartialGist|undefined} {iv, cipherText} + */ export const getGist = (uuid) => { const query = db.prepare("SELECT cipherText, iv FROM gists WHERE uuid = ?"); return query.get(uuid); diff --git a/static/subtle.mjs b/static/subtle.mjs index 68aa5a1..030d9d4 100644 --- a/static/subtle.mjs +++ b/static/subtle.mjs @@ -16,9 +16,9 @@ export const JWT_PARAMS = { }; /** - * @param {Uint8Array} iv - * @param {string} k - * @param {ArrayBuffer} cipherText + * @param {Uint8Array} iv + * @param {string} k + * @param {ArrayBuffer} cipherText * @return {Promise} */ export const decrypt = async (iv, k, cipherText) => {