Remove buffer dependency.
All checks were successful
continuous-integration/drone/push Build is passing

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 { interface DecryptResult {
success: boolean success: boolean
plaintext: Buffer plaintext: Uint8Array
} }
/** /**
@ -13,7 +13,7 @@ interface DecryptResult {
* @param key The encryption key. * @param key The encryption key.
* @returns A decrypted DecryptResult object. * @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. // Split nonce from ciphertext.
const nonce = Array.from(buffer.slice(0, 16)) const nonce = Array.from(buffer.slice(0, 16))
const ciphertext = Array.from(buffer.slice(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 the ciphertext and decryption status.
return { return {
success: result.success, 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. * @param key The encryption key.
* @returns The nonce-prepended ciphertext. * @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. // Generate a nonce.
const nonce = Buffer.alloc(16) const nonce = new Uint8Array(16)
uuidv4({}, nonce) uuidv4({}, nonce)
// Encrypt the data using the associated data, newly generated nonce and encryption key. // 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 the nonce-prepended ciphertext.
return Buffer.concat([nonce, ciphertext]) return new Uint8Array([...nonce, ...ciphertext])
} }

View File

@ -2,7 +2,7 @@ import { decrypt } from '../src/decrypt'
test('Test nonce parsing by public decrypt function.', () => { test('Test nonce parsing by public decrypt function.', () => {
// Given // Given
const ciphertext = Buffer.from([ const ciphertext = Uint8Array.from([
// Nonce // Nonce
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
// Ciphertext // Ciphertext
@ -10,21 +10,21 @@ test('Test nonce parsing by public decrypt function.', () => {
25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162, 142, 69, 40, 25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162, 142, 69, 40,
167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172, 165, 116, 119, 128 167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172, 165, 116, 119, 128
]) ])
const associatedData = Buffer.from('Some associated data.') const associatedData = new TextEncoder().encode('Some associated data.')
const key = Buffer.from('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f') const key = Uint8Array.from([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f])
// When // When
const result = decrypt(ciphertext, associatedData, key) const result = decrypt(ciphertext, associatedData, key)
// Then // Then
const expectedResult = Buffer.from('Hello, World! This is a test message.') const expectedResult = new TextEncoder().encode('Hello, World! This is a test message.')
expect(result.success).toBe(true) expect(result.success).toBe(true)
expect(result.plaintext).toMatchObject(expectedResult) expect(result.plaintext).toMatchObject(expectedResult)
}) })
test('Test decryption with an invalid key.', () => { test('Test decryption with an invalid key.', () => {
// Given // Given
const ciphertext = Buffer.from([ const ciphertext = Uint8Array.from([
// Nonce // Nonce
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
// Ciphertext // Ciphertext
@ -32,13 +32,13 @@ test('Test decryption with an invalid key.', () => {
25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162, 142, 69, 40, 25, 202, 255, 201, 206, 49, 60, 58, 82, 216, 72, 116, 106, 129, 162, 142, 69, 40,
167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172, 165, 116, 119, 128 167, 88, 94, 195, 174, 217, 242, 149, 224, 125, 196, 237, 172, 165, 116, 119, 128
]) ])
const associatedData = Buffer.from('Some associated data.') const associatedData = new TextEncoder().encode('Some associated data.')
const key = Buffer.from('\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x00') const key = Uint8Array.from([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00])
// When // When
const result = decrypt(ciphertext, associatedData, key) const result = decrypt(ciphertext, associatedData, key)
// Then // Then
expect(result.success).toBe(false) expect(result.success).toBe(false)
expect(result.plaintext).toMatchObject(Buffer.alloc(0)) expect(result.plaintext).toMatchObject(new Uint8Array())
}) })

View File

@ -3,9 +3,9 @@ import { encrypt } from '../src/encrypt'
test('Test nonce generation by public encrypt function.', () => { test('Test nonce generation by public encrypt function.', () => {
// Given // Given
const message = Buffer.from('Hello, World! This is a test message.') const message = new TextEncoder().encode('Hello, World! This is a test message.')
const associatedData = Buffer.from('Some associated data.') const associatedData = new TextEncoder().encode('Some associated data.')
const key = Buffer.from('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f') const key = Uint8Array.from([0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f])
// When // When
const ciphertext = encrypt(message, associatedData, key) const ciphertext = encrypt(message, associatedData, key)