Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Aris committed Oct 12, 2020
0 parents commit 04870ca
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
package-lock.json
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# @arispati/src-crypto
SRC Crypto Library for node.js

## How to Install
- Install with npm
```bash
npm install @arispati/src-crypto
```

## How to Use
```javascript
import SrcCrypto from '@arispati/src-crypto'

// Set the secret key
let secretKey = 's3cr3tK3y' // default secret key

// Initilize CryptoAES Class
let crypto = new SrcCrypto(secretKey)

// Encrypt
let encrypt = crypto.encrypt('@arispati/src-crypto')
// di92SjlPSnFab3JhclI2U0Q5K1k3SVFncTV4VTVzdDlkZTNZT09TV21Jcz0=

// Decrypt
let decrypt = crypto.decrypt(encrypted)
// "@arispati/src-crypto"
```

## Tests

- Install the dev dependencies
```bash
npm install
```

- Run the test command
```bash
npm test
```
12 changes: 12 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
presets: [
[
'@babel/preset-env',
{
targets: {
node: 'current',
},
},
],
],
};
34 changes: 34 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@arispati/src-crypto",
"version": "0.1.0",
"description": "SRC Crypto Library",
"main": "src/index.js",
"scripts": {
"test": "jest --verbose ./tests"
},
"repository": {
"type": "git",
"url": "git+https://github.com/arispati/src-crypto.git"
},
"keywords": [
"src",
"crypto",
"encrypt",
"decrypt"
],
"author": "Muhamad Aris",
"license": "MIT",
"bugs": {
"url": "https://github.com/arispati/src-crypto/issues"
},
"homepage": "https://github.com/arispati/src-crypto#readme",
"dependencies": {
"php-serialize": "^4.0.2"
},
"devDependencies": {
"@babel/core": "^7.11.6",
"@babel/preset-env": "^7.11.5",
"babel-jest": "^26.5.2",
"jest": "^26.5.3"
}
}
37 changes: 37 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict';

import crypto from 'crypto';
import {serialize, unserialize} from 'php-serialize';

export default class SrcCrypto {
constructor (secretKey = 's3cr3tK3y') {
const hashKey = crypto.createHash('sha256').update(secretKey).digest('hex');

this.cryptoKey = hashKey.substr(0, 32);
this.cryptoIv = hashKey.substr(0, 16);
}

encrypt(plaintext) {
let cipher = crypto.createCipheriv('AES-256-CBC', this.cryptoKey, this.cryptoIv);

return Buffer.from(Buffer.concat([
cipher.update(serialize(plaintext)),
cipher.final()
]).toString('base64')).toString('base64');
}

decrypt(chiperText) {
if (chiperText === null || typeof chiperText === 'undefined' || chiperText === '') {
return chiperText;
}

chiperText = Buffer.from(chiperText, 'base64').toString();

let decipher = crypto.createDecipheriv('AES-256-CBC', this.cryptoKey, this.cryptoIv);

return unserialize(Buffer.concat([
decipher.update(chiperText, 'base64'),
decipher.final()
]).toString());
}
}
11 changes: 11 additions & 0 deletions tests/crypto.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import SrcCrypto from '../src/index'

const crypto = new SrcCrypto('zLbFftHXGCdDZM5ceV1wCbny7YBr70GY');

test('Encrypt', () => {
expect(crypto.encrypt('@arispati/src-crypto')).toBe('S0NQaXYwVFhIdi9wNnd3Q0UrK3BmOWNGRmFmWFJ4Q3BIUlVLakhnQysvUT0=')
});

test('Decrypt', () => {
expect(crypto.decrypt('S0NQaXYwVFhIdi9wNnd3Q0UrK3BmOWNGRmFmWFJ4Q3BIUlVLakhnQysvUT0=')).toBe('@arispati/src-crypto')
});

0 comments on commit 04870ca

Please sign in to comment.