Skip to content

Commit

Permalink
Initial working version
Browse files Browse the repository at this point in the history
  • Loading branch information
queicherius committed May 22, 2020
0 parents commit f9a74db
Show file tree
Hide file tree
Showing 13 changed files with 4,410 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"imageSize": 75,
"projectName": "aes-encryption",
"projectOwner": "devoxa",
"repoType": "github",
"repoHost": "https://github.com",
"skipCi": true,
"contributors": [
{
"login": "queicherius",
"name": "David Reeß",
"avatar_url": "https://avatars3.githubusercontent.com/u/4615516?v=4",
"profile": "https://www.david-reess.de",
"contributions": [
"code",
"doc",
"test"
]
}
],
"files": [
"README.md"
],
"contributorsPerLine": 7
}
21 changes: 21 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
version: 2.1
orbs:
node: circleci/[email protected]
jobs:
build-and-test:
executor:
name: node/default
steps:
- checkout
- node/with-cache:
steps:
- run: yarn install
- run: yarn test --coverage
- run: bash <(curl -s https://codecov.io/bash)
- run: yarn format:check
- run: yarn lint
- run: yarn build
workflows:
build-and-test:
jobs:
- build-and-test
28 changes: 28 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
],
root: true,
env: {
node: true,
jest: true,
},
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-use-before-define': [
'error',
{ functions: false, classes: false, variables: true },
],
},
}
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Compiled Output
dist/

# Tests
coverage/

# Dependencies
node_modules/
yarn-debug.log*
yarn-error.log*

# Development Environments
.history/
10 changes: 10 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Tests
coverage/

# Dependencies
node_modules/
yarn-debug.log*
yarn-error.log*

# Development Environments
.history/
13 changes: 13 additions & 0 deletions .prettierrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
printWidth: 100
tabWidth: 2
useTabs: false
semi: false
singleQuote: true
quoteProps: as-needed
jsxSingleQuote: true
trailingComma: es5
bracketSpacing: true
jsxBracketSameLine: false
arrowParens: always
proseWrap: always
endOfLine: lf
92 changes: 92 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<!-- Title -->
<h1 align="center">
aes-encryption
</h1>

<!-- Description -->
<h4 align="center">
Encrypting and decrypting of strings with <code>aes-256-gcm</code>.
</h4>

<!-- Badges -->
<p align="center">
<a href="https://www.npmjs.com/package/@devoxa/aes-encryption">
<img
src="https://img.shields.io/npm/v/@devoxa/aes-encryption?style=flat-square"
alt="Package Version"
/>
</a>

<a href="https://app.circleci.com/pipelines/github/devoxa/aes-encryption?branch=master">
<img
src="https://img.shields.io/circleci/build/github/devoxa/aes-encryption/master?style=flat-square"
alt="Build Status"
/>
</a>

<a href="https://codecov.io/github/devoxa/aes-encryption">
<img
src="https://img.shields.io/codecov/c/github/devoxa/aes-encryption/master?style=flat-square"
alt="Code Coverage"
/>
</a>
</p>

<!-- Quicklinks -->
<p align="center">
<a href="#installation">Installation</a> •
<a href="#usage">Usage</a> •
<a href="#contributors">Contributors</a> •
<a href="#license">License</a>
</p>

<br>

## Installation

```bash
yarn add @devoxa/aes-encryption
```

## Usage

