From d45a96f558aeb806cf7377d4006330fe423e1f3f Mon Sep 17 00:00:00 2001 From: Faisal Hakim Date: Tue, 2 Aug 2016 22:12:56 +0700 Subject: [PATCH] update credential.d.ts --- credential/credential-tests.ts | 29 +++++++++++++++++++++-------- credential/credential.d.ts | 32 +++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 15 deletions(-) diff --git a/credential/credential-tests.ts b/credential/credential-tests.ts index 6013436b05..307201c76e 100644 --- a/credential/credential-tests.ts +++ b/credential/credential-tests.ts @@ -1,16 +1,29 @@ /// +// all from current main repo examples + import * as credential from 'credential'; -credential.hash('password', function(err: Error, hash: string) { - if (err) console.error(err); - else console.log(hash); +var pw = credential(); +var newPassword = 'I have a really great password.'; + +pw.hash(newPassword, function (err, hash) { + if (err) { throw err; } + console.log('Store the password hash.', hash); }); -const hash = '{}'; -const password = 'test'; +var storedHash = { + "hash": "gNofnhlBl36AdRyktwATxKoqWKa6hsIEzwCmW/YXN//7PtiJwCRbepV9fUKu0L9TJELCKoDiBy6rGM8ov7lg2yLY", + "salt": "yyN3KUzlr4KrKWMM2K3d2Ddxf8OTq+vkKG+mtnmQVIibxSJz8drfzkYzqcH0EM+PVKR/1nClRr/CPDuJsq+FOcIw", + "keyLength": 66, + "hashMethod": "pbkdf2", + "iterations": 181019 +}; +var userInput = 'I have a really great password.'; -credential.verify(hash, password, function(err: Error, isValid: boolean) { - if (err) console.error(err); - else console.log(isValid ? 'Password match' : 'Incorrect password'); +pw.verify(storedHash, userInput, function (err, isValid) { + var msg: string; + if (err) { throw err; } + msg = isValid ? 'Passwords match!' : 'Wrong password.'; + console.log(msg); }); diff --git a/credential/credential.d.ts b/credential/credential.d.ts index 2934a090d4..af16e0e412 100644 --- a/credential/credential.d.ts +++ b/credential/credential.d.ts @@ -4,15 +4,33 @@ // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped declare module 'credential' { + interface defaultOptions { + keyLength: number; + work: number; + hashMethod: string; + } - type HashCallback = (err: Error, hash: string) => void; - type VerifyCallback = (err: Error, isValid: boolean) => void; + interface hashObject { + hash: string; + salt: string; + keyLength: number; + hashMethod: string; + iterations: number; + } - namespace credential { - function hash(password: string, callback: HashCallback): void; - function verify(hash: string, password: string, callback: VerifyCallback): void; - } + type HashCallback = (err: Error, hash: hashObject) => void; + type VerifyCallback = (err: Error, isValid: boolean) => void; - export = credential; + function credential(defaultOptions?: defaultOptions): { + hash(password: string, callback: HashCallback): void; + hash(password: string): Promise; + // iterations(work: number, base): number; + verify(hash: hashObject | string, password: string, callback: VerifyCallback): void; + verify(hash: hashObject | string, password: string): Promise; + expired(hash: string, days: number): boolean; + } + namespace credential { } + + export = credential; }