-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aris
committed
Oct 12, 2020
0 parents
commit 04870ca
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = { | ||
presets: [ | ||
[ | ||
'@babel/preset-env', | ||
{ | ||
targets: { | ||
node: 'current', | ||
}, | ||
}, | ||
], | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}); |