Skip to content

Commit b04299a

Browse files
feat(repo): init repo
0 parents  commit b04299a

37 files changed

+12497
-0
lines changed

.eslintrc.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
module.exports = {
2+
extends: ['airbnb-base'],
3+
rules: {
4+
'comma-dangle': ['error', 'never'],
5+
'import/prefer-default-export': 0,
6+
'max-len': 0,
7+
'import/extensions': 0
8+
},
9+
overrides: [
10+
{
11+
files: ['**/*.ts'],
12+
extends: ['plugin:@typescript-eslint/recommended'],
13+
parser: '@typescript-eslint/parser',
14+
plugins: ['@typescript-eslint'],
15+
settings: {
16+
'import/parsers': {
17+
'@typescript-eslint/parser': ['.ts']
18+
},
19+
'import/resolver': {
20+
typescript: {}
21+
}
22+
},
23+
rules: {
24+
'@typescript-eslint/no-explicit-any': 0,
25+
'@typescript-eslint/explicit-function-return-type': 0,
26+
}
27+
},
28+
{
29+
files: ['**/*.test.ts'],
30+
extends: ['plugin:jest/recommended'],
31+
env: {
32+
jest: true
33+
}
34+
}
35+
]
36+
};

.github/workflows/eslint.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: "eslint"
2+
on:
3+
push:
4+
jobs:
5+
eslint:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v2
9+
10+
- name: Cache node_modules
11+
id: cache-node_modules
12+
uses: actions/cache@v1
13+
with:
14+
path: node_modules
15+
key: node_modules-${{ hashFiles('package-lock.json') }}
16+
17+
- name: NPM Install
18+
if: steps.cache-node_modules.outputs.cache-hit != 'true'
19+
run: npm install
20+
21+
- name: Run eslint
22+
run: npm run eslint

.github/workflows/unit-test.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: "unit test"
2+
on:
3+
push:
4+
jobs:
5+
unit-test:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v2
9+
10+
- name: Cache node_modules
11+
id: cache-node_modules
12+
uses: actions/cache@v1
13+
with:
14+
path: node_modules
15+
key: node_modules-${{ hashFiles('package-lock.json') }}
16+
17+
- name: NPM Install
18+
if: steps.cache-node_modules.outputs.cache-hit != 'true'
19+
run: npm install
20+
21+
- name: Run test
22+
run: npm run test

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# .vscode
2+
.vscode/
3+
4+
# generated from tsc
5+
dist/
6+
7+
# Dependency directories
8+
node_modules/
9+
10+
# Logs
11+
logs
12+
*.log
13+
npm-debug.log*
14+
yarn-debug.log*
15+
yarn-error.log*
16+
17+
# Runtime data
18+
pids
19+
*.pid
20+
*.seed
21+
*.pid.lock
22+
23+
# Optional npm cache directory
24+
.npm
25+
26+
# Optional eslint cache
27+
.eslintcache
28+
29+
# Optional REPL history
30+
.node_repl_history
31+
32+
# Output of 'npm pack'
33+
*.tgz
34+
35+
# Ingore webpack cache
36+
.webpack-cache/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 limplash
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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+

jest.config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = {
2+
testEnvironment: 'node',
3+
testPathIgnorePatterns: [
4+
'/node_modules',
5+
'/dist'
6+
],
7+
globals: {
8+
'ts-jest': {
9+
tsconfig: './tsconfig.json'
10+
}
11+
},
12+
roots: [
13+
'test'
14+
],
15+
transform: {
16+
'^.+\\.(ts)$': 'ts-jest'
17+
}
18+
};

0 commit comments

Comments
 (0)