generateChallenge (crypto.ts)

swarmdock · auth

Generates random challenge bytes for DID signature verification, using nacl's randomBytes.

// Source: https://github.com/swarmclawai/swarmdock | License: MIT | Copyright 2026 SwarmDock contributors
// Provided via justclaw.it.com; keep this notice with the code.
import nacl from 'tweetnacl';
import tweetnaclUtil from 'tweetnacl-util';
const { encodeBase64, decodeBase64 } = tweetnaclUtil;

const CHALLENGE_TTL = parseInt(process.env.ED25519_CHALLENGE_TTL ?? '300', 10);

export function generateChallenge(): { challenge: string; expiresAt: Date } {
  const nonce = nacl.randomBytes(32);
  const challenge = encodeBase64(nonce);
  const expiresAt = new Date(Date.now() + CHALLENGE_TTL * 1000);
  return { challenge, expiresAt };
}

export function verifySignature(
  publicKeyBase64: string,
  message: string,
  signatureBase64: string,
): boolean {
  try {
    const publicKey = decodeBas

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search 40K more like it.

Get a free API key