From 8f808f0a03b198505835d65a22897aa998e313d1 Mon Sep 17 00:00:00 2001 From: David Broder-Rodgers Date: Wed, 23 Dec 2015 11:03:25 +0000 Subject: [PATCH] Added typings for bcrypt-nodejs --- bcrypt-nodejs/bcrypt-nodejs-tests.ts | 30 ++++++++++++ bcrypt-nodejs/bcrypt-nodejs.d.ts | 68 ++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 bcrypt-nodejs/bcrypt-nodejs-tests.ts create mode 100644 bcrypt-nodejs/bcrypt-nodejs.d.ts diff --git a/bcrypt-nodejs/bcrypt-nodejs-tests.ts b/bcrypt-nodejs/bcrypt-nodejs-tests.ts new file mode 100644 index 0000000000..2a151c1c7e --- /dev/null +++ b/bcrypt-nodejs/bcrypt-nodejs-tests.ts @@ -0,0 +1,30 @@ +/// + +import bCrypt = require("bcrypt-nodejs"); + +function test_sync() { + var salt1 = bCrypt.genSaltSync(); + var salt2 = bCrypt.genSaltSync(8); + + var hash1 = bCrypt.hashSync('super secret'); + var hash2 = bCrypt.hashSync('super secret', salt1); + + var compare1 = bCrypt.compareSync('super secret', hash1); + + var rounds1 = bCrypt.getRounds(hash2); +} + +function test_async() { + var cbString = (error: Error, result: string) => {}; + var cbVoid = () => {}; + var cbBoolean = (error: Error, result: boolean) => {}; + + bCrypt.genSalt(8, cbString); + + var salt = bCrypt.genSaltSync(); + bCrypt.hash('super secret', salt, cbString); + bCrypt.hash('super secret', salt, cbVoid, cbString); + + var hash = bCrypt.hashSync('super secret'); + bCrypt.compare('super secret', hash, cbBoolean); +} \ No newline at end of file diff --git a/bcrypt-nodejs/bcrypt-nodejs.d.ts b/bcrypt-nodejs/bcrypt-nodejs.d.ts new file mode 100644 index 0000000000..e0a46ffa52 --- /dev/null +++ b/bcrypt-nodejs/bcrypt-nodejs.d.ts @@ -0,0 +1,68 @@ +// Type definitions for bcrypt-nodejs +// Project: https://github.com/shaneGirish/bcrypt-nodejs +// Definitions by: David Broder-Rodgers +// Definitions: https://github.com/DavidBR-SW/DefinitelyTyped + +declare module "bcrypt-nodejs" { + /** + * Generate a salt synchronously + * @param rounds Number of rounds to process the data for (default - 10) + * @return Generated salt + */ + export function genSaltSync(rounds?: number): string; + + /** + * Generate a salt asynchronously + * @param rounds Number of rounds to process the data for (default - 10) + * @param callback Callback with error and resulting salt, to be fired once the salt has been generated + */ + export function genSalt(rounds: number, callback: (error: Error, result: string) => void): void; + + /** + * Generate a hash synchronously + * @param data Data to be encrypted + * @param salt Salt to be used in encryption (default - new salt generated with 10 rounds) + * @return Generated hash + */ + export function hashSync(data: string, salt?: string): string; + + /** + * Generate a hash asynchronously + * @param data Data to be encrypted + * @param salt Salt to be used in encryption + * @param callback Callback with error and hashed result, to be fired once the data has been encrypted + */ + export function hash(data: string, salt: string, callback: (error: Error, result: string) => void): void; + + /** + * Generate a hash asynchronously + * @param data Data to be encrypted + * @param salt Salt to be used in encryption + * @param progressCallback Callback to be fired multiple times during the hash calculation to signify progress + * @param callback Callback with error and hashed result, to be fired once the data has been encrypted + */ + export function hash(data: string, salt: string, progressCallback: () => void, callback: (error: Error, result: string) => void): void; + + /** + * Compares data with a hash synchronously + * @param data Data to be compared + * @param hash Hash to be compared to + * @return true if matching, false otherwise + */ + export function compareSync(data: string, hash: string): boolean; + + /** + * Compares data with a hash asynchronously + * @param data Data to be compared + * @param hash Hash to be compared to + * @param callback Callback with error and match result, to be fired once the data has been compared + */ + export function compare(data: string, hash: string, callback: (error: Error, result: boolean) => void): void; + + /** + * Get number of rounds used for hash + * @param hash Hash from which the number of rounds used should be extracted + * @return number of rounds used to encrypt a given hash + */ + export function getRounds(hash: string): number; +}