io.taig.taigless.hashing.BcryptHashing.scala Maven / Gradle / Ivy
package io.taig.taigless.hashing
import at.favre.lib.crypto.bcrypt.BCrypt
import cats.effect.kernel.Sync
import cats.syntax.all._
final class BcryptHashing[F[_]](hasher: BCrypt.Hasher, verifier: BCrypt.Verifyer, cost: Int)(implicit F: Sync[F])
extends Hashing[F, Array[Byte], Array[Byte]] {
override def hash(payload: Array[Byte]): F[Array[Byte]] = F.delay(hasher.hash(cost, payload))
override def verify(payload: Array[Byte], hash: Array[Byte]): F[Boolean] =
F.delay(verifier.verify(payload, hash)).map(_.verified)
}
object BcryptHashing {
def apply[F[_]: Sync](
hasher: BCrypt.Hasher,
verifier: BCrypt.Verifyer,
cost: Int
): Hashing[F, Array[Byte], Array[Byte]] =
new BcryptHashing[F](hasher, verifier, cost)
def default[F[_]](cost: Int = 12)(implicit F: Sync[F]): F[Hashing[F, Array[Byte], Array[Byte]]] = F.delay {
BcryptHashing(BCrypt.withDefaults(), BCrypt.verifyer(), cost)
}
}