Class: Encryption
The encryption class allows encrypting and decrypting values using aes-256-cbc or aes-128-cbc algorithms. The encrypted value uses a unique iv for every encryption and this ensures semantic security (read more https://en.wikipedia.org/wiki/Semantic_security).
Constructors
Constructor
new Encryption(options: EncryptionOptions): Encryption;Parameters
| Parameter | Type |
|---|---|
options | EncryptionOptions |
Returns
Encryption
Properties
| Property | Type | Default value | Description |
|---|---|---|---|
base64 | Base64 | base64 | Reference to base64 object for base64 encoding/decoding values |
verifier | MessageVerifier | undefined | Reference to the instance of message verifier for signing and verifying values. |
Accessors
algorithm
Get Signature
get algorithm(): EncryptionAlgorithm;The algorithm in use
Returns
Methods
child()
child(options?: EncryptionOptions): Encryption;Create a children instance with different secret key
Parameters
| Parameter | Type |
|---|---|
options? | EncryptionOptions |
Returns
Encryption
decrypt()
decrypt<T>(value: string, purpose?: string): null | T;Decrypt value and verify it against a purpose
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type |
|---|---|
value | string |
purpose? | string |
Returns
null | T
encodable()
encodable(value: unknown): value is any;Checks if the value can be encoded
Parameters
| Parameter | Type |
|---|---|
value | unknown |
Returns
value is any
encrypt()
encrypt(
payload: any,
expiresIn?: string | number,
purpose?: string): string;Encrypt a given piece of value using the app secret. A wide range of data types are supported. See @nhtio/serialization for more information on the supported data types.
You can optionally define a purpose for which the value was encrypted and mentioning a different purpose/no purpose during decrypt will fail.
Parameters
| Parameter | Type | Description |
|---|---|---|
payload | any | The value to encrypt |
expiresIn? | string | number | The time to live for the value. It can be a number or a string |
purpose? | string | The purpose for which the value was encrypted |
Returns
string
encodable()
static encodable(value: unknown): value is any;Checks if the value can be encoded
Parameters
| Parameter | Type |
|---|---|
value | unknown |
Returns
value is any