infinity.bcrypt¶
Provides bcrypt hashing and verifying functionality for strings.
Module: infinity.crypto
infinity.loadModule('infinity.crypto');
let hash = infinity.bcrypt.hash('dataToHash', 10);
console.debug(hash);
//$2a$10$3.ywjLH6iKKThUQl1iWNu.udUXZChZIxqJaVfTVph1LJcKTLUFyFi (differs on every call because of the random salt)
console.debug(infinity.bcrypt.verify('dataToHash', hash));
//true
hash()¶
Returns the bcrypt hash of the given string, using a random 16-byte salt. The result is a 60-character string in the format $2a$<two-digit cost>$<22 characters salt><31 characters hash>. The password is encoded as UTF-8; only the first 72 bytes are significant. Returns an empty string if password is empty.
Signature:
hash( password: string, cost?: number ): string
Parameters:
-
password:
stringThe string to serve as a password.
-
cost:
number, optionalThe cost factor (base-2 logarithm of the number of rounds) between
4and31. Defaults to11(also used if0is passed). Each increment doubles the computation time. Other values throw an exception.
Return type: string
Example:
infinity.loadModule('infinity.crypto');
let hash = infinity.bcrypt.hash(dataToHash, 10);
verify()¶
Returns whether the provided hash corresponds to the given string. Cost and salt are taken from the hash; hashes with the prefixes $2$, $2a$, $2b$ and $2y$ are accepted. The comparison is done in constant time. Returns false if password or hash is empty. Throws an exception if hash is not a valid bcrypt hash.
Signature:
verify( password: string, hash: string ): boolean
Parameters:
-
password:
stringThe original string used for the creation of the hash for the function to return true.
-
hash:
stringThe hash to be checked.
Return type: boolean
Example:
infinity.loadModule('infinity.crypto');
console.debug(infinity.bcrypt.verify(dataToVerify, hash));