diff --git a/types/jssha/index.d.ts b/types/jssha/index.d.ts index 45d6686aed..e717e23f17 100644 --- a/types/jssha/index.d.ts +++ b/types/jssha/index.d.ts @@ -1,81 +1,81 @@ -// Type definitions for jsSHA +// Type definitions for jsSHA 2.0 // Project: https://github.com/Caligatio/jsSHA -// Definitions by: David Li , Tobias Kahlert +// Definitions by: David Li +// Tobias Kahlert +// Kannan Goundan // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped - declare namespace jsSHA { + type StringDataFormat = 'HEX' | 'TEXT' | 'B64' | 'BYTES'; + type ArrayBufferDataFormat = 'ARRAYBUFFER'; - export interface EncodingOptions { - encoding? : string; + interface EncodingOptions { + encoding?: string; } - export interface Options extends EncodingOptions { - numRounds? : number; + interface Options extends EncodingOptions { + numRounds?: number; } - export interface OutputFormatOptions { - outputUpper? : boolean; - b64Pad? : string; + interface OutputFormatOptions { + outputUpper?: boolean; + b64Pad?: string; + shakeLen?: number; } - export interface jsSHA { + interface jsSHA { /** - * jsSHA is the workhorse of the library. Instantiate it with the string to - * be hashed as the parameter + * Create a Hasher object. * - * @param {string} variant The desired SHA variant (SHA-1, SHA-224, SHA-256, - * SHA-384, or SHA-512) - * @param {string} inputFormat The format of srcString: HEX, TEXT, B64, or BYTES - * @param {{encoding: (string|undefined), numRounds: (string|undefined)}=} - * options Optional values + * @param variant - The desired SHA variant (SHA-1, SHA-224, SHA-256, + * SHA-384, SHA-512, SHA3-224, SHA3-256, SHA3-384, SHA3-512, SHAKE128, or SHAKE256). + * @param inputFormat The format of srcString. */ - new (variant:string, inputFormat:string, options?:Options):jsSHA; + new(variant: string, inputFormat: StringDataFormat, options?: Options): Hasher; + new(variant: string, inputFormat: ArrayBufferDataFormat, options?: Options): Hasher; + } + + interface Hasher { + /** + * Sets the HMAC key for an eventual getHMAC call. Must be called + * immediately after jsSHA object instantiation + * + * @param key - The key used to calculate the HMAC. + * @param inputFormat - The format of key. + */ + setHMACKey(key: string, inputFormat: StringDataFormat, encodingOpts?: EncodingOptions): void; + setHMACKey(key: ArrayBuffer, inputFormat: ArrayBufferDataFormat, encodingOpts?: EncodingOptions): void; /** - * Sets the HMAC key for an eventual getHMAC call. Must be called - * immediately after jsSHA object instantiation - * - * @param {string} key The key used to calculate the HMAC - * @param {string} inputFormat The format of key, HEX, TEXT, B64, or BYTES - * @param {{encoding : (string|undefined)}=} encodingOpts Associative array - * of input format options - */ - setHMACKey(key:string, inputFormat:string, encodingOpts?:EncodingOptions):void; + * Takes strString and hashes as many blocks as possible. Stores the + * rest for either a future update or getHash call. + * + * @param srcString - The string to be hashed + */ + update(srcString: InputT): void; /** - * Takes strString and hashes as many blocks as possible. Stores the - * rest for either a future update or getHash call. - * - * @param {string} srcString The string to be hashed - */ - update(srcString:string):void; - + * Returns the desired SHA hash of the string specified at instantiation + * using the specified parameters + * + * @expose + * @param format - The desired output formatting. + * @returns The string representation of the hash + * in the format specified. + */ + getHash(format: StringDataFormat, outputFormatOpts?: OutputFormatOptions): string; + getHash(format: ArrayBufferDataFormat, outputFormatOpts?: OutputFormatOptions): ArrayBuffer; /** - * Returns the desired SHA hash of the string specified at instantiation - * using the specified parameters - * - * @param {string} format The desired output formatting (B64, HEX, or BYTES) - * @param {{outputUpper : (boolean|undefined), b64Pad : (string|undefined)}=} - * outputFormatOpts Hash list of output formatting options - * @return {string} The string representation of the hash in the format - * specified - */ - getHash(format:string, outputFormatOpts?:OutputFormatOptions):string; - - /** - * Returns the the HMAC in the specified format using the key given by - * a previous setHMACKey call. - * - * @param {string} format The desired output formatting - * (B64, HEX, or BYTES) - * @param {{outputUpper : (boolean|undefined), b64Pad : (string|undefined)}=} - * outputFormatOpts associative array of output formatting options - * @return {string} The string representation of the hash in the format - * specified - */ - getHMAC(format:string, outputFormatOpts?:OutputFormatOptions):string; + * Returns the the HMAC in the specified format using the key given by + * a previous setHMACKey call. + * + * @param format - The desired output formatting. + * @returns The string representation of the hash in the format + * specified + */ + getHMAC(format: StringDataFormat, outputFormatOpts?: OutputFormatOptions): string; + getHMAC(format: ArrayBufferDataFormat, outputFormatOpts?: OutputFormatOptions): ArrayBuffer; } } diff --git a/types/jssha/jssha-tests.ts b/types/jssha/jssha-tests.ts index bbff8dedca..c4df337ea8 100644 --- a/types/jssha/jssha-tests.ts +++ b/types/jssha/jssha-tests.ts @@ -1,53 +1,56 @@ import imported = require("jssha"); // constructor -let shaObj1 = new imported("SHA-512", "TEXT"); -let shaObj2 = new imported("SHA-512", "TEXT", { }); -let shaObj3 = new imported("SHA-512", "TEXT", { encoding: "UTF8" }); -let shaObj4 = new imported("SHA-512", "TEXT", { numRounds: 1 }); -let shaObj5 = new imported("SHA-512", "TEXT", { encoding: "UTF8", numRounds: 1 }); +const shaObj1 = new imported("SHA-512", "TEXT"); +const shaObj2 = new imported("SHA-512", "TEXT", { }); +const shaObj3 = new imported("SHA-512", "TEXT", { encoding: "UTF8" }); +const shaObj4 = new imported("SHA-512", "TEXT", { numRounds: 1 }); +const shaObj5 = new imported("SHA-512", "TEXT", { encoding: "UTF8", numRounds: 1 }); +const shakeObj = new imported("SHAKE256", "ARRAYBUFFER"); // setHMACKey shaObj1.setHMACKey("key", "TEXT"); shaObj2.setHMACKey("key", "TEXT", { }); shaObj3.setHMACKey("key", "TEXT", { encoding: "UTF8" }); +shaObj3.setHMACKey(new ArrayBuffer(10), "ARRAYBUFFER"); // update shaObj1.update("This is a test"); +shakeObj.update(new ArrayBuffer(10)); // getHash -let hash1:string = shaObj4.getHash("HEX"); -let hash2:string = shaObj4.getHash("HEX", {}); -let hash3:string = shaObj4.getHash("HEX", { b64Pad: "=" }); -let hash4:string = shaObj4.getHash("HEX", { outputUpper: true }); -let hash5:string = shaObj4.getHash("HEX", { outputUpper: true, b64Pad: '=' }); +const hash1: string = shaObj4.getHash("HEX"); +const hash2: string = shaObj4.getHash("HEX", {}); +const hash3: string = shaObj4.getHash("HEX", { b64Pad: "=" }); +const hash4: string = shaObj4.getHash("HEX", { outputUpper: true }); +const hash5: string = shaObj4.getHash("HEX", { outputUpper: true, b64Pad: '=' }); +const hash6: ArrayBuffer = shakeObj.getHash("ARRAYBUFFER", { shakeLen: 128 }); // getHMAC -let hmac1:string = shaObj1.getHMAC("HEX"); -let hmac2:string = shaObj1.getHMAC("HEX", {}); -let hmac3:string = shaObj1.getHMAC("HEX", { b64Pad: "=" }); -let hmac4:string = shaObj1.getHMAC("HEX", { outputUpper: true }); -let hmac5:string = shaObj1.getHMAC("HEX", { outputUpper: true, b64Pad: '=' }); - +const hmac1: string = shaObj1.getHMAC("HEX"); +const hmac2: string = shaObj1.getHMAC("HEX", {}); +const hmac3: string = shaObj1.getHMAC("HEX", { b64Pad: "=" }); +const hmac4: string = shaObj1.getHMAC("HEX", { outputUpper: true }); +const hmac5: string = shaObj1.getHMAC("HEX", { outputUpper: true, b64Pad: '=' }); +const hmac6: ArrayBuffer = shaObj1.getHMAC("ARRAYBUFFER", { shakeLen: 128 }); // examples from the readme.md (https://github.com/Caligatio/jsSHA/blob/v2.0.2/README.md) { - var shaObj = new imported("SHA-512", "TEXT"); + const shaObj = new imported("SHA-512", "TEXT"); shaObj.update("This is a test"); - var hash = shaObj.getHash("HEX"); + const hash = shaObj.getHash("HEX"); } - { - let shaObj = new imported("SHA-256", "TEXT"); + const shaObj = new imported("SHA-256", "TEXT"); shaObj.setHMACKey("abc", "TEXT"); shaObj.update("This is a test"); - let hmac = shaObj.getHMAC("HEX"); + const hmac = shaObj.getHMAC("HEX"); } // Browser global test { - var shaObj = new imported("SHA-512", "TEXT"); + const shaObj = new imported("SHA-512", "TEXT"); shaObj.update("This is a test"); - var hash = shaObj.getHash("HEX"); -} \ No newline at end of file + const hash = shaObj.getHash("HEX"); +} diff --git a/types/jssha/tsconfig.json b/types/jssha/tsconfig.json index 9e54d3c27a..c97f7cc2b8 100644 --- a/types/jssha/tsconfig.json +++ b/types/jssha/tsconfig.json @@ -10,7 +10,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ @@ -20,4 +20,4 @@ "noEmit": true, "forceConsistentCasingInFileNames": true } -} \ No newline at end of file +} diff --git a/types/jssha/tslint.json b/types/jssha/tslint.json index a41bf5d19a..f93cf8562a 100644 --- a/types/jssha/tslint.json +++ b/types/jssha/tslint.json @@ -1,79 +1,3 @@ { - "extends": "dtslint/dt.json", - "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false - } + "extends": "dtslint/dt.json" }