# @am92/kms

[![npm version](https://img.shields.io/npm/v/@am92/kms?style=for-the-badge)](https://www.npmjs.com/package/@am92/kms)&nbsp;
[![Security: Snyk](https://img.shields.io/badge/Security-Snyk-blueviolet?logo=snyk&style=for-the-badge)](https://snyk.io/test/npm/@am92/kms)&nbsp;
[![License: MIT](https://img.shields.io/npm/l/@am92/kms?color=yellow&style=for-the-badge)](https://opensource.org/licenses/MIT)&nbsp;
[![Downloads](https://img.shields.io/npm/dy/@am92/kms?style=for-the-badge)](https://npm-stat.com/charts.html?package=%40m92%2Fkms)
[![Bundle Size](https://img.shields.io/bundlephobia/minzip/@am92/kms?style=for-the-badge)](https://bundlephobia.com/package/@am92/kms)

<br />

This package provides Key Management related functionalities using either Node Crypto or AWS KMS. It provides the following functionalities:
* Generating Encryption Key for Symmetric and Asymmetric Algorithms
* Encrypting and Decrypting data

<br />

For full documentation, visit [here](https://heliumtank92.github.io/am92-kms).

<br />

## Table of Content
- [Installation](#installation)
- [Environment Variables](#environment-variables)
- [Creating an Instance](#creating-an-instance)
- [Self-managed Config](#self-managed-config)
- [Contributors](#contributors)
- [Resources](#resources)
- [License](#license)

<br />

## Installation
```bash
npm install --save @am92/kms
```
<br />

## Environment Variables
The following environment variables need to be set to work with this package:

```sh
##### KMS Config
export KMS_ENABLED=
export KMS_TYPE=''
export KMS_KEY_SPEC=''
export KMS_KEY_PAIR_SPEC=''
export KMS_KEY_FORMAT=''
export KMS_PLAIN_TEXT_FORMAT=''
export KMS_CIPHER_TEXT_FORMAT=''
export KMS_MASTER_KEY_HEX=''
export KMS_MASTER_IV_HEX=''
export KMS_AWS_REGION=''
export KMS_AWS_KEY_ID=''
```

| Variable Name            | Required | Default                  | Description                                                                 |
| ----------------------- | -------- | ------------------------ | --------------------------------------------------------------------------- |
| `KMS_ENABLED`           | No       | `false`                  | Enables/Disables KMS functionality                                          |
| `KMS_TYPE`              | No*      | -                        | Type of KMS to use. Possible values: `NODE`, `AWS`                        |
| `KMS_KEY_SPEC`          | No       | `AES_256`                | Specification for symmetric key generation                                  |
| `KMS_KEY_PAIR_SPEC`     | No       | `RSA_2048`               | Specification for asymmetric key pair generation                            |
| `KMS_KEY_FORMAT`        | No       | `base64`                 | Format for generated keys                                                   |
| `KMS_PLAIN_TEXT_FORMAT` | No       | `base64`                 | Format for plain text data                                                  |
| `KMS_CIPHER_TEXT_FORMAT`| No       | `base64`                 | Format for encrypted data                                                   |
| `KMS_MASTER_KEY_HEX`    | No       | `0000000000000000000000000000000000000000000000000000000000000000` | Master key in hex format for Node Crypto |
| `KMS_MASTER_IV_HEX`     | No       | `00000000000000000000000000000000` | Master IV in hex format for Node Crypto    |
| `KMS_AWS_REGION`        | No       | `ap-south-1`             | AWS region for KMS operations                                              |
| `KMS_AWS_KEY_ID`        | No**     | -                        | AWS KMS key ID for encryption/decryption                                   |

*Note:*
* *\* - Required if `KMS_ENABLED` is set to `true`.*
* *\*\* - Required if `KMS_TYPE` is set to `AWS`.*

<br />

## Creating an Instance
```javascript
import { generateKmsInstance } from '@am92/kms'

const kms = generateKmsInstance()

export default kms
```

## Self-managed Config
If you wish to pass your custom 'config' for Kms, then you can avoid setting any environment variables defined above and pass your own config as follows:

```javascript
import { generateKmsInstance } from '@am92/kms'

const config = {
  KMS_TYPE: 'NODE'
}

const kms = generateKmsInstance(config)

export default kms
```

<br />

## Contributors
<table>
  <tbody>
    <tr>
      <td align="center">
        <a href='https://github.com/ankitgandhi452'>
          <img src="https://avatars.githubusercontent.com/u/8692027?s=400&v=4" width="100px;" alt="Ankit Gandhi"/>
          <br />
          <sub><b>Ankit Gandhi</b></sub>
        </a>
      </td>
      <td align="center">
        <a href='https://github.com/agarwalmehul'>
          <img src="https://avatars.githubusercontent.com/u/8692023?s=400&v=4" width="100px;" alt="Mehul Agarwal"/>
          <br />
          <sub><b>Mehul Agarwal</b></sub>
        </a>
      </td>
    </tr>
  </tbody>
</table>

<br />

## Resources
* [AWS SDK - KMS Client](https://www.npmjs.com/package/@aws-sdk/client-kms)

<br />

## License
* [MIT](https://opensource.org/licenses/MIT)


<br />
<br />
