Skip to content

Commit

Permalink
feat(crypto): add the crypto package
Browse files Browse the repository at this point in the history
  • Loading branch information
IKatsuba committed Dec 30, 2020
1 parent e5ed7aa commit 1626887
Show file tree
Hide file tree
Showing 14 changed files with 322 additions and 1 deletion.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ module.exports = {
'<rootDir>/libs/fs',
'<rootDir>/libs/child-process',
'<rootDir>/libs/http',
'<rootDir>/libs/crypto',
],
};
2 changes: 2 additions & 0 deletions libs/crypto.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# crypto

1 change: 1 addition & 0 deletions libs/crypto/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "extends": "../../.eslintrc.json", "ignorePatterns": ["!**/*"], "rules": {} }
14 changes: 14 additions & 0 deletions libs/crypto/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
displayName: 'crypto',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsConfig: '<rootDir>/tsconfig.spec.json',
},
},
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/libs/crypto',
};
29 changes: 29 additions & 0 deletions libs/crypto/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@rxnode/crypto",
"version": "0.0.0-development",
"license": "MIT",
"keywords": [
"rxjs",
"node",
"rxnode",
"rx-node",
"crypto"
],
"author": "Igor Katsuba <[email protected]>",
"description": "crypto package for rxnode",
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "git+https://github.com/IKatsuba/rxnode.git"
},
"bugs": {
"url": "https://github.com/IKatsuba/rxnode/issues"
},
"homepage": "https://github.com/IKatsuba/rxnode#readme",
"peerDependencies": {
"rxjs": "6.6.3",
"@rxnode/core": "latest"
}
}
1 change: 1 addition & 0 deletions libs/crypto/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/crypto';
49 changes: 49 additions & 0 deletions libs/crypto/src/lib/crypto.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {
generateKeyPair,
pbkdf2,
randomBytes,
randomFill,
scrypt,
} from './crypto';

describe('crypto', () => {
it('generateKeyPair', async () => {
expect(
await generateKeyPair('rsa', {
modulusLength: 4096,
publicKeyEncoding: {
type: 'spki',
format: 'pem',
},
privateKeyEncoding: {
type: 'pkcs8',
format: 'pem',
cipher: 'aes-256-cbc',
passphrase: 'top secret',
},
}).toPromise()
).toEqual([expect.any(String), expect.any(String)]);
});

it('pbkdf2', async () => {
expect(
await pbkdf2('secret', 'salt', 100000, 64, 'sha512').toPromise()
).toEqual(expect.any(Buffer));
});

it('randomBytes', async () => {
expect(await randomBytes(256).toPromise()).toEqual(expect.any(Buffer));
});

it('randomFill', async () => {
expect(await randomFill(Buffer.alloc(10), 5, 5).toPromise()).toEqual(
expect.any(Buffer)
);
});

it('scrypt', async () => {
expect(await scrypt('secret', 'salt', 64).toPromise()).toEqual(
expect.any(Buffer)
);
});
});
142 changes: 142 additions & 0 deletions libs/crypto/src/lib/crypto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import {
DSAKeyPairOptions,
ECKeyPairOptions,
generateKeyPair as originalGenerateKeyPair,
pbkdf2 as originalPbkdf2,
randomBytes as originalRandomBytes,
randomFill as originalRandomFill,
RSAKeyPairOptions,
scrypt as originalScrypt,
ScryptOptions,
} from 'crypto';
import { observablify } from '@rxnode/core';
import { Observable } from 'rxjs';

export function generateKeyPair(
type: 'rsa',
options: RSAKeyPairOptions<'pem', 'pem'>
): Observable<[publicKey: string, privateKey: string]>;
export function generateKeyPair(
type: 'rsa',
options: RSAKeyPairOptions<'pem', 'der'>
): Observable<[publicKey: string, privateKey: Buffer]>;
export function generateKeyPair(
type: 'rsa',
options: RSAKeyPairOptions<'der', 'pem'>
): Observable<[publicKey: Buffer, privateKey: string]>;
export function generateKeyPair(
type: 'rsa',
options: RSAKeyPairOptions<'der', 'der'>
): Observable<[publicKey: Buffer, privateKey: Buffer]>;
export function generateKeyPair(
type: 'dsa',
options: DSAKeyPairOptions<'pem', 'pem'>
): Observable<[publicKey: string, privateKey: string]>;
export function generateKeyPair(
type: 'dsa',
options: DSAKeyPairOptions<'pem', 'der'>
): Observable<[publicKey: string, privateKey: Buffer]>;
export function generateKeyPair(
type: 'dsa',
options: DSAKeyPairOptions<'der', 'pem'>
): Observable<[publicKey: Buffer, privateKey: string]>;
export function generateKeyPair(
type: 'dsa',
options: DSAKeyPairOptions<'der', 'der'>
): Observable<[publicKey: Buffer, privateKey: Buffer]>;
export function generateKeyPair(
type: 'ec',
options: ECKeyPairOptions<'pem', 'pem'>
): Observable<[publicKey: string, privateKey: string]>;
export function generateKeyPair(
type: 'ec',
options: ECKeyPairOptions<'pem', 'der'>
): Observable<[publicKey: string, privateKey: Buffer]>;
export function generateKeyPair(
type: 'ec',
options: ECKeyPairOptions<'der', 'pem'>
): Observable<[publicKey: Buffer, privateKey: string]>;
export function generateKeyPair(
type: 'ec',
options: ECKeyPairOptions<'der', 'der'>
): Observable<[publicKey: Buffer, privateKey: Buffer]>;
export function generateKeyPair(
type: 'rsa' | 'dsa' | 'ec',
options:
| RSAKeyPairOptions<'pem' | 'der', 'pem' | 'der'>
| DSAKeyPairOptions<'pem' | 'der', 'pem' | 'der'>
| ECKeyPairOptions<'pem' | 'der', 'pem' | 'der'>
): Observable<[publicKey: string | Buffer, privateKey: string | Buffer]> {
return observablify<
[
type: 'rsa' | 'dsa' | 'ec',
options:
| RSAKeyPairOptions<'pem' | 'der', 'pem' | 'der'>
| DSAKeyPairOptions<'pem' | 'der', 'pem' | 'der'>
| ECKeyPairOptions<'pem' | 'der', 'pem' | 'der'>
],
[publicKey: string | Buffer, privateKey: string | Buffer]
>(originalGenerateKeyPair)(type, options);
}

