A set of utilities for generating digests and handling binary codecs in Scala. Built on top of Apache Commons Codec.
Add the following to your build.sbt file. The latest version of the library should be in the Maven badge above.
Otherwise you can checkout the artefact listing on mvnrepository.com.
libraryDependencies +="au.id.tmm.digest4s"%%"digest4s-core"%"1.0.0"The simplest way to use the library is with the syntax import:
importau.id.tmm.digest4s.syntax._importau.id.tmm.digest4s.binarycodecs.syntax._importorg.apache.commons.codec.DecoderExceptionimportscala.collection.immutable.ArraySeq// Parse binary encoded Strings safelyvalbyteArray:Either[DecoderException, ArraySeq[Byte]] ="VXQ2DXQ=".parseBase32
valbyteArray:Either[DecoderException, ArraySeq[Byte]] ="reGh3g==".parseBase64
valbyteArray:Either[DecoderException, ArraySeq[Byte]] ="11011110101000011110000110101101".parseBinary
valbyteArray:Either[DecoderException, ArraySeq[Byte]] ="ADE1A1DE".parseHex
// Parse unsafely, throw if encoding is invalid:valbyteArray:ArraySeq[Byte] ="VXQ2DXQ=".parseBase32OrThrow
valbyteArray:ArraySeq[Byte] ="reGh3g==".parseBase64OrThrow
valbyteArray:ArraySeq[Byte] ="11011110101000011110000110101101".parseBinaryOrThrow
valbyteArray:ArraySeq[Byte] ="ADE1A1DE".parseHexOrThrow
// Use StringContext (throws if invalid)valbyteArray:ArraySeq[Byte] =base32"VXQ2DXQ="valbyteArray:ArraySeq[Byte] =base64"reGh3g=="valbyteArray:ArraySeq[Byte] =binary"11011110101000011110000110101101"valbyteArray:ArraySeq[Byte] =hex"ADE1A1DE"importau.id.tmm.digest4s.binarycodecs.syntax._importscala.collection.immutable.ArraySeq// Encode bytes to Stringvalbytes:Array[Byte] =Array[Byte](0xad.toByte, 0xe1.toByte, 0xa1.toByte, 0xde.toByte)
bytes.asBase32String // "VXQ2DXQ="
bytes.asBase64String // "reGh3g=="
bytes.asBinaryString // "11011110101000011110000110101101"
bytes.asHexString // "ADE1A1DE"// Can be used for different collection types
bytes.to(ArraySeq).asHexString // "ADE1A1DE"
bytes.toVector.asHexString // "ADE1A1DE"
bytes.toList.asHexString // "ADE1A1DE"There are a number of value classes in the au.id.tmm.digest4s.digest package which can improve the semantic clarity of
checksum fields:
importau.id.tmm.digest4s.digest._caseclassDocument(
checksum: Array[Byte], // 🤷 Bad, not clear what the algorithm is
)
caseclassDocument(
checksum: MD5Digest, // 😎 Clearer, describes the algorithm
)importjava.io.IOExceptionimportau.id.tmm.digest4s.digest._importau.id.tmm.digest4s.digest.syntax._// Compute digests for safe typesvalmd5Digest:MD5Digest="Hello".md5
valsha256Digest:SHA256Digest=Array[Byte](0xad.toByte, 0xe1.toByte, 0xa1.toByte, 0xde.toByte).sha256
valsha512Digest:SHA512Digest=List[Byte](0xad.toByte, 0xe1.toByte, 0xa1.toByte, 0xde.toByte).sha512
// Compute digests for types where you might hit an IOException:valmd5Digest:Either[IOException, MD5Digest] = java.nio.file.Paths.get("test").md5OrError