2.0.0 • Published 4 years ago
ihacks-hash v2.0.0
An idiot's guide to good hashing
Download the hashing module.
yarn add ihacks-hash
npm i -S ihacks-hash
pnpm install ihacks-hash
Docs
hash(hash, text, key?, rounds?, salt?)
Hash some content into a string component.
Parameter | Type | Optional | Default | Description |
---|---|---|---|---|
hash | HashName | No | None | The name of the hash you want to use. |
text | string | No | None | The text you want to hash. |
key | string | Yes | undefined | Whether or not to use a key whilst hashing. Recommended. |
rounds | number | Yes | 1 | How many rounds to hash some content. |
salt | string | Yes | undefined | The salt to use when hashing, this is only here for my convenience, do not use. |
Returns: (string
) The hashes content.
test(data, text, key?)
Test if some text would be equal to some hashed content when hashed.
Parameter | Type | Optional | Default | Description |
---|---|---|---|---|
data | string | No | None | The hashed string component you want to test against. |
text | string | No | None | The unhashed text you want to test against the hash. |
key | string | Yes | undefined | Whether or not to use a key whilst hashing. Recommended. |
Returns: (boolean
) Whether or not the text was the same as the data when hashed.
HashName
Type: string
HashName is an enumerable (type) of the list of supported hashing algorithms.
Example (basic hash & test)
// Import the module.
const { hash, test } = require("ihacks-hash");
// Information
const password = "MyVeryFakePassword123";
// Hash a password.
const hashed = hash("sha512", password);
const hashed2 = hash("sha512", password);
// Test the hash.
test(hashed, password); // true
hashed === hashed2; // false
Example (basic hash w/ key)
// Import the module.
const { hash, test } = require("ihacks-hash");
// Server Key
const key = "Super Secret Server Key";
// Information
const password = "MyVeryFakePassword123";
// Hash a password.
const hashed = hash("sha512", password, key);
const hashed2 = hash("sha512", password, key);
// Test the hash.
test(hashed, password, key); // true
hashed === hashed2; // false
Example (basic hash w/ multiple hashing rounds)
// Import the module.
const { hash, test } = require("ihacks-hash");
// How many rounds to hash.
const rounds = 32; // Recommended value: 32
// Server Key
const key = "Super Secret Server Key";
// Information
const password = "MyVeryFakePassword123";
// Hash a password.
const hashed = hash("sha512", password, key, rounds);
const hashed2 = hash("sha512", password, key, rounds);
// Test the hash.
test(hashed, password); // true
hashed === hashed2; // false
Example (basic hash w/ key & multiple hashing rounds)
// Import the module.
const { hash, test } = require("ihacks-hash");
// How many rounds to hash.
const rounds = 32; // Recommended value: 32
// Information
const password = "MyVeryFakePassword123";
// Hash a password.
const hashed = hash("sha512", password, null, rounds);
const hashed2 = hash("sha512", password, null, rounds);
// Test the hash.
test(hashed, password); // true
hashed === hashed2; // false