{
  "id": 22361101,
  "title": "How to Generate a JWT Secret the Right Way (2026)",
  "url": "https://urgent.news/2026/11/28/how-to-generate-a-jwt-secret-the-right-way-2026",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-28T12:14:40.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/gary_yan_86eb77d35e0070f5/how-to-generate-a-jwt-secret-the-right-way-2026-3mcb"
  },
  "original_language": null,
  "account": "JSON Web Tokens (JWTs) rely on a secret key for security. The strength of a JWT depends on three factors: key size, encoding method, and storage location.\n\nFor HS256, HS384, and HS512 algorithms, the key must be at least 32, 48, and 64 random bytes respectively. These byte counts translate to 64, 96, and 128 hex characters, or 43, 64, and 86 Base64URL characters. Many projects mistakenly use a \"32-character secret,\" which only provides 16 bytes if the characters are ASCII only. Check your library to see if it decodes the hex string or uses the bytes as-is.\n\nEncoding impacts storage but not the token verification. Hex is universally compatible but doubles the size. Standard Base64 contains characters like + and / that require quoting, while Base64URL replaces those with - and _, making it ideal for environments and headers. Whatever method you choose, both signing and verifying services must handle it identically.\n\nThe generation aspect is straightforward, but storage is crucial. Storing the secret in version control is risky and requires frequent rotation. The simplest approach is to load an environment variable from a .env file stored in .gitignore, with an accompanying .env.example containing just the variable name. A production setup should use a secret manager like Vault or a platform-specific encrypted environment store, with access restricted to signing and verifying services. Implement key rotation by adding a key ID (kid) header, mapping each kid to its corresponding key, signing new tokens with the new key while keeping old tokens valid, and retiring the old key after the longest token validity period. Short-lived access tokens (5-15 minutes) simplify rotation, whereas long-lived tokens (weeks) make it challenging.\n\nIf a website generates the key for you, verify its safety by checking if the key is sent to a server. Use browser developer tools to inspect network requests and source code for any instances of the key. Avoid using secrets generated on the website itself.",
  "summary": "A JSON Web Token is only as strong as the secret that signs it. Most guides show a single command and move on, but three details decide whether the result is safe in production: how many bytes the key really has, how it is encoded when you store it, and where it lives. 1. The size rule is about bytes, not characters RFC 7518 section 3.2 requires an HMAC key at least as large as the hash output.…",
  "summary_source": "publisher",
  "key_points": [
    "JWT security depends on key size (32, 48, 64 bytes for HS256, HS384, HS512)",
    "Hex encoding doubles token size, Base64URL recommended for environments",
    "Store secret in environment variable, not version control or website generation"
  ],
  "editors_take": null,
  "opinion": false,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "machine_translated": false,
  "ai_generated": true,
  "disclaimer": "Accounts, briefs, key points, the editor’s take and translations are written by software from other outlets’ reporting and may contain errors. A summary whose summary_source is \"publisher\" is the outlet’s own feed text. Always check the linked original."
}