Skip to content

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

ts
new Encryption(options: EncryptionOptions): Encryption;

Parameters

ParameterType
optionsEncryptionOptions

Returns

Encryption

Properties

PropertyTypeDefault valueDescription
base64Base64base64Reference to base64 object for base64 encoding/decoding values
verifierMessageVerifierundefinedReference to the instance of message verifier for signing and verifying values.

Accessors

algorithm

Get Signature

ts
get algorithm(): EncryptionAlgorithm;

The algorithm in use

Returns

EncryptionAlgorithm

Methods

child()

ts
child(options?: EncryptionOptions): Encryption;

Create a children instance with different secret key

Parameters

ParameterType
options?EncryptionOptions

Returns

Encryption


decrypt()

ts
decrypt<T>(value: string, purpose?: string): null | T;

Decrypt value and verify it against a purpose

Type Parameters

Type Parameter
T

Parameters

ParameterType
valuestring
purpose?string

Returns

null | T


encodable()

ts
encodable(value: unknown): value is any;

Checks if the value can be encoded

Parameters

ParameterType
valueunknown

Returns

value is any


encrypt()

ts
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

ParameterTypeDescription
payloadanyThe value to encrypt
expiresIn?string | numberThe time to live for the value. It can be a number or a string
purpose?stringThe purpose for which the value was encrypted

Returns

string


encodable()

ts
static encodable(value: unknown): value is any;

Checks if the value can be encoded

Parameters

ParameterType
valueunknown

Returns

value is any