Skip to content

Function: decryptByPassword() ​

decryptByPassword(encrypted, password): Promise<Buffer<ArrayBufferLike>>

Decrypt data encrypted by encryptByPassword, deriving the key from the password and the salt stored in the data (the same scrypt cost).

Parameters ​

encrypted ​

string

The encrypted data, base64url as encryptByPassword returns it.

password ​

string | Uint8Array<ArrayBufferLike>

The password it was encrypted with: a string (used as its UTF-8 bytes) or bytes.

Returns ​

Promise<Buffer<ArrayBufferLike>>

The decrypted bytes (call .toString('utf8') for a string).

Throws ​

When the password is wrong, or the data was changed, truncated or not produced by encryptByPassword (data encrypted by encryptByKey included).

Example ​

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

declare const encrypted: string
declare const passphrase: string

try {
    console.log((await decryptByPassword(encrypted, passphrase)).toString('utf8'))
} catch (error) {
    if (error instanceof DecryptionFailedException) console.log('Wrong passphrase, or changed data')
}

Released under the MIT License.