-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(crypto): add the crypto package
- Loading branch information
Showing
14 changed files
with
322 additions
and
1 deletion.
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
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,2 @@ | ||
# crypto | ||
|
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 @@ | ||
{ "extends": "../../.eslintrc.json", "ignorePatterns": ["!**/*"], "rules": {} } |
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,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', | ||
}; |
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,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" | ||
} | ||
} |
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 @@ | ||
export * from './lib/crypto'; |
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,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) | ||
); | ||
}); | ||
}); |
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,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); | ||
} |
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,13 @@ | ||
{ | ||
"extends": "../../tsconfig.base.json", | ||
"files": [], | ||
"include": [], | ||
"references": [ | ||
{ | ||
"path": "./tsconfig.lib.json" | ||
}, | ||
{ | ||
"path": "./tsconfig.spec.json" | ||
} | ||
] | ||
} |
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,11 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"outDir": "../../dist/out-tsc", | ||
"declaration": true, | ||
"types": ["node"] | ||
}, | ||
"exclude": ["**/*.spec.ts"], | ||
"include": ["**/*.ts"] | ||
} |
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,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" | ||
] | ||
} |
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 |
---|---|---|
|
@@ -36,6 +36,9 @@ | |
}, | ||
"http": { | ||
"tags": [] | ||
}, | ||
"crypto": { | ||
"tags": [] | ||
} | ||
} | ||
} |
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
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