How Keycloak stores passwords
Keycloak never stores a password itself. It stores a hash: the result of running the password through a slow, one-way function with a random salt. At sign-in it hashes what the user typed the same way and compares the results.
Each password is a credential of type password with two parts, both JSON written as strings. secretData holds the hash and the salt, in Base64. credentialData holds the algorithm and the number of iterations. Keeping the algorithm next to the hash lets Keycloak check old passwords even after the realm moves to another algorithm.
{
"type": "password",
"secretData": "{\"value\":\"3ZpF…==\",\"salt\":\"gKs2…==\",\"additionalParameters\":{}}",
"credentialData": "{\"hashIterations\":210000,\"algorithm\":\"pbkdf2-sha512\",\"additionalParameters\":{}}"
}Why you would build a hash yourself
Most of the time Keycloak makes hashes on its own, when users set or change passwords. You need to build one when users come from somewhere else and should keep their passwords, or when you prepare test users with known passwords for a staging realm, or when you want to check that a stored credential matches a password during a migration.
In each case the credential must be exactly what Keycloak expects. A wrong encoding, a hex hash where Base64 belongs, or an iteration count in the wrong field means the user cannot sign in, and the error at sign-in only says the credentials are invalid.
PBKDF2 in Keycloak
PBKDF2 runs an HMAC many thousands of times over the password and salt. Keycloak offers it with three hash functions, each with its own provider id:
| Provider id | Hash function | OWASP suggested iterations |
|---|---|---|
pbkdf2-sha512 | HMAC-SHA-512 | 210,000 |
pbkdf2-sha256 | HMAC-SHA-256 | 600,000 |
pbkdf2 | HMAC-SHA-1 | 1,300,000 |
The iteration numbers are the OWASP Password Storage Cheat Sheet’s suggestions, not Keycloak settings. The realm’s password policy decides what Keycloak uses for new passwords, with hashAlgorithm and hashIterations. Recent Keycloak versions also offer Argon2; this tool does not build or check Argon2 credentials.
What this tool does
Generate
Enter a password, pick the PBKDF2 variant and the iterations, and keep the random 16-byte salt or make a new one. The tool derives the key with the browser’s Web Crypto API, encodes the password as UTF-8, and writes the credential and a ready user object for a partial or realm import. The derived key is 64 bytes for pbkdf2-sha512 and pbkdf2, and 32 bytes for pbkdf2-sha256.
Verify
Paste a credential, a user with credentials, or a database row with secret_data and credential_data, then type a password. The tool reads the salt, iterations and algorithm from the credential, derives a key of the same length as the stored one, and says whether they match.
How we checked the output
The hashes this page makes were compared with Node.js crypto.pbkdf2Sync for all three variants, with plain and non-ASCII passwords, and matched. The same check passed on values read back from the page in a browser. Verifying a key of another length, made outside the tool, also passed, since the length comes from the stored value.
How to use it
- Choose Generate. A sample password is filled in; replace it.
- Pick the algorithm. Match the realm’s policy if it names one.
- Set the iterations. Higher is slower to guess, and also slower at every sign-in.
- Copy the credential, or the user object with a username of your choice.
- Import it with Realm settings, Action, Partial import, or inside a realm import file.
- Sign in as that user on a staging realm to confirm it works.
Where it helps
Moving users from another system
If the old system used PBKDF2, convert each stored hash and salt to Base64 and write them into credentials, with the right algorithm and iteration count. Users keep their passwords. Use Verify on a few known test accounts before the real import. If the old system used another scheme, such as bcrypt, Keycloak cannot check those hashes without a custom provider. Then import users without passwords and have them reset.
Test and demo realms
Seed users with known passwords without keeping the passwords in plain text in the import file. Pair it with the user import generator to build the rest of each user.
Debugging a failed sign-in after an import
Paste the credential from the export or the database and the password the user says they used. If Verify matches but Keycloak refuses the sign-in, the problem is elsewhere: a disabled user, a required action, a lockout or a federation provider in front of the local credential.
Converting hashes from another system
Old systems store PBKDF2 hashes in many shapes: hex strings, Base64 with or without padding, or one combined string with the algorithm, iterations, salt and hash separated by a character such as $ or :. Keycloak wants four separate things: the hash and the salt in standard Base64, the iteration count as a number, and the provider id.
printf %s "$HEX" | xxd -r -p | base64Check which hash function the old system used with PBKDF2. A hash made with HMAC-SHA-256 written as pbkdf2-sha512 will never match. If the old system added a pepper, a secret mixed in besides the salt, Keycloak cannot reproduce it, and those users need a reset.
Common mistakes
| Mistake | Symptom | Fix |
|---|---|---|
| Hash or salt in hex | Invalid credentials at sign-in | Convert both to standard Base64. |
secretData as an object in a partial import | Import error or unusable credential | Write it as a JSON string, as exports do. |
| Wrong algorithm id | Invalid credentials | Use pbkdf2-sha512, pbkdf2-sha256 or pbkdf2 exactly. |
| Iterations from the policy, not the old system | Invalid credentials | Use the count the hash was made with. |
| Password trimmed or re-encoded | Only some users fail | Hash the exact UTF-8 bytes the user types. |
Changing algorithms later
Keycloak checks each password with the algorithm and iterations stored in its own credential, so changing the realm policy does not lock anyone out. New and changed passwords use the policy’s settings. Keycloak may also re-hash a password with the current policy at the next sign-in, which moves users over without a reset. Build the policy with the password policy builder.
Security notes
The password, the salt and the result stay in your browser; nothing is sent to us. Even so, use this page for test accounts and imports, not to process passwords people use elsewhere.
A hash is not harmless. Anyone with a hash and its salt can guess passwords offline, and weak passwords fall quickly whatever the iteration count. Treat credential exports and import files as secrets, and delete them after use. To share an export, remove credentials first with the realm export sanitizer.
Best practices
- Use a fresh random salt for every password. Never reuse one.
- Prefer
pbkdf2-sha512with an iteration count your servers can afford at peak sign-in load. - Test a handful of migrated users with Verify and a real sign-in before you import everyone.
- Mark generated test passwords temporary, so real users never keep them.
- Plan users together with the rest of the realm when you move servers; see how migration works on managed hosting.