{"version":3,"file":"symmetric-crypto.mjs","names":["aeadCipher"],"sources":["../../src/symmetric-crypto/aes.native.ts"],"sourcesContent":["import { concatBytes, randomBytes } from '@noble/hashes/utils.js';\nimport { IV_LEN_BYTES, AES_KEY_BYTE_LENGTH } from '../constants';\nimport { gcm as aeadCipher } from '@noble/ciphers/aes.js';\n\n/**\n * Symmetrically encrypts the message\n *\n * @param encryptionKey - The symmetric key used for message encryption\n * @param message - The message to encrypt\n * @param aux - The auxilary string (e.g., context string or timestamp) for AEAD.\n * @returns The resulting ciphertext.\n */\nexport async function encryptSymmetrically(\n  encryptionKey: Uint8Array,\n  message: Uint8Array,\n  aux?: Uint8Array,\n): Promise<Uint8Array> {\n  try {\n    const iv = randomBytes(IV_LEN_BYTES);\n    const ciphertext = aeadCipher(encryptionKey, iv, aux).encrypt(message);\n    return concatBytes(ciphertext, iv);\n  } catch (error) {\n    throw new Error('Failed to encrypt symmetrically', { cause: error });\n  }\n}\n\n/**\n * Decryps symmetrically encrypted message\n *\n * @param encryptionKey - The symmetric key used for message encryption\n * @param encryptedMessage - The ciphertext\n * @param aux - The auxilary string (e.g., context string or timestamp) for AEAD.\n * @returns The resulting ciphertext.\n */\nexport async function decryptSymmetrically(\n  encryptionKey: Uint8Array,\n  encryptedMessage: Uint8Array,\n  aux?: Uint8Array,\n): Promise<Uint8Array> {\n  try {\n    const ciphertext = encryptedMessage.slice(0, encryptedMessage.length - IV_LEN_BYTES);\n    const iv = encryptedMessage.slice(encryptedMessage.length - IV_LEN_BYTES);\n    const result = aeadCipher(encryptionKey, iv, aux).decrypt(ciphertext);\n    return result;\n  } catch (error) {\n    throw new Error('Failed to decrypt symmetrically', { cause: error });\n  }\n}\n\n/**\n * Generates symmetric key as Uint8Array\n *\n * @returns The generated Uint8Array.\n */\nexport function genSymmetricKey(): Uint8Array {\n  return randomBytes(AES_KEY_BYTE_LENGTH);\n}\n"],"mappings":";;;;;;;;;;;;AAYA,eAAsB,qBACpB,eACA,SACA,KACqB;AACrB,KAAI;EACF,MAAM,KAAK,YAAA,GAAyB;AAEpC,SAAO,YADYA,IAAW,eAAe,IAAI,IAAI,CAAC,QAAQ,QACjC,EAAE,GAAG;UAC3B,OAAO;AACd,QAAM,IAAI,MAAM,mCAAmC,EAAE,OAAO,OAAO,CAAC;;;;;;;;;;;AAYxE,eAAsB,qBACpB,eACA,kBACA,KACqB;AACrB,KAAI;EACF,MAAM,aAAa,iBAAiB,MAAM,GAAG,iBAAiB,SAAA,GAAsB;AAGpF,SADeA,IAAW,eADf,iBAAiB,MAAM,iBAAiB,SAAA,GACR,EAAE,IAAI,CAAC,QAAQ,WAC7C;UACN,OAAO;AACd,QAAM,IAAI,MAAM,mCAAmC,EAAE,OAAO,OAAO,CAAC;;;;;;;;AASxE,SAAgB,kBAA8B;AAC5C,QAAO,YAAA,GAAgC"}