com.dwolla.security.crypto.Armor.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of fs2-pgp-bcpg1.72.1_2.13 Show documentation
Show all versions of fs2-pgp-bcpg1.72.1_2.13 Show documentation
fs2 pipes for encrypting and decrypting streams with BouncyCastle PGP 1.72.1
The newest version!
package com.dwolla.security.crypto
import cats.effect.*
import cats.syntax.all.*
import fs2.*
import fs2.io.{readOutputStream, writeOutputStream}
import org.bouncycastle.bcpg.*
import java.io.*
import scala.annotation.nowarn
trait Armor[F[_]] {
def armor(chunkSize: ChunkSize): Pipe[F, Byte, Byte]
final def armor: Pipe[F, Byte, Byte] = armor(defaultChunkSize)
}
object Armor {
@nowarn("""msg=parameter (?:value )?ev in method apply is never used""")
def apply[F[_] : Async](implicit ev: BouncyCastleResource): Armor[F] = new Armor[F] {
private val closeStreamsAfterUse = false
override def armor(chunkSize: ChunkSize): Pipe[F, Byte, Byte] = bytes =>
readOutputStream(chunkSize.unrefined) { out =>
Stream.resource(Resource.fromAutoCloseable(Sync[F].blocking(new ArmoredOutputStream(out))))
.flatMap { armorer =>
bytes.through(writeOutputStream(armorer.pure[F].widen[OutputStream], closeStreamsAfterUse))
}
.compile
.drain
}
}
}