|
| 1 | +# String-Cipher |
| 2 | + |
| 3 | +Simple set of crypto function for encrypting and decrypting UTF-8 strings. The module uses AES-GMC (128, 192 and 256) bases on Node crypto module. Solution used is based on this [gist](https://gist.github.com/AndiDittrich/4629e7db04819244e843.js). By using GMC encryption chiper text is authenticated as well. Base of this module are the make functions that generate desired encrypt and decrypt functions. |
| 4 | + |
| 5 | +Written in Typescript as an ES6 module, all functions are provided in Sync and Async versions. In order to imporve over all security scheme, user supplied `Password` and random `Salt` is used to drive a key using pbkdf2 (with default iterations of 1, for speed but this can be changed using options). The key length depends on the AES-GMC version (128/192/256 use 16/24/32 bit keys) other values are defaulted to values specified by [RFC 5288](https://tools.ietf.org/html/rfc5288) |
| 6 | + |
| 7 | +## Contents |
| 8 | + |
| 9 | +- [Installation and Usage](#installation-and-usage) |
| 10 | +- [Customization](#customization) |
| 11 | +- [Make functions Option](#make-functions-option) |
| 12 | + |
| 13 | +## Installation and Usage |
| 14 | + |
| 15 | +```sh |
| 16 | +npm install --save string-cipher |
| 17 | + |
| 18 | +``` |
| 19 | +#### Async Api usage |
| 20 | +Using async api for string inputs |
| 21 | +```javascript |
| 22 | +import { encryptString, decryptString } from 'string-cipher'; |
| 23 | + |
| 24 | +const fetchedPassword = 'password'; // fetched from secure location not to placed in code like this |
| 25 | +const plainText = 'test string'; // utf-8 strings |
| 26 | +const cipherText = await encryptString(plainText, fetchedPassword); |
| 27 | +const retrivedText = await decryptString(cipherText, fetchedPassword); |
| 28 | +``` |
| 29 | +Using async api for JSON inputs |
| 30 | +```javascript |
| 31 | +import { encryptJson, decryptJson } from 'string-cipher'; |
| 32 | + |
| 33 | +const fetchedPassword = 'password'; // fetched from secure location not to placed in code like this |
| 34 | +const plainJson = { "field": "value" }; // any object that can be stringifed by JSON.stringify |
| 35 | +const cipherJson = await encryptJson(plainJson, fetchedPassword); |
| 36 | +const retrivedJson = await decryptJson(cipherJson, fetchedPassword); |
| 37 | +``` |
| 38 | +#### Sync Api usage |
| 39 | +Using sync api for string inputs |
| 40 | +```javascript |
| 41 | +import { encryptStringSync, decryptStringSync } from 'string-cipher'; |
| 42 | + |
| 43 | +const fetchedPassword = 'password'; // fetched from secure location not to placed in code like this |
| 44 | +const plainText = 'test string'; // utf-8 strings |
| 45 | +const cipherText = encryptStringSync(plainText, fetchedPassword); |
| 46 | +const retrivedText = decryptStringSync(cipherText, fetchedPassword); |
| 47 | +``` |
| 48 | +Using sync api for JSON inputs |
| 49 | +```javascript |
| 50 | +import { encryptJsonSync, decryptJsonSync } from 'string-cipher'; |
| 51 | + |
| 52 | +const fetchedPassword = 'password'; // fetched from secure location not to placed in code like this |
| 53 | +const plainJson = { "field": "value" }; // any object that can be stringifed by JSON.stringify |
| 54 | +const cipherJson = encryptJsonSync(plainJson, fetchedPassword); |
| 55 | +const retrivedJson = decryptJsonSync(cipherJson, fetchedPassword); |
| 56 | +``` |
| 57 | +## Customization |
| 58 | +With the help of make functions provided by the module you can customize the encryption and decrption setting. Note that same configuration should be appiled to both `makeStringEncrypter` and `makeStringDecrypter` |
| 59 | + |
| 60 | +```javascript |
| 61 | +import { makeStringEncrypter, makeStringDecrypter } from 'string-cipher'; |
| 62 | + |
| 63 | +const commonOptions = { |
| 64 | + algorithm: 'aes-128-gcm', |
| 65 | + stringEncoding = 'ascii', |
| 66 | + authTagLength = 8, |
| 67 | + ivLength = 8, |
| 68 | + saltLength = 8, |
| 69 | + iterations = 10, |
| 70 | + digest = 'sha256' |
| 71 | +} |
| 72 | + |
| 73 | +const customEncrypt = makeStringEncrypter({ |
| 74 | + outputEncoding = 'hex', |
| 75 | + ...commonOptions |
| 76 | +}); |
| 77 | +const customEncryptJson = (payload, password) => customEncrypt(JSON.stringify(payload), password); |
| 78 | + |
| 79 | +const customDecrypt = makeStringDecrypter({ |
| 80 | + inputEncoding = 'hex', |
| 81 | + ...commonOptions |
| 82 | +}); |
| 83 | +const customDecryptJson = async (payload, password) => JSON.parse(await customDecrypt(payload, password)); |
| 84 | + |
| 85 | +const fetchedPassword = 'password'; // fetched from secure location not to placed in code like this |
| 86 | +const plainText = 'test string'; |
| 87 | +const cipherText = await customEncrypt(plainText, fetchedPassword); |
| 88 | +const retrivedText = await customDecrypt(cipherText, fetchedPassword); |
| 89 | + |
| 90 | +``` |
| 91 | +`makeStringEncrypterSync` and `makeStringDecrypterSync` functions can used to generate Sync versions of cutomized encrypt and decrypt functions. |
| 92 | +## Make Functions Option |
| 93 | +|Option|Type|Required|Values|Default|Notes| |
| 94 | +|------|----|--------|------|-------|-----| |
| 95 | +|algorithm|CipherGCMTypes|Yes|`aes-256-gcm | aes-128-gcm | aes-192-gcm`|none|| |
| 96 | +|stringEncoding|string|No|`utf8 | ascii`|`utf8`|encoding format of the input string| |
| 97 | +|outputEncoding|string|No|`base64 | hex`|`base64`|only for encryption function output format| |
| 98 | +|inputEncoding|string|No|`base64 | hex`|`base64`|only for decryption function input format| |
| 99 | +|ivLength|number|No|any number|12|Security of encryption depends on this 12 is recomemded| |
| 100 | +|authTagLength|number|No|any number|16|Security of encryption depends on this 16 is recomemded| |
| 101 | +|saltLength|number|No|any number|32|Used for password generation| |
| 102 | +|iterations|number|No|any number|1|Used by pbkdf2 to drive key, main factor is speed| |
| 103 | +|digest|string|No|`sha256 | sha512`|`sha256`|Used by pbkdf2 to drive key| |
| 104 | + |
0 commit comments