Skip to content

Commit

Permalink
Merge pull request #1 from OpenCerts/feat/pre-generated-encryptionkey
Browse files Browse the repository at this point in the history
feat: functionality to encrypt document with pre generated key added
  • Loading branch information
sumitnitsurat authored Oct 1, 2019
2 parents d4f7cd4 + 9be4e0c commit bd681cf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 22 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,22 @@ const encryptionKeyRegex = new RegExp(`^[0-9a-f]{${ENCRYPTION_PARAMETERS.keyLeng
describe("storage/crypto", () => {
describe("encryptString", () => {
let encryptionResults: any | IEncryptionResults;
beforeAll(() => {

test("should have the right keys and values when no key passed", async () => {
encryptionResults = encryptString("hello world");
expect(encryptionResults).toEqual(
expect.objectContaining({
cipherText: expect.stringMatching(base64Regex),
iv: expect.stringMatching(base64Regex),
tag: expect.stringMatching(base64Regex),
key: expect.stringMatching(encryptionKeyRegex),
type: ENCRYPTION_PARAMETERS.version
})
);
});
test("should have the right keys and values", async () => {
test("should have the right keys and values when key is passed", async () => {
const encryptionKey = "35fb46ca758889669f38c83d2f159b0f5a320b5a97387a9eaecb5652d15e0e3d";
encryptionResults = encryptString("hello world", encryptionKey);
expect(encryptionResults).toEqual(
expect.objectContaining({
cipherText: expect.stringMatching(base64Regex),
Expand All @@ -19,8 +31,10 @@ describe("storage/crypto", () => {
type: ENCRYPTION_PARAMETERS.version
})
);
expect(encryptionResults.key).toEqual(encryptionKey);
});
test("should throw error if input is not a string", () => {
encryptionResults = encryptString("hello world");
// @ts-ignore because we're explicitly testing failure mode
expect(() => encryptString({})).toThrow("encryptString only accepts strings");
// @ts-ignore because we're explicitly testing failure mode
Expand All @@ -33,5 +47,11 @@ describe("storage/crypto", () => {
const encryptionResults = encryptString("hello world");
expect(decryptString(encryptionResults)).toBe("hello world");
});

test("can decrypt when encryption key is passed", () => {
const encryptionKey = "35fb46ca758889669f38c83d2f159b0f5a320b5a97387a9eaecb5652d15e0e3d";
const encryptionResults = encryptString("hello world", encryptionKey);
expect(decryptString(encryptionResults)).toBe("hello world");
});
});
});
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const ENCRYPTION_PARAMETERS = {
* Generates a random key represented as a hexadecimal string
* @param {number} keyLengthInBits Key length
*/
const generateEncryptionKey = (keyLengthInBits = ENCRYPTION_PARAMETERS.keyLength) => {
export const generateEncryptionKey = (keyLengthInBits = ENCRYPTION_PARAMETERS.keyLength) => {
const encryptionKey = forge.random.getBytesSync(keyLengthInBits / 8);
return forge.util.bytesToHex(encryptionKey);
};
Expand All @@ -33,8 +33,7 @@ const generateIv = (ivLengthInBits = ENCRYPTION_PARAMETERS.ivLength) => {
* Generates the requisite randomised variables and initialises the cipher with them
* @returns the cipher object, encryption key in hex, and iv in base64
*/
const makeCipher = () => {
const encryptionKey = generateEncryptionKey();
const makeCipher = (encryptionKey: string = generateEncryptionKey()) => {
const iv = generateIv();
const cipher = forge.cipher.createCipher(ENCRYPTION_PARAMETERS.algorithm, forge.util.hexToBytes(encryptionKey));

Expand Down Expand Up @@ -67,12 +66,12 @@ export interface IEncryptionResults {
* @returns key encryption key in hexadecimal
* @returns type The encryption algorithm identifier
*/
export const encryptString = (document: string): IEncryptionResults => {
export const encryptString = (document: string, key?: string): IEncryptionResults => {
if (typeof document !== "string") {
throw new Error("encryptString only accepts strings");
}

const { cipher, encryptionKey, iv } = makeCipher();
const { cipher, encryptionKey, iv } = makeCipher(key);

cipher.update(preProcessDocument(document));
cipher.finish();
Expand Down

0 comments on commit bd681cf

Please sign in to comment.