diff --git a/jssha/jssha-1.6.0-tests.ts b/jssha/jssha-1.6.0-tests.ts new file mode 100755 index 000000000..67e1ec026 --- /dev/null +++ b/jssha/jssha-1.6.0-tests.ts @@ -0,0 +1,15 @@ +/// +/// + +var imported = require("jssha"); + +var shaObj1:jsSHA.jsSHA = new jsSHA("This is a Test", "TEXT", "UTF8"); +var shaObj2 = new imported("This is a Test", "TEXT"); + +var hash1:string = shaObj2.getHash("SHA-512", "HEX"); +var hash2:string = shaObj2.getHash("SHA-512", "HEX", 2); +var hash3:string = shaObj2.getHash("SHA-512", "HEX", 2, {outputUpper: false, b64Pad: "foobar"}); + +var format:jsSHA.OutputFormatOptions = {outputUpper: false, b64Pad: "foobar"}; +var hmac1 = shaObj2.getHMAC("SecretKey", "TEXT", "SHA-512", "HEX"); +var hmac2 = shaObj2.getHMAC("SecretKey", "TEXT", "SHA-512", "HEX", format); diff --git a/jssha/jssha-1.6.0.d.ts b/jssha/jssha-1.6.0.d.ts new file mode 100755 index 000000000..b6dcef80f --- /dev/null +++ b/jssha/jssha-1.6.0.d.ts @@ -0,0 +1,65 @@ +// Type definitions for jsSHA-1.6.0 +// Project: https://github.com/Caligatio/jsSHA +// Definitions by: David Li +// Definitions: https://github.com/borisyankov/DefinitelyTyped + + +declare module jsSHA { + 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} srcString The string to be hashed + * @param {string} inputFormat The format of srcString, HEX, TEXT, B64, or BYTES + * @param {string=} encoding The text encoding to use to encode the source + * string + */ + new (srcString:string, inputFormat:string, encoding?:string):jsSHA; + + /** + * Returns the desired SHA hash of the string specified at instantiation + * using the specified parameters + * + * @param {string} variant The desired SHA variant (SHA-1, SHA-224, + * SHA-256, SHA-384, or SHA-512) + * @param {string} format The desired output formatting (B64, HEX, or BYTES) + * @param {number=} numRounds The number of rounds of hashing to be + * executed + * @param {{outputUpper : boolean, b64Pad : string}=} outputFormatOpts + * Hash list of output formatting options + * @return {string} The string representation of the hash in the format + * specified + */ + getHash(variant:string, format:string, numRounds?:number, outputFormatOpts?:OutputFormatOptions):string; + + /** + * Returns the desired HMAC of the string specified at instantiation + * using the key and variant parameter + * + * @param {string} key The key used to calculate the HMAC + * @param {string} inputFormat The format of key, HEX, TEXT, B64, or BYTES + * @param {string} variant The desired SHA variant (SHA-1, SHA-224, + * SHA-256, SHA-384, or SHA-512) + * @param {string} outputFormat The desired output formatting + * (B64, HEX, or BYTES) + * @param {{outputUpper : boolean, b64Pad : string}=} outputFormatOpts + * associative array of output formatting options + * @return {string} The string representation of the hash in the format + * specified + */ + getHMAC(key:string, inputFormat:string, variant:string, outputFormat:string, outputFormatOpts?:OutputFormatOptions):string; + } +} + +declare var jsSHA: jsSHA.jsSHA; +declare module 'jssha' { + export = jsSHA; +} diff --git a/jssha/jssha-tests.ts b/jssha/jssha-tests.ts old mode 100755 new mode 100644 index 8d6eec5a6..f6e0f96b4 --- a/jssha/jssha-tests.ts +++ b/jssha/jssha-tests.ts @@ -1,15 +1,49 @@ /// /// -var imported = require("jssha"); +import imported = require("jssha"); -var shaObj1:jsSHA.jsSHA = new jsSHA("This is a Test", "TEXT", "UTF8"); -var shaObj2 = new imported("This is a Test", "TEXT"); +// constructor +let shaObj1:jsSHA.jsSHA = new imported("SHA-512", "TEXT"); +let shaObj2:jsSHA.jsSHA = new imported("SHA-512", "TEXT", { }); +let shaObj3:jsSHA.jsSHA = new imported("SHA-512", "TEXT", { encoding: "UTF8" }); +let shaObj4:jsSHA.jsSHA = new imported("SHA-512", "TEXT", { numRounds: 1 }); +let shaObj5:jsSHA.jsSHA = new imported("SHA-512", "TEXT", { encoding: "UTF8", numRounds: 1 }); -var hash1:string = shaObj2.getHash("SHA-512", "HEX"); -var hash2:string = shaObj2.getHash("SHA-512", "HEX", 2); -var hash3:string = shaObj2.getHash("SHA-512", "HEX", 2, {outputUpper: false, b64Pad: "foobar"}); +// setHMACKey +shaObj1.setHMACKey("key", "TEXT"); +shaObj2.setHMACKey("key", "TEXT", { }); +shaObj3.setHMACKey("key", "TEXT", { encoding: "UTF8" }); -var format:jsSHA.OutputFormatOptions = {outputUpper: false, b64Pad: "foobar"}; -var hmac1 = shaObj2.getHMAC("SecretKey", "TEXT", "SHA-512", "HEX"); -var hmac2 = shaObj2.getHMAC("SecretKey", "TEXT", "SHA-512", "HEX", format); +// update +shaObj1.update("This is a test"); + +// 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: '=' }); + +// 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 imported("SHA-512", "TEXT"); + shaObj.update("This is a test"); + var hash = shaObj.getHash("HEX"); +} + + +{ + let shaObj = new imported("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 old mode 100755 new mode 100644 index 1c75c3862..6dc4f65d3 --- a/jssha/jssha.d.ts +++ b/jssha/jssha.d.ts @@ -1,13 +1,22 @@ // Type definitions for jsSHA // Project: https://github.com/Caligatio/jsSHA -// Definitions by: David Li -// Definitions: https://github.com/borisyankov/DefinitelyTyped +// Definitions by: David Li , Tobias Kahlert +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare module jsSHA { + + export interface EncodingOptions { + encoding? : string; + } + + export interface Options extends EncodingOptions { + numRounds? : number; + } + export interface OutputFormatOptions { - outputUpper : boolean; - b64Pad : string; + outputUpper? : boolean; + b64Pad? : string; } export interface jsSHA { @@ -15,51 +24,62 @@ declare module jsSHA { * jsSHA is the workhorse of the library. Instantiate it with the string to * be hashed as the parameter * - * @constructor - * @this {jsSHA} - * @param {string} srcString The string to be hashed - * @param {string} inputFormat The format of srcString, HEX, TEXT, B64, or BYTES - * @param {string=} encoding The text encoding to use to encode the source - * string + * @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 (srcString:string, inputFormat:string, encoding?:string):jsSHA; + new (variant:string, inputFormat:string, options?:Options):jsSHA; /** - * Returns the desired SHA hash of the string specified at instantiation - * using the specified parameters - * - * @param {string} variant The desired SHA variant (SHA-1, SHA-224, - * SHA-256, SHA-384, or SHA-512) - * @param {string} format The desired output formatting (B64, HEX, or BYTES) - * @param {number=} numRounds The number of rounds of hashing to be - * executed - * @param {{outputUpper : boolean, b64Pad : string}=} outputFormatOpts - * Hash list of output formatting options - * @return {string} The string representation of the hash in the format - * specified - */ - getHash(variant:string, format:string, numRounds?:number, outputFormatOpts?:OutputFormatOptions):string; + * 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 {string} srcString The string to be hashed + */ + update(srcString:string):void; + /** - * Returns the desired HMAC of the string specified at instantiation - * using the key and variant parameter - * - * @param {string} key The key used to calculate the HMAC - * @param {string} inputFormat The format of key, HEX, TEXT, B64, or BYTES - * @param {string} variant The desired SHA variant (SHA-1, SHA-224, - * SHA-256, SHA-384, or SHA-512) - * @param {string} outputFormat The desired output formatting - * (B64, HEX, or BYTES) - * @param {{outputUpper : boolean, b64Pad : string}=} outputFormatOpts - * associative array of output formatting options - * @return {string} The string representation of the hash in the format - * specified - */ - getHMAC(key:string, inputFormat:string, variant:string, outputFormat:string, outputFormatOpts?:OutputFormatOptions):string; + * 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; } } -declare var jsSHA: jsSHA.jsSHA; declare module 'jssha' { + var jsSHA: jsSHA.jsSHA; export = jsSHA; -} +} \ No newline at end of file