crypto

Provides cryptographic functions for hashing, encryption, and secure random string generation.
Description:
  • Provides cryptographic functions for hashing, encryption, and secure random string generation.

Methods

(static) CRC32(inputString) → {number}

Description:
  • Computes the CRC32 checksum for a string.
Example
const checksum = crypto.CRC32("Hello, World!");
console.log("CRC32 Checksum:", checksum);
Parameters:
Name Type Description
inputString string The string to process.
Returns:
The CRC32 checksum as an integer.
Type
number

(static) MD5(inputString) → {string}

Description:
  • Computes the MD5 hash for a string.
Example
const hash = crypto.MD5("Hello, World!");
console.log("MD5 Hash:", hash);
Parameters:
Name Type Description
inputString string The string to process.
Returns:
The MD5 hash as a hex string.
Type
string

(static) SHA2(inputString) → {string}

Description:
  • Computes the SHA256 hash for a string. (SHA2 is a family, SHA256 is the most common)
Example
const hash = crypto.SHA2("Hello, World!");
console.log("SHA256 Hash:", hash);
Parameters:
Name Type Description
inputString string The string to process.
Returns:
The SHA256 hash as a hex string.
Type
string

(static) SHA3(inputString) → {string}

Description:
  • Computes the SHA3-256 hash for a string.
Example
const hash = crypto.SHA3("Hello, World!");
console.log("SHA3-256 Hash:", hash);
Parameters:
Name Type Description
inputString string The string to process.
Returns:
The SHA3-256 hash as a hex string.
Type
string

(static) decrypt(encryptedPackage, secret) → {string|null}

Description:
  • Decrypts text that was encrypted with the encrypt() function.
Example
const decrypted = crypto.decrypt("iv:authtag:encryptedtext", "my-secret");
console.log("Decrypted Text:", decrypted);
Parameters:
Name Type Description
encryptedPackage string The "iv:authtag:encryptedtext" string.
secret string The secret key used for encryption.
Throws:
If the decryption fails due to an invalid format or other issues.
Type
Error
Returns:
The original plaintext or null if decryption fails.
Type
string | null

(static) encrypt(textToEncrypt, secret) → {string}

Description:
  • Encrypts text using AES-256-GCM.
Example
const encrypted = crypto.encrypt("Hello, World!", "my-secret");
console.log("Encrypted Text:", encrypted);
Parameters:
Name Type Description
textToEncrypt string The plaintext string.
secret string The secret key to use for encryption.
Returns:
A combined string "iv:authtag:encryptedtext" in hex format.
Type
string

(static) generateSecureRandomString(length) → {string}

Description:
  • Generates a cryptographically secure random string.
Example
const randomString = crypto.generateSecureRandomString(32);
console.log("Random String:", randomString);
Parameters:
Name Type Description
length number The desired length of the final string.
Returns:
A random, URL-safe string.
Type
string

(static) hashPassword(plainTextPassword) → {Promise.<string>}

Description:
  • Securely hashes a password using Argon2.
Example
const hash = await crypto.hashPassword("mySecurePassword");
console.log("Hashed Password:", hash);
Parameters:
Name Type Description
plainTextPassword string The user's password.
Returns:
A promise that resolves to the full hash string.
Type
Promise.<string>

(static) hmacSha256Encrypt(inputString, secret) → {string}

Description:
  • Encrypts (signs) a string using HMAC-SHA256.
Example
const signature = crypto.hmacSha256Encrypt("Hello, World!", "my-secret");
console.log("HMAC-SHA256 Signature:", signature);
Parameters:
Name Type Description
inputString string The string to encrypt/sign.
secret string The secret key.
Returns:
The HMAC signature as a hex string.
Type
string

(static) hmacSha256Verify(encryptedString, originalString, secret) → {boolean}

Description:
  • Verifies an HMAC-SHA256 signature.
Example
const isValid = crypto.hmacSha256Verify(signature, "Hello, World!", "my-secret");
console.log("Is the signature valid? - ", isValid);
Parameters:
Name Type Description
encryptedString string The signature (hex string) to verify.
originalString string The original, unencrypted string.
secret string The secret key used for signing.
Returns:
True if the signature is valid, false otherwise.
Type
boolean

(static) verifyPassword(plainTextPassword, hash) → {Promise.<boolean>}

Description:
  • Verifies a plaintext password against an Argon2 hash.
Example
const isValid = await crypto.verifyPassword("mySecurePassword", hash);
console.log("Is the password valid? - ", isValid);
Parameters:
Name Type Description
plainTextPassword string The password to check.
hash string The hash string from the database.
Returns:
A promise that resolves to true if they match, false otherwise.
Type
Promise.<boolean>