Skip to main content

Try it Live

Run SIWE examples in the interactive playground

Utilities

Helper functions for SIWE message operations.

generateNonce

Generate cryptographically secure random nonce.

Signature

Parameters

  • length - Nonce length in characters (default: 11, minimum: 8)

Returns

Random base62 alphanumeric string (0-9, a-z, A-Z)

Throws

  • Error - If length < 8 (EIP-4361 requirement)

Example

Character Set

Base62: 62 total characters
  • Digits: 0123456789 (10 chars)
  • Uppercase: ABCDEFGHIJKLMNOPQRSTUVWXYZ (26 chars)
  • Lowercase: abcdefghijklmnopqrstuvwxyz (26 chars)
Properties:
  • URL-safe (no special characters)
  • Case-sensitive
  • Human-readable
  • No ambiguous characters

Randomness

Browser Environment

Uses Web Crypto API:

Node.js Environment

Uses Node crypto module:
Security:
  • Cryptographically secure PRNG
  • Suitable for authentication tokens
  • Platform-specific random source

Generation Algorithm

Modulo bias is negligible for 256 values mapped to 62 characters

Common Patterns

Message Creation

Server-Side Nonce Management

Batch Generation

Custom Length for Different Security Levels

Entropy Analysis

Bits of entropy = log2(62^length) Recommendations:
  • 8 chars: Spec minimum, adequate for short-lived nonces with rate limiting
  • 11 chars: Default, good balance of security and length
  • 16+ chars: High-value authentication, long-lived sessions

Security Considerations

Nonce Requirements

  1. Uniqueness: Each nonce must be globally unique
  2. Randomness: Cryptographically secure random generation
  3. Single-Use: Consume after verification to prevent replay
  4. Expiration: Limit nonce lifetime (5-15 minutes recommended)
  5. Storage: Store server-side, verify on auth

Replay Attack Prevention

Rate Limiting

Performance

Time Complexity: O(n) where n = length Space Complexity: O(n) Typical Performance:
  • 11-char nonce: less than 0.1ms
  • 100 nonces: less than 1ms
  • Dominated by random number generation
Optimization:
  • Pre-generate nonce pool for high throughput
  • Batch generation more efficient than individual calls
  • No allocation overhead (returns string)

Best Practices

  1. Default Length: Use default 11 chars unless specific needs
  2. Server-Side Generation: Generate on backend, send to frontend
  3. Store Metadata: Track creation time, expiration, usage
  4. Single Use: Invalidate after successful authentication
  5. Expiration: Set reasonable TTL (5-15 minutes)
  6. Rate Limit: Prevent nonce generation abuse
  7. Uniqueness Check: Verify nonce not already in use (optional for high security)

See Also