Hashing data
Exposed supports one-way hashing for sensitive data such as passwords through the exposed-crypt module.
Unlike encryption, hashing does not allow the original value to be recovered. Instead, you verify a plaintext value against the stored hash.
Add dependencies
To use hashing with Exposed, add the exposed-crypt module to your build script:
Optionally, to use scrypt and Argon2 hashing, add the Bouncy Castle library as a runtime dependency:
Basic usage
To create a hashed column, apply the .hashed() function to a character column:
The .hashed() function changes the Kotlin type of the column from String to Hashed.
Supported algorithms
The .hashed() function uses BCryptHasher by default. You can change the default hasher by choosing one of the supported Hasher implementations:
Hasher | Algorithm |
|---|---|
| |
| |
| |
|
To use another hashing algorithm or customize its parameters, create a Hasher and pass it to the .hashed() function:
Configure a hasher
Each hasher provides parameters for configuring the amount of work required to generate and verify a hash. For example, you can configure the strength used by BCryptHasher:
For Argon2Hasher, you can configure parameters such as memory usage, iterations, and parallelism:
Pbkdf2Hasher also lets you select the pseudorandom function:
Use a Spring Security password encoder
If your application already uses a Spring Security PasswordEncoder, wrap it with PasswordEncoderHasher to adapt it to the Hasher type:
PasswordEncoderHasher delegates hashing and verification to the supplied PasswordEncoder while exposing the standard Exposed Hasher API.
Hash and store a value
Use the configured Hasher to hash a plaintext value before storing it:
The .hash() function returns a Hashed value containing the encoded hash.
Verify a value
To verify a plaintext value, use the .matches() function on the stored Hashed value:
The .matches() function returns true if the plaintext value matches the stored hash.