From ded5d86b471cea2e54acfe9d672e251ee8f6ce96 Mon Sep 17 00:00:00 2001 From: Nico Jansen Date: Mon, 5 Feb 2018 10:26:33 +0100 Subject: [PATCH] feat(js-sha512): add js-sha512 types --- types/js-sha512/index.d.ts | 40 ++++++++++++++++++++++ types/js-sha512/js-sha512-tests.ts | 53 ++++++++++++++++++++++++++++++ types/js-sha512/tsconfig.json | 23 +++++++++++++ types/js-sha512/tslint.json | 1 + 4 files changed, 117 insertions(+) create mode 100644 types/js-sha512/index.d.ts create mode 100644 types/js-sha512/js-sha512-tests.ts create mode 100644 types/js-sha512/tsconfig.json create mode 100644 types/js-sha512/tslint.json diff --git a/types/js-sha512/index.d.ts b/types/js-sha512/index.d.ts new file mode 100644 index 0000000000..ad7c0844b1 --- /dev/null +++ b/types/js-sha512/index.d.ts @@ -0,0 +1,40 @@ +// Type definitions for js-sha512 0.7 +// Project: https://github.com/emn178/js-sha512 +// Definitions by: Nico Jansen +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +interface Hash { + update(messageToHash: string): Hash; + hex(): string; + array(): number[]; + digest(): number[]; + arrayBuffer(): ArrayBuffer; +} + +interface HashStatic { + (messageToHash: string): string; + hex(messageToHash: string): string; + array(messageToHash: string): number[]; + digest(messageToHash: string): number[]; + arrayBuffer(messageToHash: string): ArrayBuffer; + create(): Hash; + update(messageToHash: string): Hash; + hmac: HmacStatic; +} + +interface JSSha512Static extends HashStatic { + sha512: HashStatic; + sha384: HashStatic; + sha512_224: HashStatic; + sha512_256: HashStatic; + create(): Hash; +} + +interface HmacStatic { + (key: string, messageToHash: string): string; + create(key: string): Hash; + update(key: string, messageToHash: string): Hash; +} + +declare const jsSha512: JSSha512Static; +export = jsSha512; diff --git a/types/js-sha512/js-sha512-tests.ts b/types/js-sha512/js-sha512-tests.ts new file mode 100644 index 0000000000..8d1ce687c5 --- /dev/null +++ b/types/js-sha512/js-sha512-tests.ts @@ -0,0 +1,53 @@ +import jsSha512 = require('js-sha512'); +import { sha384, sha512, sha512_224, sha512_256 } from 'js-sha512'; + +sha512('Message to hash'); +sha384('Message to hash'); +sha512_256('Message to hash'); +sha512_224('Message to hash'); + +const hash = sha512.create(); +hash.update('Message to hash'); +hash.hex(); + +const hash2 = sha512.update('Message to hash'); +hash2.update('Message2 to hash'); +hash2.array(); + +// HMAC +sha512.hmac('key', 'Message to hash'); +sha384.hmac('key', 'Message to hash'); + +const hash3 = sha512.hmac.create('key'); +hash3.update('Message to hash'); +hash3.hex(); + +const hash4 = sha512.hmac.update('key', 'Message to hash'); +hash4.update('Message2 to hash'); +hash4.array(); + +sha512(''); // cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e +sha512('The quick brown fox jumps over the lazy dog'); // 07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6 +sha512('The quick brown fox jumps over the lazy dog.'); // 91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed +sha384(''); // 38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b +sha384('The quick brown fox jumps over the lazy dog'); // ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1 +sha384('The quick brown fox jumps over the lazy dog.'); // ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7 +sha512_256(''); // c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a +sha512_256('The quick brown fox jumps over the lazy dog'); // dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d +sha512_256('The quick brown fox jumps over the lazy dog.'); // 1546741840f8a492b959d9b8b2344b9b0eb51b004bba35c0aebaac86d45264c3 +sha512_224(''); // 6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4 +sha512_224('The quick brown fox jumps over the lazy dog'); // 944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37 +sha512_224('The quick brown fox jumps over the lazy dog.'); // 6d6a9279495ec4061769752e7ff9c68b6b0b3c5a281b7917ce0572de + +// It also supports UTF-8 encoding +sha512('中文'); // 8b88efc2ebbcbdad5ac2d65af05bec57bda25e71fd5fb25bbd892057a2755fbd05d8d8491cb2946febd5b0f124ffdfbaecf7e34946353c4f1b5ab29545895468 +sha384('中文'); // 93422ceb8291a69b22f02dc1114c39a287493ad525dcebc77e4019a44eaee2633a85d0f29cd298ee6799048c33a4be0c +sha512_256('中文'); // b6dab29c16ec35ab34a5d92ff135b58de96741dda78b1009a2181cf8b45d2f72 +sha512_224('中文'); // 0f46a0ae7f226517dd66ece0ce1efa29ffb7ced05ac4566fdcaed188 + +// Different output +sha512(''); // cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e +sha512.hex(''); // cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e +sha512.array(''); // [207, 131, ..., 62] +sha512.digest(''); // [207, 131, ..., 62] +sha512.arrayBuffer(''); // ArrayBuffer diff --git a/types/js-sha512/tsconfig.json b/types/js-sha512/tsconfig.json new file mode 100644 index 0000000000..7777de3803 --- /dev/null +++ b/types/js-sha512/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", + "js-sha512-tests.ts" + ] +} diff --git a/types/js-sha512/tslint.json b/types/js-sha512/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/js-sha512/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }