Last active
February 3, 2026 20:24
-
-
Save dacr/870665b262ce5f44ccb2c141bb702311 to your computer and use it in GitHub Desktop.
Various programmatic hashes. / published by https://github.com/dacr/code-examples-manager #b38b4236-7059-4f97-8c82-3172c4016f3a/c4b08c1547305b8f0d29ea97917cf58ba62ec4e8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : Various programmatic hashes. | |
| // keywords : scala, hashes, md5sum, sha1, murmurHash3, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : b38b4236-7059-4f97-8c82-3172c4016f3a | |
| // created-on : 2020-05-31T19:54:52Z | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala "3.4.2" | |
| //> using dep "org.scalatest::scalatest:3.2.10" | |
| // --------------------- | |
| import org.scalatest.* | |
| import flatspec.* | |
| import matchers.* | |
| import java.io.* | |
| import java.nio.file.Path | |
| object Hashes { | |
| def md5sum(that: String): String = | |
| md5sum(new ByteArrayInputStream(that.getBytes())) // TODO : Warning manage charsets... | |
| def md5sum(input: InputStream): String = { | |
| val bis = new BufferedInputStream(input) | |
| val buf = new Array[Byte](1024) | |
| val md5 = java.security.MessageDigest.getInstance("MD5") | |
| LazyList.continually(bis.read(buf)).takeWhile(_ != -1).foreach(md5.update(buf, 0, _)) | |
| md5.digest().map(0xFF & _).map { "%02x".format(_) }.foldLeft("") { _ + _ } | |
| } | |
| def murmurHash3(that:String):Int = | |
| scala.util.hashing.MurmurHash3.stringHash(that) | |
| def sha1(that: String): String = | |
| // Inspired from https://alvinalexander.com/source-code/scala-method-create-md5-hash-of-string | |
| import java.security.MessageDigest | |
| import java.math.BigInteger | |
| val md = MessageDigest.getInstance("SHA-1") | |
| val digest = md.digest(that.getBytes) | |
| val bigInt = new BigInteger(1, digest) | |
| val hashedString = bigInt.toString(16) | |
| hashedString | |
| def fileDigest(path: Path, algo:String="SHA-256"): String = | |
| import java.math.BigInteger | |
| import java.security.{MessageDigest, DigestInputStream} | |
| import java.io.FileInputStream | |
| val buffer = new Array[Byte](8192) | |
| val md5 = MessageDigest.getInstance(algo) | |
| val dis = new DigestInputStream(new FileInputStream(path.toFile), md5) | |
| try { while (dis.read(buffer) != -1) {} } | |
| finally { dis.close() } | |
| md5.digest.map("%02x".format(_)).mkString | |
| } | |
| import Hashes._ | |
| object HashesTest extends AnyFlatSpec with should.Matchers { | |
| override def suiteName: String = "HashesTest" | |
| val example = "Please hash me !" | |
| "murmurHash3" should "return the right hash" in { | |
| murmurHash3(example) shouldBe 370020140 | |
| } | |
| "md5sum" should "return the right hash" in { | |
| md5sum(example) shouldBe "55340948b4b044ad6d1632908c86e765" | |
| } | |
| "sha1" should "return the right hash" in { | |
| sha1(example) shouldBe "4031d74d6a72919da236a388bdf3b966126b80f2" | |
| } | |
| } | |
| HashesTest.execute() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment