From d77b54a2f855113337d7ca927c35377ff2f37b92 Mon Sep 17 00:00:00 2001 From: Jack Hadrill Date: Mon, 31 Jan 2022 00:02:51 +0000 Subject: [PATCH] Add public API --- src/decrypt.ts | 6 ++++++ src/encrypt.ts | 6 ++++++ src/index.ts | 3 ++- tests/decrypt.test.ts | 21 +++++++++++++++++++++ tests/encrypt.test.ts | 20 ++++++++++++++++++++ tsconfig.eslint.json | 1 - 6 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 src/decrypt.ts create mode 100644 src/encrypt.ts create mode 100644 tests/decrypt.test.ts create mode 100644 tests/encrypt.test.ts diff --git a/src/decrypt.ts b/src/decrypt.ts new file mode 100644 index 0000000..05f2ce2 --- /dev/null +++ b/src/decrypt.ts @@ -0,0 +1,6 @@ +import { cryptoAeadDecrypt } from './romulus-m' + +export function decrypt (ciphertext: Buffer, associatedData: Buffer, nonce: Buffer, key: Buffer): Buffer { + const plaintext = cryptoAeadDecrypt(Array.from(ciphertext), Array.from(associatedData), Array.from(nonce), Array.from(key)) + return Buffer.from(plaintext) +} diff --git a/src/encrypt.ts b/src/encrypt.ts new file mode 100644 index 0000000..668e422 --- /dev/null +++ b/src/encrypt.ts @@ -0,0 +1,6 @@ +import { cryptoAeadEncrypt } from './romulus-m' + +export function encrypt (message: Buffer, associatedData: Buffer, nonce: Buffer, key: Buffer): Buffer { + const ciphertext = cryptoAeadEncrypt(Array.from(message), Array.from(associatedData), Array.from(nonce), Array.from(key)) + return Buffer.from(ciphertext) +} diff --git a/src/index.ts b/src/index.ts index 06216b2..d898f7b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ -console.log('Hello, World') +export { encrypt } from './encrypt' +export { decrypt } from './decrypt' diff --git a/tests/decrypt.test.ts b/tests/decrypt.test.ts new file mode 100644 index 0000000..d5bad56 --- /dev/null +++ b/tests/decrypt.test.ts @@ -0,0 +1,21 @@ +import { decrypt } from '../src/decrypt' + +test('Test buffers are supported by decrypt function.', () => { + // Given + const ciphertext = Buffer.from([ + 225, 53, 3, 212, 22, 112, 246, 194, 61, 171, 230, 187, 157, 102, 32, 76, 62, 65, + 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 + ]) + const associatedData = Buffer.from('Some associated data.') + const nonce = Buffer.from('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f') + const key = Buffer.from('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f') + + // When + const result = decrypt(ciphertext, associatedData, nonce, key) + + // Then + const expectedResult = Buffer.from('Hello, World! This is a test message.') + + expect(result).toMatchObject(expectedResult) +}) diff --git a/tests/encrypt.test.ts b/tests/encrypt.test.ts new file mode 100644 index 0000000..a6faa31 --- /dev/null +++ b/tests/encrypt.test.ts @@ -0,0 +1,20 @@ +import { encrypt } from '../src/encrypt' + +test('Test buffers are supported by encrypt function.', () => { + // Given + const message = Buffer.from('Hello, World! This is a test message.') + const associatedData = Buffer.from('Some associated data.') + const nonce = Buffer.from('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f') + const key = Buffer.from('\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f') + + // When + const result = encrypt(message, associatedData, nonce, key) + + // Then + const expectedResult = Buffer.from([ + 225, 53, 3, 212, 22, 112, 246, 194, 61, 171, 230, 187, 157, 102, 32, 76, 62, 65, + 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 + ]) + expect(result).toMatchObject(expectedResult) +}) diff --git a/tsconfig.eslint.json b/tsconfig.eslint.json index 4430ff4..21c332e 100644 --- a/tsconfig.eslint.json +++ b/tsconfig.eslint.json @@ -1,5 +1,4 @@ { "extends": "./tsconfig.json", - // "include": ["src", "tests"], "exclude": [] } \ No newline at end of file