export function pbkdf2(
password: string | Buffer | NodeJS.TypedArray | DataView,
salt: string | Buffer | NodeJS.TypedArray | DataView,
iterations: number,
keylen: number,
digest: string
): Observable<Buffer> {
return observablify(originalPbkdf2)(
password,
salt,
iterations,
keylen,
digest
);
}

export function randomBytes(size: number): Observable<Buffer> {
return observablify(originalRandomBytes)(size);
}

export function randomFill<T extends Buffer | NodeJS.TypedArray | DataView>(
buffer: T
): Observable<T>;
export function randomFill<T extends Buffer | NodeJS.TypedArray | DataView>(
buffer: T,
offset: number
): Observable<T>;
export function randomFill<T extends Buffer | NodeJS.TypedArray | DataView>(
buffer: T,
offset: number,
size: number
): Observable<T>;
export function randomFill<T extends Buffer | NodeJS.TypedArray | DataView>(
buffer: T,
offset?: number,
size?: number
): Observable<T> {
return observablify<[buffer: T, offset?: number, size?: number], [buf: T]>(
originalRandomFill
)(buffer, offset, size);
}

export function scrypt(
password: string | Buffer | NodeJS.TypedArray | DataView,
salt: string | Buffer | NodeJS.TypedArray | DataView,
keylen: number
): Observable<Buffer>;
export function scrypt(
password: string | Buffer | NodeJS.TypedArray | DataView,
salt: string | Buffer | NodeJS.TypedArray | DataView,
keylen: number,
options: ScryptOptions
): Observable<Buffer>;
export function scrypt(
password: string | Buffer | NodeJS.TypedArray | DataView,
salt: string | Buffer | NodeJS.TypedArray | DataView,
keylen: number,
options?: ScryptOptions
): Observable<Buffer> {
return observablify(originalScrypt)(password, salt, keylen, options);
}
13 changes: 13 additions & 0 deletions libs/crypto/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
]
}
11 changes: 11 additions & 0 deletions libs/crypto/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["**/*.spec.ts"],
"include": ["**/*.ts"]
}
15 changes: 15 additions & 0 deletions libs/crypto/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"**/*.spec.ts",
"**/*.spec.tsx",
"**/*.spec.js",
"**/*.spec.jsx",
"**/*.d.ts"
]
}
3 changes: 3 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
},
"http": {
"tags": []
},
"crypto": {
"tags": []
}
}
}
3 changes: 2 additions & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"@rxnode/core": ["libs/core/src/index.ts"],
"@rxnode/fs": ["libs/fs/src/index.ts"],
"@rxnode/child_process": ["libs/child-process/src/index.ts"],
"@rxnode/http": ["libs/http/src/index.ts"]
"@rxnode/http": ["libs/http/src/index.ts"],
"@rxnode/crypto": ["libs/crypto/src/index.ts"]
}
},
"exclude": ["node_modules", "tmp"]
Expand Down
39 changes: 39 additions & 0 deletions workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,45 @@
}
}
}
},
"crypto": {
"root": "libs/crypto",
"sourceRoot": "libs/crypto/src",
"projectType": "library",
"schematics": {},
"architect": {
"lint": {
"builder": "@nrwl/linter:eslint",
"options": {
"lintFilePatterns": ["libs/crypto/**/*.ts"]
}
},
"test": {
"builder": "@nrwl/jest:jest",
"options": {
"jestConfig": "libs/crypto/jest.config.js",
"passWithNoTests": true
}
},
"build": {
"builder": "@nrwl/node:package",
"options": {
"outputPath": "dist/libs/crypto",
"tsConfig": "libs/crypto/tsconfig.lib.json",
"packageJson": "libs/crypto/package.json",
"main": "libs/crypto/src/index.ts",
"assets": ["libs/crypto/*.md"]
}
},
"release": {
"builder": "@ng-builders/semrel:release",
"options": {
"npm": {
"pkgRoot": "./dist/libs/crypto"
}
}
}
}
}
},
"cli": {
Expand Down

0 comments on commit 1626887

Please sign in to comment.