**Under no circumstances use this for passwords. You should be using hashing instead.
[Read more](https://stackoverflow.com/a/477578)**

```ts
import { encrypt, decrypt } from '@devoxa/aes-encryption'

// The 32 character encryption key
const key = 'ZtdDl3Ex7ycFfgdbAC3uTLNk8eLVDcEd'

const encrypted = encrypt(key, 'My secret text')
// -> 'j2G63AgcRSkiFeE4jonB8I/GZYp6Uc40ItdwSappAWi75ItbDzzoOzo7EuaMaA=='

const decrypted = decrypt(key, encrypted)
// -> 'My secret text'
```

## Contributors

Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tr>
<td align="center"><a href="https://www.david-reess.de"><img src="https://avatars3.githubusercontent.com/u/4615516?v=4" width="75px;" alt=""/><br /><sub><b>David Reeß</b></sub></a><br /><a href="https://github.com/devoxa/aes-encryption/commits?author=queicherius" title="Code">💻</a> <a href="https://github.com/devoxa/aes-encryption/commits?author=queicherius" title="Documentation">📖</a> <a href="https://github.com/devoxa/aes-encryption/commits?author=queicherius" title="Tests">⚠️</a></td>
</tr>
</table>

<!-- markdownlint-enable -->
<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors)
specification. Contributions of any kind welcome!

## License

MIT
5 changes: 5 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testPathIgnorePatterns: ['dist'],
}
32 changes: 32 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "@devoxa/aes-encryption",
"description": "Encrypting and decrypting of strings with `aes-256-gcm`",
"version": "0.1.0",
"main": "dist/src/index.js",
"license": "MIT",
"repository": {
"url": "https://github.com/devoxa/aes-encryption"
},
"scripts": {
"test": "jest",
"format": "prettier --ignore-path='.gitignore' --list-different --write .",
"format:check": "prettier --ignore-path='.gitignore' --check .",
"lint": "eslint '{src,tests}/**/*.ts'",
"build": "rm -rf dist/ && tsc"
},
"devDependencies": {
"@types/jest": "^25.2.2",
"@types/node": "^14.0.1",
"@typescript-eslint/eslint-plugin": "^2.33.0",
"@typescript-eslint/parser": "^2.33.0",
"eslint": "^7.0.0",
"eslint-config-prettier": "^6.11.0",
"jest": "^26.0.1",
"prettier": "^2.0.5",
"ts-jest": "^26.0.0",
"typescript": "^3.9.2"
},
"publishConfig": {
"access": "public"
}
}
29 changes: 29 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as crypto from 'crypto'

export function encrypt(key: string, input: string): string {
const keyBuffer = Buffer.from(key)
const iv = crypto.randomBytes(16)

const cipher = crypto.createCipheriv('aes-256-gcm', keyBuffer, iv)

const encrypted = Buffer.concat([cipher.update(input, 'utf8'), cipher.final()])
const tag = cipher.getAuthTag()

return Buffer.concat([iv, tag, encrypted]).toString('base64')
}

export function decrypt(key: string, input: string): string {
const keyBuffer = Buffer.from(key)
const inputBuffer = Buffer.from(input, 'base64')

const iv = inputBuffer.slice(0, 16)
const tag = inputBuffer.slice(16, 32)
const text = inputBuffer.slice(32)

const decipher = crypto.createDecipheriv('aes-256-gcm', keyBuffer, iv)
decipher.setAuthTag(tag)

const decrypted = decipher.update(text, 'binary', 'utf8') + decipher.final('utf8')

return decrypted
}
9 changes: 9 additions & 0 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable @typescript-eslint/camelcase */
import { encrypt, decrypt } from '../src/index'

describe('aes-encryption', () => {
it('can encrypt and decrypt a string', () => {
const KEY = 'gdPfTP7h1hcs1ySGp0vcFwIZVpWfUSqJ'
expect(decrypt(KEY, encrypt(KEY, 'FooBar'))).toEqual('FooBar')
})
})
33 changes: 33 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"compilerOptions": {
// Output Options
"target": "es2015",
"module": "commonjs",
"jsx": "react",
"noEmitOnError": true,
"newLine": "lf",
"outDir": "dist/",
"baseUrl": "./",
"declaration": true,
"sourceMap": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"incremental": true,

// Type-Checking Options
"lib": ["esnext"],
"strict": true,
"skipLibCheck": true,
"strictPropertyInitialization": false,

// Module Resolution Options
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"allowJs": false
},
"include": ["src/", "tests/"]
}
Loading

0 comments on commit f9a74db

Please sign in to comment.