From 6907afd19d05f02694179c4fb494349c1b92ab6e Mon Sep 17 00:00:00 2001 From: Federico Bond Date: Thu, 17 Jan 2019 19:29:40 -0300 Subject: [PATCH] [aes-js] Add types --- types/aes-js/aes-js-tests.ts | 10 +++ types/aes-js/index.d.ts | 134 +++++++++++++++++++++++++++++++++++ types/aes-js/tsconfig.json | 23 ++++++ types/aes-js/tslint.json | 1 + 4 files changed, 168 insertions(+) create mode 100644 types/aes-js/aes-js-tests.ts create mode 100644 types/aes-js/index.d.ts create mode 100644 types/aes-js/tsconfig.json create mode 100644 types/aes-js/tslint.json diff --git a/types/aes-js/aes-js-tests.ts b/types/aes-js/aes-js-tests.ts new file mode 100644 index 0000000000..4f9d582630 --- /dev/null +++ b/types/aes-js/aes-js-tests.ts @@ -0,0 +1,10 @@ +import * as aesjs from 'aes-js'; + +const key = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; + +const data: Uint8Array = aesjs.utils.utf8.toBytes('hello world'); + +const ecb = new aesjs.ModeOfOperation.ecb(key); +ecb.decrypt(ecb.encrypt(data)); + +const hex: string = aesjs.utils.hex.fromBytes(data); diff --git a/types/aes-js/index.d.ts b/types/aes-js/index.d.ts new file mode 100644 index 0000000000..142e03f7d9 --- /dev/null +++ b/types/aes-js/index.d.ts @@ -0,0 +1,134 @@ +// Type definitions for aes-js 3.1 +// Project: https://github.com/ricmoo/aes-js +// Definitions by: Federico Bond +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export type ByteSource = ArrayBuffer | Uint8Array | number[]; + +export class AES { + /** + * Create a new AES block cipher. + * @param key The cipher key. + */ + constructor(key: ByteSource) + encrypt(v: ByteSource): ByteSource; +} + +/** + * Create a new Counter state for CTR cipher mode. + * @param initialValue The Counter initial value. + */ +export class Counter { + constructor(initialValue: number) + setValue(value: number): void; + setBytes(bytes: ByteSource): void; + increment(): void; +} + +export namespace ModeOfOperation { + class ModeOfOperationECB { + /** + * Create a new ECB stream cipher. + * @param key The cipher key. + */ + constructor(key: ByteSource) + encrypt(v: ByteSource): Uint8Array; + decrypt(v: ByteSource): Uint8Array; + } + + class ModeOfOperationCBC { + /** + * Create a new CBC stream cipher. + * @param key The cipher key. + * @param iv The cipher initialization vector. + */ + constructor(key: ByteSource, iv: ByteSource); + encrypt(v: ByteSource): Uint8Array; + decrypt(v: ByteSource): Uint8Array; + } + + class ModeOfOperationCFB { + /** + * Create a new CFB stream cipher. + * @param key The cipher key. + * @param iv The cipher initialization vector. + * @param segmentSize The cipher segment size. + */ + constructor(key: ByteSource, iv: ByteSource, segmentSize: number); + encrypt(v: ByteSource): Uint8Array; + decrypt(v: ByteSource): Uint8Array; + } + + class ModeOfOperationOFB { + /** + * Create a new OFB stream cipher. + * @param key The cipher key. + * @param iv The cipher initialization vector. + */ + constructor(key: ByteSource, iv: ByteSource); + encrypt(v: ByteSource): Uint8Array; + decrypt(v: ByteSource): Uint8Array; + } + + class ModeOfOperationCTR { + /** + * Create a new CTR stream cipher. + * @param key The cipher key. + * @param counter The cipher counter state. + */ + constructor(key: ByteSource, counter?: Counter) + encrypt(v: ByteSource): Uint8Array; + decrypt(v: ByteSource): Uint8Array; + } + + const ecb: typeof ModeOfOperationECB; + const cbc: typeof ModeOfOperationCBC; + const cfb: typeof ModeOfOperationCFB; + const ofb: typeof ModeOfOperationOFB; + const ctr: typeof ModeOfOperationCTR; +} + +export namespace utils { + namespace utf8 { + /** + * Convert a UTF8 encoded string to a Uint8Array. + * @param data The input string. + */ + function toBytes(data: string): Uint8Array; + + /** + * Convert an array-like object containing UTF8 data to a string. + * @param data The input data. + */ + function fromBytes(data: ByteSource): string; + } + namespace hex { + /** + * Convert a hexadecimal string to a Uint8Array. + * @param data The input string. + */ + function toBytes(data: string): Uint8Array; + + /** + * Convert an array-like object to a hexadecimal string. + * @param data The input data. + */ + function fromBytes(data: ByteSource): string; + } +} + +export namespace padding { + namespace pkcs7 { + /** + * Add standard PKCS7 padding to an array. + * @param data The input data. + */ + function pad(data: ByteSource): Uint8Array; + + /** + * Remove standard PKCS7 padding from an array. + * @param data The input data. + */ + function strip(data: ByteSource): Uint8Array; + } +} diff --git a/types/aes-js/tsconfig.json b/types/aes-js/tsconfig.json new file mode 100644 index 0000000000..10f3720c1e --- /dev/null +++ b/types/aes-js/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "aes-js-tests.ts" + ] +} diff --git a/types/aes-js/tslint.json b/types/aes-js/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/aes-js/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }