Remove buffer dependency.

This commit is contained in:
Jack Hadrill
2022-02-17 19:22:26 +00:00
parent 881fd751dd
commit 6dec3496e8
4 changed files with 18 additions and 18 deletions

View File

@@ -2,7 +2,7 @@ import { cryptoAeadDecrypt } from './romulus-m'
interface DecryptResult {
success: boolean
plaintext: Buffer
plaintext: Uint8Array
}
/**
@@ -13,7 +13,7 @@ interface DecryptResult {
* @param key The encryption key.
* @returns A decrypted DecryptResult object.
*/
export function decrypt (buffer: Buffer, associatedData: Buffer, key: Buffer): DecryptResult {
export function decrypt (buffer: Uint8Array, associatedData: Uint8Array, key: Uint8Array): DecryptResult {
// Split nonce from ciphertext.
const nonce = Array.from(buffer.slice(0, 16))
const ciphertext = Array.from(buffer.slice(16))
@@ -24,6 +24,6 @@ export function decrypt (buffer: Buffer, associatedData: Buffer, key: Buffer): D
// Return the ciphertext and decryption status.
return {
success: result.success,
plaintext: Buffer.from(result.plaintext)
plaintext: Uint8Array.from(result.plaintext)
}
}

View File

@@ -9,14 +9,14 @@ import { v4 as uuidv4 } from 'uuid'
* @param key The encryption key.
* @returns The nonce-prepended ciphertext.
*/
export function encrypt (message: Buffer, associatedData: Buffer, key: Buffer): Buffer {
export function encrypt (message: Uint8Array, associatedData: Uint8Array, key: Uint8Array): Uint8Array {
// Generate a nonce.
const nonce = Buffer.alloc(16)
const nonce = new Uint8Array(16)
uuidv4({}, nonce)
// Encrypt the data using the associated data, newly generated nonce and encryption key.
const ciphertext = Buffer.from(cryptoAeadEncrypt(Array.from(message), Array.from(associatedData), Array.from(nonce), Array.from(key)))
const ciphertext = Uint8Array.from(cryptoAeadEncrypt(Array.from(message), Array.from(associatedData), Array.from(nonce), Array.from(key)))
// Return the nonce-prepended ciphertext.
return Buffer.concat([nonce, ciphertext])
return new Uint8Array([...nonce, ...ciphertext])
}