{"record":{"id":"5b0e80ce3bf64b8a","repo":"lcobucci/jwt","slug":"key-provided-is-shorter-than-expectedlength-bits-only","errorCode":null,"errorMessage":"Key provided is shorter than {expectedLength} bits, only {actualLength} bits provided","messagePattern":"Key provided is shorter than (.+?) bits, only (.+?) bits provided","errorType":"exception","errorClass":"Lcobucci\\JWT\\Signer\\InvalidKeyProvided","httpStatus":null,"severity":"error","filePath":"src/Signer/Blake2b.php","lineNumber":26,"sourceCode":"use function hash_equals;\nuse function sodium_crypto_generichash;\nuse function strlen;\n\nfinal readonly class Blake2b implements Signer\n{\n    private const int MINIMUM_KEY_LENGTH_IN_BITS = 256;\n\n    public function algorithmId(): string\n    {\n        return 'BLAKE2B';\n    }\n\n    public function sign(string $payload, Key $key): string\n    {\n        $actualKeyLength = 8 * strlen($key->contents());\n\n        if ($actualKeyLength < self::MINIMUM_KEY_LENGTH_IN_BITS) {\n            throw InvalidKeyProvided::tooShort(self::MINIMUM_KEY_LENGTH_IN_BITS, $actualKeyLength);\n        }\n\n        return sodium_crypto_generichash($payload, $key->contents());\n    }\n\n    public function verify(string $expected, string $payload, Key $key): bool\n    {\n        return hash_equals($expected, $this->sign($payload, $key));\n    }\n}\n","sourceCodeStart":8,"sourceCodeEnd":37,"githubUrl":"https://github.com/lcobucci/jwt/blob/375813049c24c7111bda8b6884c57b071ceb2fe7/src/Signer/Blake2b.php#L8-L37","documentation":"Blake2b signer enforces a minimum key length (MINIMUM_KEY_LENGTH_IN_BITS) because sodium_crypto_generichash requires a key between 16 and 64 bytes. Before signing, it converts the key contents to bits and throws InvalidKeyProvided::tooShort if the key supplies fewer bits than the configured minimum. This guards against silently using cryptographically weak keys.","triggerScenarios":"Calling sign($payload, $key) (or verify(), which calls sign) with a Key whose contents() are shorter than the minimum bit length (e.g. an empty string, a 4-byte short secret, or a password of a few characters).","commonSituations":"Config files storing empty or placeholder secrets ('secret', 'changeme'); env vars trimmed or truncated; generating keys with random_bytes(8) instead of an adequate length; copying an HMAC-style key intended for a different algorithm.","solutions":["Generate a key of at least the minimum bit length: sodium_crypto_generichash_keygen() (32 bytes) or random_bytes(32), and pass its binary contents in the Key.","Measure the current key: 8 * strlen($key->contents()) and compare against Blake2b::MINIMUM_KEY_LENGTH_IN_BITS to see how short it is.","Fix the configuration/env var that supplies the key so it contains the full secret (check for trimming, quoting, or truncation issues).","For user-supplied secrets too short on their own, derive a proper-length key with sodium_crypto_generichash/hkdf before constructing the Key."],"exampleFix":"// before\n$key = new Key('short'); // 8 bits -> tooShort\n\n// after\n$key = new Key(sodium_crypto_generichash_keygen()); // 256 bits","handlingStrategy":"validation","validationCode":"$bits = 8 * strlen($keyMaterial);\nif ($bits < Blake2b::MINIMUM_KEY_LENGTH_IN_BITS) {\n    throw new InvalidArgumentException(sprintf('Blake2b key must be at least %d bits, got %d', Blake2b::MINIMUM_KEY_LENGTH_IN_BITS, $bits));\n}","typeGuard":"function isBlake2bKeySize(string $secret): bool\n{\n    return 8 * strlen($secret) >= 16 && 8 * strlen($secret) <= 64 * 8; // generichash allows 16..64 bytes\n}","tryCatchPattern":"try {\n    $signature = $signer->sign($payload, $key);\n} catch (InvalidKeyProvided $e) {\n    throw new ConfigurationException('Blake2b signing key is too short: ' . $e->getMessage(), previous: $e);\n}","preventionTips":["Always generate Blake2b keys with sodium_crypto_generichash_keygen() (32 bytes).","Never use short human passwords directly as keys; derive with HKDF/argon2 first if a passphrase must be used.","Add a startup/config check asserting strlen($secret) >= minimum bytes before constructing the Key.","Keep secrets in env/config without trimming or truncation; verify length at deploy time."],"tags":["blake2b","signing","key-length","crypto"],"backgroundTag":"invalid-key-length","analyzedSha":"375813049c24c7111bda8b6884c57b071ceb2fe7","analyzedAt":"2026-09-14T11:12:28.004Z","contentChangedAt":"2026-09-14T11:12:28.004Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}