Skip to content

Function: encryptByPassword() ​

encryptByPassword(data, password): Promise<string>

Encrypt data with a password chosen by a person, using AES-256-GCM (authenticated encryption) with a key derived from the password and a random 16-byte salt by scrypt (N = 16384, r = 8, p = 1: about 16 MB of memory and a few tens of milliseconds per call, off the main thread). The slow derivation makes guessing the password costly; a strong password still matters. For a random key, encryptByKey is faster. To check a login password, use PasswordHash instead.

Parameters ​

data ​

string | Uint8Array<ArrayBufferLike>

The data to encrypt: a string (encrypted as its UTF-8 bytes) or bytes.

password ​

string | Uint8Array<ArrayBufferLike>

The password: a string (used as its UTF-8 bytes) or bytes. Its length is not checked.

Returns ​

Promise<string>

The encrypted data, base64url encoded without padding, in the format of encryptByKey with another version byte: the two are not interchangeable.

Example ​

typescript
import {decryptByPassword, encryptByPassword} from 'lakutata/security'

const encrypted: string = await encryptByPassword('backup contents', 'a long passphrase of the user')
console.log((await decryptByPassword(encrypted, 'a long passphrase of the user')).toString('utf8'))

Released under the MIT License.