Skip to Content
This project is in active development. Features and behaviors may change without notice.
DocumentationUsageEncryption / Decryption

Encryption

The SAuthBase SDK provides functions for HMAC, AES-GCM encryption, and decryption.
These features can be used to securely handle data such as session IDs and account information.


HMAC Calculation

This can be used for tamper detection or verifying token generation.

src/index.ts
const = .("example"); .(.data); // Hexadecimal hash

Encrypting Data

You can encrypt data using AES-256-GCM.

src/index.ts
const = .("username|hmac"); .(.data); // Format: iv:encrypted:authTag
  • Returns encrypted data as a string if successful.
  • Use together with decryption for secure communication.

Decrypting Data

Decrypt encrypted strings and restore the original data.

src/index.ts
const = .(.data); .(.data); // Original username|hmac
  • Fails if the encrypted format is invalid.
  • Useful for verifying authentication token contents.

Example: Generate Encrypted Token from Username

Using generateEncryptedToken(), you can encrypt a username with its HMAC.

src/index.ts
const = await .("exampleUser"); .(.data); // Secure token that can be decrypted and verified

Example: Extract Session ID and Username from Token

src/index.ts
const = await .(.data); const = .(.data);
  • extractUserWithVerify() also performs HMAC validation.
  • extractUserUnsafe() extracts only the username (no verification).

Use calculateHmac() for tamper protection, encrypt()/decrypt() for confidentiality, and extractUserWithVerify()/extractUserUnsafe() for session integration.

You have now completed the basics of encryption.

Last updated on