akka.stream.alpakka.s3.auth.package.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of akka-stream-alpakka-s3_2.11 Show documentation
Show all versions of akka-stream-alpakka-s3_2.11 Show documentation
Alpakka is a Reactive Enterprise Integration library for Java and Scala, based on Reactive Streams and Akka.
/*
* Copyright (C) 2016-2018 Lightbend Inc.
*/
package akka.stream.alpakka.s3
import java.security.MessageDigest
import javax.xml.bind.DatatypeConverter
import akka.stream.scaladsl.{Flow, Keep, Sink}
import akka.util.ByteString
import scala.concurrent.Future
package object auth {
def encodeHex(bytes: Array[Byte]): String = DatatypeConverter.printHexBinary(bytes).toLowerCase
def encodeHex(bytes: ByteString): String = encodeHex(bytes.toArray)
def digest(algorithm: String = "SHA-256"): Sink[ByteString, Future[ByteString]] =
Flow[ByteString]
.fold(MessageDigest.getInstance(algorithm)) {
case (digest, bytes) =>
digest.update(bytes.asByteBuffer)
digest
}
.map(d => ByteString(d.digest()))
.toMat(Sink.head[ByteString])(Keep.right)
}