From 8bbf0548e54d6b7544d47940058c3bf42c5e71e8 Mon Sep 17 00:00:00 2001 From: SirTobi Date: Sun, 22 Nov 2015 20:38:19 +0100 Subject: [PATCH] add: added type definitions for new jssha version (2.0.2) because of the rework from 1.6 to 2.0 --- jssha/jssha-tests.ts | 50 +++++++++++++++++++++++++ jssha/jssha.d.ts | 87 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 jssha/jssha-tests.ts create mode 100644 jssha/jssha.d.ts diff --git a/jssha/jssha-tests.ts b/jssha/jssha-tests.ts new file mode 100644 index 000000000..def07a2d2 --- /dev/null +++ b/jssha/jssha-tests.ts @@ -0,0 +1,50 @@ +/// +/// + +var imported = require("jssha"); + +// constructor +let shaObj1:jsSHA.jsSHA = imported("SHA-256", "HEX"); +let shaObj2:jsSHA.jsSHA = new jsSHA("SHA-512", "TEXT"); +let shaObj3:jsSHA.jsSHA = new jsSHA("SHA-512", "TEXT", { }); +let shaObj4:jsSHA.jsSHA = new jsSHA("SHA-512", "TEXT", { encoding: "UTF" }); +let shaObj5:jsSHA.jsSHA = new jsSHA("SHA-512", "TEXT", { numRounds: 1 }); +let shaObj6:jsSHA.jsSHA = new jsSHA("SHA-512", "TEXT", { encoding: "UTF", numRounds: 1 }); + +// setHMACKey +shaObj1.setHMACKey("key", "TEXT"); +shaObj1.setHMACKey("key", "TEXT", { }); +shaObj1.setHMACKey("key", "TEXT", { encoding: "UTF" }); + +// update +shaObj1.update("This is a test"); + +// getHash +let hash1:string = shaObj1.getHash("HEX"); +let hash2:string = shaObj1.getHash("HEX", {}); +let hash3:string = shaObj1.getHash("HEX", { b64Pad: "=" }); +let hash4:string = shaObj1.getHash("HEX", { outputUpper: true }); +let hash5:string = shaObj1.getHash("HEX", { outputUpper: true, b64Pad: '=' }); + +// 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: '=' }); + + +// examples from the readme.md (https://github.com/Caligatio/jsSHA/blob/v2.0.2/README.md) +{ + var shaObj = new jsSHA("SHA-512", "TEXT"); + shaObj.update("This is a test"); + var hash = shaObj.getHash("HEX"); +} + + +{ + let shaObj = new jsSHA("SHA-256", "TEXT"); + shaObj.setHMACKey("abc", "TEXT"); + shaObj.update("This is a test"); + let hmac = shaObj.getHMAC("HEX"); +} \ No newline at end of file diff --git a/jssha/jssha.d.ts b/jssha/jssha.d.ts new file mode 100644 index 000000000..8ce44f2c1 --- /dev/null +++ b/jssha/jssha.d.ts @@ -0,0 +1,87 @@ +// Type definitions for jsSHA +// Project: https://github.com/Caligatio/jsSHA +// Definitions by: Tobias Kahlert +// Definitions: https://github.com/SrTobi/DefinitelyTyped + + +declare module jsSHA { + + export interface EncodingOptions { + encoding? : string; + } + + export interface Options extends EncodingOptions { + numRounds? : number; + } + + export interface OutputFormatOptions { + outputUpper? : boolean; + b64Pad? : string; + } + + export interface jsSHA { + /** + * jsSHA is the workhorse of the library. Instantiate it with the string to + * be hashed as the parameter + * + * @constructor + * @this {jsSHA} + * @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 + */ + new (variant:string, inputFormat:string, options?:Options):jsSHA; + + /** + * 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)}=} options 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 {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 + * + * @param {string} format The desired output formatting (B64, HEX, or BYTES) + * @param {{outputUpper : (boolean|undefined), b64Pad : (string|undefined)}=} + * options 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)}=} + * options associative array of output formatting options + * @return {string} The string representation of the hash in the format + * specified + */ + getHMAC(format:string, outputFormatOpts?:OutputFormatOptions):string; + } +} + +declare var jsSHA: jsSHA.jsSHA; +declare module 'jssha' { + export = jsSHA; +} \ No newline at end of file