Crypto

Collection of cryptographic functions.

Static properties

forge object

The Forge library (node-forge) (advanced usage).

node object

The Node Crypto library (advanced usage).

Methods

encrypt

encrypt(text: string, key: string)

Encrypts a string using the AES algorithm. Returns a base64 encoded string. The result is not deterministic.

Parameters

text string

The text to encrypt.

key string

The encryption key.

Return type

string

Examples

decrypt

Decrypts a string using the AES algorithm.

Parameters

text string

The text to decrypt.

key string

The encryption key.

Return type

string

Examples

convertString

Converts a string from one encoding to another.

Parameters

str string

The string to convert.

inputEncoding 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'utf-16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex'

The encoding of the input string.

outputEncoding 'ascii' | 'utf8' | 'utf-8' | 'utf16le' | 'utf-16le' | 'ucs2' | 'ucs-2' | 'base64' | 'base64url' | 'latin1' | 'binary' | 'hex'

The encoding of the output string.

Return type

string

Examples

hash

Calculates the hash of a string using a specific algorithm.

Parameters

text string

The plain text to hash.

algorithm string

The algorithm to use. E.g. "sha256" or "sha512".

outputEncoding string

(optional) The encoding of the output. Default is 'base64'. Other options are 'hex' and 'binary'.

Return type

string

Examples

hmacHash

Calculates the HMAC hash of a string using a base64 key.

Parameters

text string

The plain text to hash.

key string

The base64 key.

algorithm string

The algorithm to use. E.g. "sha256" or "sha512".

outputEncoding string

(optional) The encoding of the output. Default is 'base64'. Other options are 'hex' and 'binary'.

Return type

string

Examples

sign

Signs a string using a specific algorithm.

Parameters

text string

The plain text to sign.

key string | { key?: string, passphrase?: string, padding?: number, saltLength?: number, format?: string, type?: string, encoding?: string }

The key to use for signing.

algorithm string

(optional) The algorithm to use. E.g. "sha256" or "sha512". Default is "sha256".

outputEncoding string

(optional) The encoding of the output. Default is 'base64'. Other options are 'hex' and 'binary'.

Return type

string

Examples

sha256

Calculates the SHA256 hash of a string.

Parameters

text string

The plain text to hash.

outputEncoding string

(optional) The encoding of the output. Default is 'base64'. Other options are 'hex' and 'binary'.

Return type

string

Examples

hmacSha256

Calculates the HMAC SHA256 hash of a string using a base64 key.

Parameters

text string

The plain text to hash.

key string

The base64 key.

outputEncoding string

(optional) The encoding of the output. Default is 'base64'. Other options are 'hex' and 'binary'.

Return type

string

Examples

getPrivateKeyFromCertificate

Converts a base64 encoded PKCS12 DER certificate to a PEM private key

Parameters

certificate string

The base64 certificate.

Return type

string

Examples

getClientAssertion

Creates a JWT client_assertion string for use in an OAuth 2.0 call from a certificate thumbprint and private key

Parameters

tokenEndpoint string

The token endpoint the client assertion is used for.

issuer string

The issuer where the client assertion is for.

expirationTime number

The expiration time of the client assertion.

certificateThumbprint string

The certificate thumbprint that is used to sign the client assertion.

privateKey string

The private key that is used to sign the client assertion.

Return type

string

Examples

Last updated