Function: generateSecret()
generateSecret(
bytes?):string
Generate a random secret key from the operating system's cryptographically secure generator (crypto.randomBytes), suited to the keys of Mac and Crypt, hmac and encryptByKey, and to random tokens (session IDs, reset links). It is not a password and should be stored as a secret (an environment variable, a vault).
Parameters
bytes?
number
The number of random bytes; 32 (256 bits) by default, at least 16 for a secret (0 gives an empty string). The keys of Mac and Crypt need 12 bytes or more to reach their 16-character minimum.
Returns
string
The random bytes, base64url encoded without padding: 43 characters for 32 bytes, 22 for 16.
Throws
When bytes is negative or larger than 2^31 - 1.
Example
typescript
import {generateSecret} from 'lakutata/security'
//A 256-bit key, for MAC_KEY or CRYPT_KEY
console.log(generateSecret())
//A 128-bit token
const token: string = generateSecret(16)
console.log(token)