Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added utilities/aes-gcm-nodejs/.DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions utilities/aes-gcm-nodejs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
coverage/
126 changes: 126 additions & 0 deletions utilities/aes-gcm-nodejs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# Crypto module in node.js

**Note**: All examples here are shown in **base64** format. This can be changed in the constants.js file

## Prerequisite

Make sure you are in the root of folder (the folder which contains src, \_\_tests\_\_ and README.md) of the repo and have node installed (v16^).

## Demo

Run this command to run the script (A demo for key pair and shared key generation, encrpytion and decryption)

```
node src/index.js
```

Here is a sample output

```
Key Pair 1 ==> {
publicKey: 'MCowBQYDK2VuAyEANyOBz6NyhiX4rrVyJpcekF0AUmE/aHPAO/b0bqY0EXw=',
privateKey: 'MC4CAQAwBQYDK2VuBCIEIPDHIoxb4nFan0TAxTBCJzbt1s+UNfVkKstth3AyG5By'
}
Key Pair 2 ==> {
publicKey: 'MCowBQYDK2VuAyEAwScw6KPlklcKUufAtFJDjGmQ3cRMeQQVX2U0dpk7agI=',
privateKey: 'MC4CAQAwBQYDK2VuBCIEIDAZizDiwEPopeOPO63ajN6wrj65iXKNkpfmakHqQrF4'
}
SharedKey1 ==> X+T+C7UvJo258rAqAhT8NLRfh24/by+Vd6A1h9c3mno=
SharedKey2 ==> X+T+C7UvJo258rAqAhT8NLRfh24/by+Vd6A1h9c3mno=
sharedKey1 == sharedKey2 ==> true
Encrypted Data ===> eyJlbmNyeXB0ZWRfZGF0YSI6Ikk5L3hUVmNPTkZ1aVVBcm9PNHJwaG9hRnRKZFpyQ3NFRnR3SVJ3PT0iLCJobWFjIjoiUlNYOURWUXRGdk1DeTNVNTZyY2s0QT09Iiwibm9uY2UiOiJ2R01kU0VnRk9nVnAxalNnIn0=
Decrypted Data ===> Hello This is ONDC Test Data
```

## Usage

Functions in keyUtils.js and encryptionUtil.js can also be used individually (doc strings can be used as a guide)

### keyUtil/generateKeyPair()

Input: none

Sample output:

```
{
publicKey: 'MCowBQYDK2VuAyEANyOBz6NyhiX4rrVyJpcekF0AUmE/aHPAO/b0bqY0EXw=',
privateKey: 'MC4CAQAwBQYDK2VuBCIEIPDHIoxb4nFan0TAxTBCJzbt1s+UNfVkKstth3AyG5By'
}
```

### keyUtil/generateSharedKey(privateKeyString, publicKeyString)

From keyPair1 and keyPair2, a shared key for both the parties is generated by using privateKey from one and publicKey from the other (Example: privateKey from keyPair1 and publicKey from keyPair2 or vise-versa)

Input: privateKey (string), publicKey (string)

Sample output:

```
X+T+C7UvJo258rAqAhT8NLRfh24/by+Vd6A1h9c3mno=
```

### encryptionUtil/encryptData(sharedKey, data)

Input: sharedKey (string), data (string)

Sample output:

```
eyJlbmNyeXB0ZWRfZGF0YSI6Ikk5L3hUVmNPTkZ1aVVBcm9PNHJwaG9hRnRKZFpyQ3NFRnR3SVJ3PT0iLCJobWFjIjoiUlNYOURWUXRGdk1DeTNVNTZyY2s0QT09Iiwibm9uY2UiOiJ2R01kU0VnRk9nVnAxalNnIn0=
```

**Note**: This encrypted string contains the information for decryption as well (hmac, nonce and encrypted string)

### encryptionUtil/decryptData(sharedKey, eData)

Input: sharedKey (string), encrypted data (string)

Sample output:

```
Hello This is ONDC Test Data
```

**Note**: the shared key and encrypted data must be the same that was used for encrpytion.

### encryptionUtil/convertPayloadToBase64(encryptedMessage, hmac, iv)

Input: encrypted message (string), hmac (string), iv (string)

Sample output:

```
eyJlbmNyeXB0ZWRfZGF0YSI6Ikk5L3hUVmNPTkZ1aVVBcm9PNHJwaG9hRnRKZFpyQ3NFRnR3SVJ3PT0iLCJobWFjIjoiUlNYOURWUXRGdk1DeTNVNTZyY2s0QT09Iiwibm9uY2UiOiJ2R01kU0VnRk9nVnAxalNnIn0=
```

## Tests and Coverage

To get the code coverage, these are the following steps:

1. Run this command to install testing libarary

```
npm install
```

2. Run this command to generate coverage report from test cases

```
npm run coverage
```

This should create a folder called `coverage`

### Folder purpose

index.js is the main flow (demo for generation of key pairs and shared key, encryption and decryption)

keyUtil.js contains all the methods for creating key pairs and shared keys

encryptionUtil.js contains all the logic for encryption and decryption

constants.js contains all the variables that can be customised

\_\_tests\_\_ contains all the test cases
23 changes: 23 additions & 0 deletions utilities/aes-gcm-nodejs/__tests__/encryption.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const { randomBytes } = require("crypto");
const { encryptData, decryptData } = require("../src/encryptionUtil");

test("Checking encryption with same shared key", () => {
const MESSAGE = "This is a secret...";
const sharedKey = randomBytes(32).toString("base64");

const encryptedData = encryptData(sharedKey, MESSAGE);
const decrpyedtData = decryptData(sharedKey, encryptedData);

expect(decrpyedtData === MESSAGE).toBe(true);
});

test("Checking encryption with different shared key", () => {
const MESSAGE = "This is a secret...";
const sharedKey1 = randomBytes(32).toString("base64");
const sharedKey2 = randomBytes(32).toString("base64");

const encryptedData = encryptData(sharedKey1, MESSAGE);
const decrpyedtData = decryptData(sharedKey2, encryptedData);

expect(decrpyedtData === MESSAGE).toBe(false);
});
21 changes: 21 additions & 0 deletions utilities/aes-gcm-nodejs/__tests__/sharedkey.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const { generateKeyPair, generateSharedKey } = require("../src/keyUtil");

test("Check if two pairs of keys generate the same shared key after exchange", () => {
const keyPair1 = generateKeyPair();
const keyPair2 = generateKeyPair();

const sharedKey1 = generateSharedKey(keyPair1.privateKey, keyPair2.publicKey);
const sharedKey2 = generateSharedKey(keyPair2.privateKey, keyPair1.publicKey);

expect(sharedKey1 === sharedKey2).toBe(true);
});

test("Check if two pairs of key generate the same shared key when not exchanged", () => {
const keyPair1 = generateKeyPair();
const keyPair2 = generateKeyPair();

const sharedKey1 = generateSharedKey(keyPair1.privateKey, keyPair1.publicKey);
const sharedKey2 = generateSharedKey(keyPair2.privateKey, keyPair2.publicKey);

expect(sharedKey1 === sharedKey2).toBe(false);
});
Loading