[@ronomon/crypto-async] introduce typings

This commit is contained in:
Dimitri Benin
2017-07-18 21:55:47 +02:00
parent 2ff74fbcac
commit d1775959f9
4 changed files with 217 additions and 0 deletions

64
types/ronomon__crypto-async/index.d.ts vendored Normal file
View File

@@ -0,0 +1,64 @@
// Type definitions for @ronomon/crypto-async 2.0
// Project: https://github.com/ronomon/crypto-async
// Definitions by: BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference types="node"/>
// 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
}
}

View File

@@ -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'));
}
);
})();

View File

@@ -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"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }