SHA256 of data stream
This can be accomplished with a MessageDigest
and Sink.fold
.
First we need a function to create an empty digest and a function to update a digest with a ByteBuffer
:
import java.security.MessageDigest
import java.nio.ByteBuffer
def emptySHA256Digest : MessageDigest = MessageDigest getInstance "SHA-256"
val updateDigest : (MessageDigest, ByteBuffer) => MessageDigest =
(messageDigest, byteBuffer) => {
messageDigest update byteBuffer
messageDigest
}
These two functions can then be used within a fold which is applied to the entity
of an HttpResponse
to update the digest with all ByteString
values in the entity:
import akka.http.scaladsl.model.HttpResponse
val responseBodyToDigest : HttpResponse => Future[MessageDigest] =
(_ : HttpResponse)
.entity
.dataBytes
.map(_.asByteBuffer)
.runFold(emptySHA256Digest)(updateDigest)
You would need Flow
which will transform one data to another data. In your case, you want to transform plain text to sha256 text.
def digest(algorithm: String = "SHA-256"): Flow[ByteString, ByteString, NotUsed] = {
Flow[ByteString].fold(MessageDigest.getInstance(algorithm)) {
case (digest: MessageDigest, bytes:ByteString) => {digest.update(bytes.asByteBuffer); digest}}
.map {case md: MessageDigest => ByteString(md.digest())}
}