Skip to content

Commit

Permalink
Version 2 upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
danang-id committed May 28, 2018
1 parent 2397c86 commit 74c2e5e
Show file tree
Hide file tree
Showing 14 changed files with 2,400 additions and 300 deletions.
3 changes: 0 additions & 3 deletions .jshintrc

This file was deleted.

280 changes: 104 additions & 176 deletions README.md

Large diffs are not rendered by default.

56 changes: 0 additions & 56 deletions crypto.js

This file was deleted.

60 changes: 0 additions & 60 deletions crypto.ts

This file was deleted.

23 changes: 23 additions & 0 deletions gulpfile.js
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']);
22 changes: 17 additions & 5 deletions package.json
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"
}
}
36 changes: 36 additions & 0 deletions src/SimpleCrypto.d.ts
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;

}

}

72 changes: 72 additions & 0 deletions src/SimpleCrypto.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/SimpleCrypto.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions src/SimpleCrypto.ts
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;
}

}
Loading

0 comments on commit 74c2e5e

Please sign in to comment.