Back to Exposed

hashed

docs/api/exposed-crypt/org.jetbrains.exposed.v1.crypt/hashed.html

1.5.04.2 KB
Original Source

hashed

fun Column<String>.hashed(hasher: Hasher = BCryptHasher()): Column<Hashed>

Transforms this character column into one that stores one-way hashed values, using the provided hasher.

kotlin
val passwordHasher = BCryptHasher()object Users : IntIdTable() { val password = text("password").hashed(passwordHasher)}Users.insert { it[password] = passwordHasher.hash("s3cret") }val user = Users.selectAll().where { Users.id eq id }.single()val granted = user[Users.password].matches(submittedPassword)

Content copied to clipboard

Using the default argument for hasher requires calling hash() directly on the column, as that is the only way to reach the underlying default Hasher implementation:

kotlin
object Users : IntIdTable() { val password = text("password").hashed()}Users.insert { it[password] = Users.password.hash("s3cret") }

Content copied to clipboard

hash() works whichever way the column was declared, and is worth preferring even when a hasher is at hand: it always hashes with the one the column verifies with, which a separately held Hasher is not guaranteed to be.

A stored value can only be verified by the algorithm that produced it, so changing hasher on a column that already holds data stops the values written before the change from matching.

Return

A new column holding Hashed values.

Parameters

hasher

Hasher responsible for hashing values and for verifying them against stored hashes. Defaults to BCryptHasher, the provided algorithm that needs nothing beyond exposed-crypt itself.


@JvmName(name = "hashedNullable")

fun Column<String?>.hashed(hasher: Hasher = BCryptHasher()): Column<Hashed?>

Transforms this nullable character column into one that stores one-way hashed values, using the provided hasher, and leaving null values untouched.

kotlin
val recoveryCodeHasher = BCryptHasher()object Users : IntIdTable() { val recoveryCode = text("recovery_code").nullable().hashed(recoveryCodeHasher)}Users.insert { it[recoveryCode] = recoveryCodeHasher.hash("r3covery") }val user = Users.selectAll().where { Users.id eq id }.single()val granted = user[Users.recoveryCode]?.matches(submittedCode) == true

Content copied to clipboard

Using the default argument for hasher requires calling hash() directly on the column, as that is the only way to reach the underlying default Hasher implementation:

kotlin
object Users : IntIdTable() { val recoveryCode = text("recovery_code").nullable().hashed()}Users.insert { it[recoveryCode] = Users.recoveryCode.hash("r3covery") }

Content copied to clipboard

hash() works whichever way the column was declared, and is worth preferring even when a hasher is at hand: it always hashes with the one the column verifies with, which a separately held Hasher is not guaranteed to be.

A stored value can only be verified by the algorithm that produced it, so changing hasher on a column that already holds data stops the values written before the change from matching.

Return

A new nullable column holding Hashed values.

Parameters

hasher

Hasher responsible for hashing values and for verifying them against stored hashes. Defaults to BCryptHasher, the provided algorithm that needs nothing beyond exposed-crypt itself.

Generated by Dokka © 2026 Copyright