work
Derives a key from data such as a password, using PBKDF2 with 100,000 iterations of SHA-256. It's deliberately slow, which makes guessing the original data expensive.
Parameter | Description |
|---|---|
| The data to derive a key from, in any format that can be converted to JSON. |
| Optional. Use the same salt to derive the same key again. Without one, a random salt is used. |
| Optional function that receives the key. Leave it out to |
Returns a pair object containing only an epriv key, so it can be passed to encrypt() and decrypt().
Without a salt, each call gives a different key that can't be derived again. To use work() for something like a password, store a salt alongside the encrypted data and pass it every time.
Examples
const salt = "a random string stored with the data"
// With await.
const key = await holster.SEA.work("my password", salt)
const enc = await holster.SEA.encrypt("hello work", key)
// Later, the same password and salt give the same key.
const again = await holster.SEA.work("my password", salt)
const dec = await holster.SEA.decrypt(enc, again)
console.log(dec) // "hello work"
// With a callback.
holster.SEA.work("my password", salt, key => console.log(key.epriv))
// Without a salt, the callback can be the second parameter.
holster.SEA.work("one-off data", key => console.log(key.epriv))This is how the User API protects an account's private keys: they're encrypted with a key derived from the password, and stored with the salt.