From d1775959f96e5112c8fda94b171a691738b85c54 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Tue, 18 Jul 2017 21:55:47 +0200 Subject: [PATCH] [@ronomon/crypto-async] introduce typings --- types/ronomon__crypto-async/index.d.ts | 64 +++++++++ .../ronomon__crypto-async-tests.ts | 130 ++++++++++++++++++ types/ronomon__crypto-async/tsconfig.json | 22 +++ types/ronomon__crypto-async/tslint.json | 1 + 4 files changed, 217 insertions(+) create mode 100644 types/ronomon__crypto-async/index.d.ts create mode 100644 types/ronomon__crypto-async/ronomon__crypto-async-tests.ts create mode 100644 types/ronomon__crypto-async/tsconfig.json create mode 100644 types/ronomon__crypto-async/tslint.json diff --git a/types/ronomon__crypto-async/index.d.ts b/types/ronomon__crypto-async/index.d.ts new file mode 100644 index 0000000000..dfc533833a --- /dev/null +++ b/types/ronomon__crypto-async/index.d.ts @@ -0,0 +1,64 @@ +// Type definitions for @ronomon/crypto-async 2.0 +// Project: https://github.com/ronomon/crypto-async +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +// tslint:disable-next-line no-single-declare-module +declare module "@ronomon/crypto-async" { + function cipher(algorithm: string, + cipherDirection: CipherDirection, + key: Buffer, + iv: Buffer, + plaintext: Buffer, + cb: (error: Error | undefined, ciphertext: Buffer) => void): void; + + function cipher(algorithm: string, + cipherDirection: CipherDirection, + key: Buffer, + keyOffset: number, + keySize: number, + iv: Buffer, + ivOffset: number, + ivSize: number, + source: Buffer, + sourceOffset: number, + sourceSize: number, + target: Buffer, + targetOffset: number, + cb: (error: Error | undefined, targetSize: number) => void): void; + + function hash(algorithm: string, + source: Buffer, + cb: (error: Error | undefined, hash: Buffer) => void): void; + + function hash(algorithm: string, + source: Buffer, + sourceOffset: number, + sourceSize: number, + target: Buffer, + targetOffset: number, + cb: (error: Error | undefined, targetSize: number) => void): void; + + function hmac(algorithm: string, + key: Buffer, + source: Buffer, + cb: (error: Error | undefined, hmac: Buffer) => void): void; + + function hmac(algorithm: string, + key: Buffer, + keyOffset: number, + keySize: number, + source: Buffer, + sourceOffset: number, + sourceSize: number, + target: Buffer, + targetOffset: number, + cb: (error: Error | undefined, targetSize: number) => void): void; + + const enum CipherDirection { + Decrypt, + Encrypt + } +} diff --git a/types/ronomon__crypto-async/ronomon__crypto-async-tests.ts b/types/ronomon__crypto-async/ronomon__crypto-async-tests.ts new file mode 100644 index 0000000000..429affe3a3 --- /dev/null +++ b/types/ronomon__crypto-async/ronomon__crypto-async-tests.ts @@ -0,0 +1,130 @@ +import * as cryptoAsync from '@ronomon/crypto-async'; + +(() => { + const algorithm = 'AES-256-CTR'; + const encrypt = cryptoAsync.CipherDirection.Encrypt; + const key = Buffer.alloc(32); + const iv = Buffer.alloc(16); + const plaintext = Buffer.alloc(128); + cryptoAsync.cipher(algorithm, encrypt, key, iv, plaintext, + (error, ciphertext) => { + if (error) throw error; + console.log(ciphertext.toString('hex')); + cryptoAsync.cipher(algorithm, cryptoAsync.CipherDirection.Decrypt, key, iv, ciphertext, + (error, plaintext) => { + if (error) throw error; + console.log(plaintext.toString('hex')); + } + ); + } + ); +})(); + +(() => { + const algorithm = 'AES-256-CTR'; + const encrypt = 1; // 0 = Decrypt, 1 = Encrypt + const key = Buffer.alloc(1024); + const keyOffset = 4; + const keySize = 32; + const iv = Buffer.alloc(32); + const ivOffset = 2; + const ivSize = 16; + const source = Buffer.alloc(1024 * 1024); + const sourceOffset = 512; + const sourceSize = 32; + const target = Buffer.alloc(1024 * 1024); + const targetOffset = 32768; + cryptoAsync.cipher( + algorithm, + encrypt, + key, + keyOffset, + keySize, + iv, + ivOffset, + ivSize, + source, + sourceOffset, + sourceSize, + target, + targetOffset, + (error, targetSize) => { + if (error) throw error; + const slice = target.slice(targetOffset, targetOffset + targetSize); + console.log(slice.toString('hex')); + } + ); +})(); + +(() => { + const algorithm = 'SHA256'; + const source = Buffer.alloc(1024 * 1024); + cryptoAsync.hash(algorithm, source, + (error, hash) => { + if (error) throw error; + console.log(hash.toString('hex')); + } + ); +})(); + +(() => { + const algorithm = 'SHA256'; + const source = Buffer.alloc(1024 * 1024); + const sourceOffset = 512; + const sourceSize = 65536; + const target = Buffer.alloc(1024 * 1024); + const targetOffset = 32768; + cryptoAsync.hash( + algorithm, + source, + sourceOffset, + sourceSize, + target, + targetOffset, + (error, targetSize) => { + if (error) throw error; + const slice = target.slice(targetOffset, targetOffset + targetSize); + console.log(slice.toString('hex')); + } + ); +})(); + +(() => { + const algorithm = 'SHA256'; + const key = Buffer.alloc(1024); + const source = Buffer.alloc(1024 * 1024); + cryptoAsync.hmac(algorithm, key, source, + (error, hmac) => { + if (error) throw error; + console.log(hmac.toString('hex')); + } + ); +})(); + +(() => { + const algorithm = 'SHA256'; + const key = Buffer.alloc(1024); + const keyOffset = 4; + const keySize = 8; + const source = Buffer.alloc(1024 * 1024); + const sourceOffset = 512; + const sourceSize = 65536; + const target = Buffer.alloc(1024 * 1024); + const targetOffset = 32768; + cryptoAsync.hmac( + algorithm, + key, + keyOffset, + keySize, + source, + sourceOffset, + sourceSize, + target, + targetOffset, + (error, targetSize) => { + if (error) throw error; + const slice = target.slice(targetOffset, targetOffset + targetSize); + console.log(slice.toString('hex')); + } + ); +})(); diff --git a/types/ronomon__crypto-async/tsconfig.json b/types/ronomon__crypto-async/tsconfig.json new file mode 100644 index 0000000000..217a5f51db --- /dev/null +++ b/types/ronomon__crypto-async/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "ronomon__crypto-async-tests.ts" + ] +} diff --git a/types/ronomon__crypto-async/tslint.json b/types/ronomon__crypto-async/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/ronomon__crypto-async/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }