-
Notifications
You must be signed in to change notification settings - Fork 27
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
Showing
14 changed files
with
2,400 additions
and
300 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,23 @@ | ||
const gulp = require('gulp'); | ||
const ts = require('gulp-typescript'); | ||
const JSON_FILES = ['src/*.json', 'src/**/*.json']; | ||
|
||
// pull in the project TypeScript config | ||
const tsProject = ts.createProject('tsconfig.json'); | ||
|
||
gulp.task('scripts', () => { | ||
const tsResult = tsProject.src() | ||
.pipe(tsProject()); | ||
return tsResult.js.pipe(gulp.dest('src')); | ||
}); | ||
|
||
gulp.task('watch', ['scripts'], () => { | ||
gulp.watch('src/**/*.ts', ['scripts']); | ||
}); | ||
|
||
gulp.task('assets', function() { | ||
return gulp.src(JSON_FILES) | ||
.pipe(gulp.dest('src')); | ||
}); | ||
|
||
gulp.task('default', ['watch', 'assets']); |
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 |
---|---|---|
@@ -1,15 +1,27 @@ | ||
{ | ||
"name": "simple-crypto-js", | ||
"version": "1.1.1", | ||
"version": "2.0.0", | ||
"description": "Simplify AES encryption and decryption of any JavaScript objects, implementing crypto-js library.", | ||
"main": "crypto.js", | ||
"main": "src/SimpleCrypto.js", | ||
"homepage": "https://github.com/danang-id/simple-crypto-js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"build": "gulp scripts", | ||
"test": "mocha --reporter spec --require ts-node/register 'test/**/*.test.ts'" | ||
}, | ||
"dependencies": { | ||
"crypto-js": "^3.1.9-1" | ||
}, | ||
"author": "Danang Galuh Tegar Prasetyo", | ||
"license": "MIT" | ||
"author": "https://github.com/danang-id", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/chai": "^4.1.3", | ||
"@types/crypto-js": "^3.1.39", | ||
"@types/mocha": "^5.2.0", | ||
"chai": "^4.1.2", | ||
"gulp": "^3.9.1", | ||
"gulp-typescript": "^4.0.2", | ||
"mocha": "^5.2.0", | ||
"ts-node": "^6.0.5", | ||
"typescript": "^2.8.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,36 @@ | ||
/** Declaration file generated by dts-gen */ | ||
|
||
declare class SimpleCrypto { | ||
constructor(...args: any[]); | ||
|
||
decrypt(...args: any[]): void; | ||
|
||
decryptObject(...args: any[]): void; | ||
|
||
encrypt(...args: any[]): void; | ||
|
||
encryptObject(...args: any[]): void; | ||
|
||
setSecret(...args: any[]): void; | ||
|
||
static generateRandom(...args: any[]): void; | ||
|
||
} | ||
|
||
|
||
declare namespace SimpleCrypto { | ||
namespace prototype { | ||
function decrypt(...args: any[]): void; | ||
|
||
function decryptObject(...args: any[]): void; | ||
|
||
function encrypt(...args: any[]): void; | ||
|
||
function encryptObject(...args: any[]): void; | ||
|
||
function setSecret(...args: any[]): void; | ||
|
||
} | ||
|
||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,72 @@ | ||
import * as CryptoJS from 'crypto-js'; | ||
|
||
export default class SimpleCrypto { | ||
|
||
private _secret: string; | ||
private _keySize: number; | ||
private _iterations: number; | ||
|
||
public constructor(secret) { | ||
if (secret === void 0) throw new Error('SimpleCrypto object MUST BE initialised with a SECRET KEY.'); | ||
this._secret = secret; | ||
this._keySize = 256; | ||
this._iterations = 100; | ||
} | ||
|
||
public static generateRandom(length: number = 128, expectsWordArray: boolean = false): string | CryptoJS.WordArray { | ||
const random = CryptoJS.lib.WordArray.random(length/8); | ||
return expectsWordArray ? random : random.toString(); | ||
} | ||
|
||
public encrypt(data: object | string | number | boolean): string { | ||
if (data == void 0) throw new Error('No data was attached to be encrypted. Encryption halted.'); | ||
const string: string = typeof data == "object" ? JSON.stringify(data) : typeof data == "string" || typeof data == "number" || typeof data == 'boolean' ? data.toString() : null; | ||
if (null === string) throw new Error('Only object, string, number and boolean data types that can be encrypted.'); | ||
const salt: string | CryptoJS.WordArray = SimpleCrypto.generateRandom(128, true); | ||
const key: CryptoJS.WordArray = CryptoJS.PBKDF2(this._secret, salt, { | ||
keySize: this._keySize / 32, | ||
iterations: this._iterations | ||
}); | ||
const initialVector: string | CryptoJS.WordArray = SimpleCrypto.generateRandom(128, true); | ||
const encrypted: CryptoJS.WordArray = CryptoJS.AES.encrypt(string, key, { | ||
iv: initialVector, | ||
padding: CryptoJS.pad.Pkcs7, | ||
mode: CryptoJS.mode.CBC | ||
}); | ||
const ciphered: string = salt.toString() + initialVector.toString() + encrypted.toString(); | ||
return ciphered; | ||
} | ||
|
||
public decrypt(ciphered: string, expectsObject: boolean = false, enc: CryptoJS.Encoder = CryptoJS.enc.Utf8): string | object { | ||
if (ciphered == void 0) throw new Error('No encrypted string was attached to be decrypted. Decryption halted.'); | ||
const salt: string = CryptoJS.enc.Hex.parse(ciphered.substr(0, 32)); | ||
const initialVector: string = CryptoJS.enc.Hex.parse(ciphered.substr(32, 32)); | ||
const encrypted: string = ciphered.substring(64); | ||
const key: string | CryptoJS.WordArray = CryptoJS.PBKDF2(this._secret, salt, { | ||
keySize: this._keySize / 32, | ||
iterations: this._iterations | ||
}); | ||
const decrypted = CryptoJS.AES.decrypt(encrypted, key, { | ||
iv: initialVector, | ||
padding: CryptoJS.pad.Pkcs7, | ||
mode: CryptoJS.mode.CBC | ||
}); | ||
const data: string | object = expectsObject ? JSON.parse(decrypted.toString(enc)) : decrypted.toString(enc); | ||
return data; | ||
} | ||
|
||
public encryptObject(object: object): string { | ||
return this.encrypt(object); | ||
} | ||
|
||
public decryptObject(string: string): object { | ||
const decrypted: string | object = this.decrypt(string, true); | ||
const object: object = typeof decrypted == 'object' ? decrypted : JSON.parse(decrypted); | ||
return object; | ||
} | ||
|
||
public setSecret(secret: string): void { | ||
this._secret = secret; | ||
} | ||
|
||
} |
Oops, something went wrong.