{"record":{"id":"55f4760af5ec77de","repo":"jwtk/jjwt","slug":"the-keytype-key-s-size-is-size-bits-which-is-55f476","errorCode":null,"errorMessage":"The ${keyType} key's size is ${size} bits which is not secure enough for the ${id} algorithm. The JWT JWA Specification (RFC 7518, Section 3.2) states that keys used with ${id} MUST have a size >= ${minKeyBitLength} bits (the key size must be greater than or equal to the hash output size). Consider using the Jwts.SIG.${id}.key() builder to create a key guaranteed to be secure enough for ${id}.  See https://tools.ietf.org/html/rfc7518#section-3.2 for more information.","messagePattern":"The (.+?) key's size is (.+?) bits which is not secure enough for the (.+?) algorithm\\. The JWT JWA Specification \\(RFC 7518, Section 3\\.2\\) states that keys used with (.+?) MUST have a size >= (.+?) bits \\(the key size must be greater than or equal to the hash output size\\)\\. Consider using the Jwts\\.SIG\\.(.+?)\\.key\\(\\) builder to create a key guaranteed to be secure enough for (.+?)\\.  See https://tools\\.ietf\\.org/html/rfc7518#section-3\\.2 for more information\\.","errorType":"validation","errorClass":"WeakKeyException","httpStatus":null,"severity":"error","filePath":"impl/src/main/java/io/jsonwebtoken/impl/security/DefaultMacAlgorithm.java","lineNumber":197,"sourceCode":"        // so return early if we can't:\n        if (size < 0) return;\n\n        if (size < this.minKeyBitLength) {\n            String msg = \"The \" + keyType + \" key's size is \" + size + \" bits which \" +\n                    \"is not secure enough for the \" + id + \" algorithm.\";\n\n            if (isJwaStandard() && isJwaStandardJcaName(getJcaName())) { //JWA standard algorithm name - reference the spec:\n                msg += \" The JWT \" +\n                        \"JWA Specification (RFC 7518, Section 3.2) states that keys used with \" + id + \" MUST have a \" +\n                        \"size >= \" + minKeyBitLength + \" bits (the key size must be greater than or equal to the hash \" +\n                        \"output size). Consider using the Jwts.SIG.\" + id + \".key() \" +\n                        \"builder to create a key guaranteed to be secure enough for \" + id + \".  See \" +\n                        \"https://tools.ietf.org/html/rfc7518#section-3.2 for more information.\";\n            } else { //custom algorithm - just indicate required key length:\n                msg += \" The \" + id + \" algorithm requires keys to have a size >= \" + minKeyBitLength + \" bits.\";\n            }\n\n            throw new WeakKeyException(msg);\n        }\n    }\n\n    @Override\n    public byte[] doDigest(final SecureRequest<InputStream, SecretKey> request) {\n        return jca(request).withMac(new CheckedFunction<Mac, byte[]>() {\n            @Override\n            public byte[] apply(Mac mac) throws Exception {\n                mac.init(request.getKey());\n                InputStream payload = request.getPayload();\n                byte[] buf = new byte[1024];\n                int len = 0;\n                while (len != -1) {\n                    len = payload.read(buf);\n                    if (len > 0) mac.update(buf, 0, len);\n                }\n                return mac.doFinal();\n            }","sourceCodeStart":179,"sourceCodeEnd":215,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/impl/src/main/java/io/jsonwebtoken/impl/security/DefaultMacAlgorithm.java#L179-L215","documentation":"WeakKeyException from DefaultMacAlgorithm.validateKey when an HMAC key is shorter than the hash output size required by RFC 7518 Section 3.2 (e.g. HS256 needs >= 256-bit keys). For standard algorithms the message references the JWA spec and suggests the Jwts.SIG key builders; custom algorithms get a simpler message.","triggerScenarios":"signWith/verifyWith HS256 with a key under 32 bytes (256 bits); a short string secret converted directly via getBytes(); keys generated with KeyGenerator without setting key size.","commonSituations":"Hard-coded short secrets like 'secret' or 'mykey' in demos/config; legacy systems built before jjwt 0.10 (which relaxed then re-enforced RFC minimums); keys migrated between environments and truncated.","solutions":["Generate a compliant key: Jwts.SIG.HS256.key().build() (or Keys.secretKeyFor(MacAlgorithm)).","Use Keys.hmacShaKeyFor(bytes) which enforces/normalizes size; give it >= 32 bytes for HS256.","Store a longer base64 secret in config and decode all of it; never truncate.","If the key genuinely must stay small, downgrade the algorithm (HS256->smaller hash is not allowed; use a custom algorithm acknowledging the risk) - preferred is to upgrade the key."],"exampleFix":"// before\nSecretKey key = Keys.hmacShaKeyFor(\"secret\".getBytes()); // 48 bits - too weak\nJwts.builder().signWith(key, Jwts.SIG.HS256);\n// after\nSecretKey key = Jwts.SIG.HS256.key().build(); // 256-bit key\nJwts.builder().signWith(key, Jwts.SIG.HS256);","handlingStrategy":"validation","validationCode":"static SecretKey strongHmacKey(byte[] secret, int minBits) {\n    if (secret.length * 8 < minBits)\n        throw new IllegalArgumentException(\"Secret too short: need >= \" + minBits + \" bits for this MAC algorithm\");\n    return Keys.hmacShaKeyFor(secret);\n}\n// strongHmacKey(secret, 256) before HS256","typeGuard":null,"tryCatchPattern":"try {\n    return Jwts.builder().signWith(key, Jwts.SIG.HS256).compact();\n} catch (WeakKeyException e) {\n    logger.error(\"HMAC key below RFC 7518 3.2 minimum; generate a new key\");\n    throw e; // do NOT silently replace: tokens signed with a new key won't verify\n}","preventionTips":["Generate keys with Jwts.SIG.HS256.key().build(); never embed short string secrets","Ensure configured secrets decode to >= 32 bytes (HS256) / 48 (HS384) / 64 (HS512)","Test key length in CI with an assertion per environment config","If upgrading from old weak secrets, plan a key-rotation window where both keys verify"],"tags":["java","jjwt","weak-key","hmac","jwa"],"backgroundTag":"invalid-argument-value","analyzedSha":"fb71496164c71442d08adec4571d9616ed5e1b8d","analyzedAt":"2026-09-09T00:33:09.982Z","contentChangedAt":"2026-09-09T00:33:09.982Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}