Skip to content

Commit 04870ca

Browse files
author
Aris
committed
initial commit
0 parents  commit 04870ca

File tree

6 files changed

+135
-0
lines changed

6 files changed

+135
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
package-lock.json

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# @arispati/src-crypto
2+
SRC Crypto Library for node.js
3+
4+
## How to Install
5+
- Install with npm
6+
```bash
7+
npm install @arispati/src-crypto
8+
```
9+
10+
## How to Use
11+
```javascript
12+
import SrcCrypto from '@arispati/src-crypto'
13+
14+
// Set the secret key
15+
let secretKey = 's3cr3tK3y' // default secret key
16+
17+
// Initilize CryptoAES Class
18+
let crypto = new SrcCrypto(secretKey)
19+
20+
// Encrypt
21+
let encrypt = crypto.encrypt('@arispati/src-crypto')
22+
// di92SjlPSnFab3JhclI2U0Q5K1k3SVFncTV4VTVzdDlkZTNZT09TV21Jcz0=
23+
24+
// Decrypt
25+
let decrypt = crypto.decrypt(encrypted)
26+
// "@arispati/src-crypto"
27+
```
28+
29+
## Tests
30+
31+
- Install the dev dependencies
32+
```bash
33+
npm install
34+
```
35+
36+
- Run the test command
37+
```bash
38+
npm test
39+
```

babel.config.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/preset-env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
},
10+
],
11+
],
12+
};

package.json

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "@arispati/src-crypto",
3+
"version": "0.1.0",
4+
"description": "SRC Crypto Library",
5+
"main": "src/index.js",
6+
"scripts": {
7+
"test": "jest --verbose ./tests"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/arispati/src-crypto.git"
12+
},
13+
"keywords": [
14+
"src",
15+
"crypto",
16+
"encrypt",
17+
"decrypt"
18+
],
19+
"author": "Muhamad Aris",
20+
"license": "MIT",
21+
"bugs": {
22+
"url": "https://github.com/arispati/src-crypto/issues"
23+
},
24+
"homepage": "https://github.com/arispati/src-crypto#readme",
25+
"dependencies": {
26+
"php-serialize": "^4.0.2"
27+
},
28+
"devDependencies": {
29+
"@babel/core": "^7.11.6",
30+
"@babel/preset-env": "^7.11.5",
31+
"babel-jest": "^26.5.2",
32+
"jest": "^26.5.3"
33+
}
34+
}

src/index.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
import crypto from 'crypto';
4+
import {serialize, unserialize} from 'php-serialize';
5+
6+
export default class SrcCrypto {
7+
constructor (secretKey = 's3cr3tK3y') {
8+
const hashKey = crypto.createHash('sha256').update(secretKey).digest('hex');
9+
10+
this.cryptoKey = hashKey.substr(0, 32);
11+
this.cryptoIv = hashKey.substr(0, 16);
12+
}
13+
14+
encrypt(plaintext) {
15+
let cipher = crypto.createCipheriv('AES-256-CBC', this.cryptoKey, this.cryptoIv);
16+
17+
return Buffer.from(Buffer.concat([
18+
cipher.update(serialize(plaintext)),
19+
cipher.final()
20+
]).toString('base64')).toString('base64');
21+
}
22+
23+
decrypt(chiperText) {
24+
if (chiperText === null || typeof chiperText === 'undefined' || chiperText === '') {
25+
return chiperText;
26+
}
27+
28+
chiperText = Buffer.from(chiperText, 'base64').toString();
29+
30+
let decipher = crypto.createDecipheriv('AES-256-CBC', this.cryptoKey, this.cryptoIv);
31+
32+
return unserialize(Buffer.concat([
33+
decipher.update(chiperText, 'base64'),
34+
decipher.final()
35+
]).toString());
36+
}
37+
}

tests/crypto.test.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import SrcCrypto from '../src/index'
2+
3+
const crypto = new SrcCrypto('zLbFftHXGCdDZM5ceV1wCbny7YBr70GY');
4+
5+
test('Encrypt', () => {
6+
expect(crypto.encrypt('@arispati/src-crypto')).toBe('S0NQaXYwVFhIdi9wNnd3Q0UrK3BmOWNGRmFmWFJ4Q3BIUlVLakhnQysvUT0=')
7+
});
8+
9+
test('Decrypt', () => {
10+
expect(crypto.decrypt('S0NQaXYwVFhIdi9wNnd3Q0UrK3BmOWNGRmFmWFJ4Q3BIUlVLakhnQysvUT0=')).toBe('@arispati/src-crypto')
11+
});

0 commit comments

Comments
 (0)