Last active
May 25, 2024 10:20
-
-
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/1a322ce2a552a75bdc6807eb90dea1cc61e94e4e
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 NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// 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