Skip to content

feat: encrypt the value before storing if a secretKey was specified #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export declare class Webstorable {
}
export declare let WEBSTORAGE_CONFIG: {
prefix: string;
secretKey: string;
};
export declare class WebStorageModule {
}
3 changes: 2 additions & 1 deletion dist/index.js

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

2 changes: 1 addition & 1 deletion dist/index.js.map

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

2 changes: 2 additions & 0 deletions dist/utility/webstorage.utility.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ export declare class WebStorageUtility {
static remove(storage: Storage, key: string): void;
private static getSettable(value);
private static getGettable(value);
private static encrypt(value, password);
private static decrypt(value, password);
}
31 changes: 30 additions & 1 deletion dist/utility/webstorage.utility.js

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

2 changes: 1 addition & 1 deletion dist/utility/webstorage.utility.js.map

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

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,9 @@
"tslint": "^3.15.1",
"typescript": "^2.2.1",
"zone.js": "^0.7.2"
},
"dependencies": {
"@types/crypto-js": "^3.1.33",
"crypto-js": "^3.1.9-1"
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export declare class Webstorable {
save(): void;
}
export let WEBSTORAGE_CONFIG = {
prefix: 'angular2ws_'
prefix: 'angular2ws_',
secretKey: ''
};

@NgModule({
Expand Down
42 changes: 35 additions & 7 deletions src/utility/webstorage.utility.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
import {WEBSTORAGE_CONFIG} from '../index';
import * as CryptoJS from 'crypto-js';

export class WebStorageUtility {
static generateStorageKey(key: string): string {
return `${WEBSTORAGE_CONFIG.prefix}${key}`
if (WEBSTORAGE_CONFIG.secretKey) {
return `${WEBSTORAGE_CONFIG.prefix}enc_${key}`;
}
return `${WEBSTORAGE_CONFIG.prefix}${key}`;
}

static get(storage: Storage, key: string): any {
let storageKey = WebStorageUtility.generateStorageKey(key);

const storageKey = WebStorageUtility.generateStorageKey(key);
let value = storage.getItem(storageKey);

if (WEBSTORAGE_CONFIG.secretKey && value !== null) {
value = this.decrypt(value, WEBSTORAGE_CONFIG.secretKey) || '';
}
return WebStorageUtility.getGettable(value);
}

static set(storage: Storage, key: string, value: any): void {
let storageKey = WebStorageUtility.generateStorageKey(key);

storage.setItem(storageKey, WebStorageUtility.getSettable(value));
const storageKey = WebStorageUtility.generateStorageKey(key);
let strValue = WebStorageUtility.getSettable(value);
if (WEBSTORAGE_CONFIG.secretKey) {
strValue = this.encrypt(strValue, WEBSTORAGE_CONFIG.secretKey);
}
storage.setItem(storageKey, strValue);
}

static remove(storage: Storage, key: string): void {
Expand All @@ -37,4 +45,24 @@ export class WebStorageUtility {
return value;
}
}

private static encrypt(value: string, password: string): string {
// Prepend sha256(value) to the value, we can use it to verify the decrypted result.
const newValue = CryptoJS.SHA256(value).toString() + value;
return CryptoJS.AES.encrypt(newValue, password).toString();
}

// Returns null if the password was incorrect.
private static decrypt(value: string, password: string): string {
const decrypted = CryptoJS.AES.decrypt(value, password).toString(CryptoJS.enc.Utf8);
if (decrypted.length < 64) {
return null;
}
const sha256 = decrypted.substr(0, 64);
const realValue = decrypted.substr(64);
if (CryptoJS.SHA256(realValue).toString() !== sha256) {
return null;
}
return realValue;
}
}