Skip to content

Commit

Permalink
Change Hashing.create to return Resource, change implicit derivation …
Browse files Browse the repository at this point in the history
…of Hashing[F]
  • Loading branch information
mpilquist committed Jul 4, 2024
1 parent 37968c1 commit 019aeda
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
package fs2
package hashing

import cats.effect.Sync
import cats.effect.{Resource, Sync}

trait HashCompanionPlatform {

def apply[F[_]: Sync](algorithm: String): Resource[F, Hash[F]] =
Resource.eval(Sync[F].delay(unsafe(algorithm)))

def unsafe[F[_]: Sync](algorithm: String): Hash[F] =
???
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@
package fs2
package hashing

import cats.effect.Sync
import cats.effect.{Resource, Sync}

import java.security.MessageDigest

private[hashing] trait HashCompanionPlatform {

def apply[F[_]: Sync](algorithm: String): Resource[F, Hash[F]] =
Resource.eval(Sync[F].delay(unsafe(algorithm)))

def unsafe[F[_]: Sync](algorithm: String): Hash[F] =
unsafeFromMessageDigest(MessageDigest.getInstance(algorithm))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
package fs2
package hashing

import cats.effect.Sync
import cats.effect.{Resource, Sync}

trait HashCompanionPlatform {

def apply[F[_]: Sync](algorithm: String): Resource[F, Hash[F]] =
Resource.eval(Sync[F].delay(unsafe(algorithm)))

def unsafe[F[_]: Sync](algorithm: String): Hash[F] =
???
}
7 changes: 1 addition & 6 deletions core/shared/src/main/scala/fs2/hashing/Hash.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
package fs2
package hashing

import cats.effect.Sync

/** Mutable data structure that incrementally computes a hash from chunks of bytes.
*
* To compute a hash, call `addChunk` one or more times and then call `computeAndReset`.
Expand Down Expand Up @@ -84,7 +82,4 @@ trait Hash[F[_]] {
)
}

object Hash extends HashCompanionPlatform {
def apply[F[_]: Sync](algorithm: String): F[Hash[F]] =
Sync[F].delay(unsafe(algorithm))
}
object Hash extends HashCompanionPlatform
31 changes: 19 additions & 12 deletions core/shared/src/main/scala/fs2/hashing/Hashing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
package fs2
package hashing

import cats.effect.Sync
import cats.effect.{IO, LiftIO, MonadCancel, Resource, Sync}

/** Capability trait that provides hashing.
*
Expand All @@ -34,38 +34,45 @@ import cats.effect.Sync
trait Hashing[F[_]] {

/** Creates a new hash using the specified hashing algorithm. */
def create(algorithm: String): F[Hash[F]]
def create(algorithm: String): Resource[F, Hash[F]]

/** Creates a new MD-5 hash. */
def md5: F[Hash[F]] = create("MD-5")
def md5: Resource[F, Hash[F]] = create("MD-5")

/** Creates a new SHA-1 hash. */
def sha1: F[Hash[F]] = create("SHA-1")
def sha1: Resource[F, Hash[F]] = create("SHA-1")

/** Creates a new SHA-256 hash. */
def sha256: F[Hash[F]] = create("SHA-256")
def sha256: Resource[F, Hash[F]] = create("SHA-256")

/** Creates a new SHA-384 hash. */
def sha384: F[Hash[F]] = create("SHA-384")
def sha384: Resource[F, Hash[F]] = create("SHA-384")

/** Creates a new SHA-512 hash. */
def sha512: F[Hash[F]] = create("SHA-512")
def sha512: Resource[F, Hash[F]] = create("SHA-512")

/** Returns a pipe that hashes the source byte stream and outputs the hash.
*
* For more sophisticated use cases, such as writing the contents of a stream
* to a file while simultaneously computing a hash, use `create` or `sha256` or
* similar to create a `Hash[F]`.
*/
def hashWith(hash: F[Hash[F]]): Pipe[F, Byte, Byte] =
source => Stream.eval(hash).flatMap(h => h.hash(source))
def hashWith(hash: Resource[F, Hash[F]])(implicit F: MonadCancel[F, ?]): Pipe[F, Byte, Byte] =
source => Stream.resource(hash).flatMap(h => h.hash(source))
}

object Hashing {
implicit def apply[F[_]](implicit F: Hashing[F]): F.type = F
def apply[F[_]](implicit F: Hashing[F]): F.type = F

implicit def forSync[F[_]: Sync]: Hashing[F] = new Hashing[F] {
def create(algorithm: String): F[Hash[F]] =
def forSync[F[_]: Sync]: Hashing[F] = new Hashing[F] {
def create(algorithm: String): Resource[F, Hash[F]] =
Hash[F](algorithm)
}

def forIO: Hashing[IO] = forLiftIO

implicit def forLiftIO[F[_]: Sync: LiftIO]: Hashing[F] = {
val _ = LiftIO[F]
forSync
}
}

0 comments on commit 019aeda

Please sign in to comment.