Arrow keys: ← previous · → next 3 of 35
@nodedk/cipher
0.2.2 stable

cipher#

Password hashing, random tokens, UUIDs, and content hashes using node:crypto.

Install#

npm i @nodedk/cipher

Usage#

var cipher = require('@nodedk/cipher')

var password = 'example-password'
var stored = cipher.cloak(password)

console.log(cipher.match(password, stored)) // true
console.log(cipher.match('another-password', stored)) // false

var id = cipher.uuid()
var token = cipher.token(32)
var digest = cipher.hash('hello')

API#

All methods are synchronous. Password derivation blocks the calling thread while it runs.

uuid()#

Returns a random UUID v4 string.

token(length = 32)#

Returns random bytes encoded as hexadecimal. length is a positive integer in bytes; the returned string is twice that length. A falsy length uses 32 bytes.

cloak(password)#

Takes a password string or Buffer. Returns hash:salt, using PBKDF2 with SHA-512, 600,000 iterations, a 64-byte derived key, and a random 16-byte salt. Store the entire returned string.

match(password, storedValue)#

Compares a password string or Buffer with a stored hash:salt string. Returns a boolean. Values without exactly two colon-separated parts or with an incorrect hash length return false.

hash(input, type = 'sha256')#

Returns a hexadecimal content hash. input is a string or Buffer; type is an algorithm supported by the runtime. Use cloak for passwords.

var cipher = require('@nodedk/cipher')

console.log(cipher.hash('hello', 'sha512'))
console.log(cipher.token(16).length) // 32

Created by Vidar Eldøy