Skip to content

Commit

Permalink
test: refine testing conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanlescure committed Oct 18, 2020
1 parent 4cf844e commit 79d6ed7
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 51 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
"types": "dist/index.d.ts",
"runkitExampleFilename": "./runkit.js",
"scripts": {
"build": "rollup -c",
"build": "rollup -c && rm -f dist/index.test.d.ts",
"dev": "concurrently \"rollup -cw\" \"nodemon --inspect runkit.js\"",
"prepare": "yarn link && yarn link string-crypto",
"test": "ts-node test.ts"
"test": "ts-node --files ./src/index.test.ts && yarn build && node runkit.js"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^15.0.0",
Expand Down
3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ const config = {
external: [
// ...Object.keys(pkg.dependencies),
...Object.keys(pkg.devDependencies),
'fs',
'crypto',
'buffer',
],
};

Expand Down
14 changes: 14 additions & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare type StringLike = string | Buffer | Uint8Array | Int8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | DataView;
declare type Digest = 'md5' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512' | 'rmd160' | 'ripemd160';
interface DeriveKeyOpts {
salt?: StringLike;
iterations?: number;
keylen?: number;
digest?: Digest;
}
declare class StringCrypto {
private _deriveKeyOptions;
constructor(options?: DeriveKeyOpts);
encryptString: (str: StringLike, password: StringLike) => string;
decryptString: (encryptedStr: StringLike, password: StringLike) => string;
}
12 changes: 4 additions & 8 deletions test.ts → src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import assert from 'assert';
import StringCrypto, {
Digest,
deriveKey,
defaultDeriveKeyOpts,
} from './src';
import StringCrypto from '.';

const testPassword = 'test';
const testMessage = 'Hello World';

const derivedKey = deriveKey(testPassword, defaultDeriveKeyOpts).toString();
let sc = new StringCrypto();

const derivedKey = sc.deriveKey(testPassword, StringCrypto.defaultDeriveKeyOpts).toString();

assert(derivedKey !== testPassword);
assert(derivedKey.length === 30);

let sc = new StringCrypto();

const messageEncWDefaults = sc.encryptString(testMessage, testPassword);

assert(messageEncWDefaults !== testMessage);
Expand Down
71 changes: 33 additions & 38 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,14 @@ const btoa = (str: string): string => {
return Buffer.from(str, 'binary').toString('base64');
};

type StringLike = string | Buffer | Uint8Array | Int8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | DataView;

export type Digest = 'md5' | 'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512' | 'rmd160' | 'ripemd160';

interface DeriveKeyOpts {
salt?: StringLike;
iterations?: number;
keylen?: number;
digest?: Digest;
}

export const defaultDeriveKeyOpts: DeriveKeyOpts = {
salt: 's41t',
iterations: 1,
keylen: 256 / 8,
digest: 'sha512',
};

export const deriveKey = (
password: StringLike,
options?: DeriveKeyOpts,
) => {
const {
salt,
iterations,
keylen,
digest,
} = {
...defaultDeriveKeyOpts,
...options,
class StringCrypto {
static defaultDeriveKeyOpts: DeriveKeyOpts = {
salt: 's41t',
iterations: 1,
keylen: 256 / 8,
digest: 'sha512',
};

return pbkdf2Sync(password, salt, iterations, keylen, digest);
};

class StringCrypto {
private _deriveKeyOptions: DeriveKeyOpts;

constructor(options?: DeriveKeyOpts) {
Expand All @@ -67,7 +39,27 @@ class StringCrypto {
}
}

encryptString = (str: StringLike, password: StringLike): string => {
deriveKey = (
password: StringLike,
options?: DeriveKeyOpts,
) => {
const {
salt,
iterations,
keylen,
digest,
} = {
...StringCrypto.defaultDeriveKeyOpts,
...options,
};

return pbkdf2Sync(password, salt, iterations, keylen, digest);
};

encryptString = (
str: StringLike,
password: StringLike,
): string => {
let base64String: string = btoa(unescape(encodeURIComponent(str.toString())));
const mod16Len = base64String.length % 16;

Expand All @@ -77,7 +69,7 @@ class StringCrypto {

const stringBytes = utf8.toBytes(base64String);

const derivedKey = deriveKey(password, this._deriveKeyOptions);
const derivedKey = this.deriveKey(password, this._deriveKeyOptions);

const randomInitVector = randomBytes(16);

Expand All @@ -92,8 +84,11 @@ class StringCrypto {
return `${initVectorHex}:${encryptedHex}`;
};

decryptString = (encryptedStr: StringLike, password: StringLike): string => {
const derivedKey = deriveKey(password, this._deriveKeyOptions);
decryptString = (
encryptedStr: StringLike,
password: StringLike,
): string => {
const derivedKey = this.deriveKey(password, this._deriveKeyOptions);

const encryptedParts: string[] = encryptedStr.toString().split(':');

Expand Down
10 changes: 8 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
"target": "ES5",
"sourceMap": true,
"pretty": true,
"esModuleInterop": true
"esModuleInterop": true,
"outDir":"dist"
},
"include": ["src"]
"include": [
"./src"
],
"files": [
"./src/index.d.ts"
]
}

0 comments on commit 79d6ed7

Please sign in to comment.