Skip to content

Instantly share code, notes, and snippets.

@ElectricCoffee
Last active March 9, 2022 10:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ElectricCoffee/01281d745e33823aff72 to your computer and use it in GitHub Desktop.
Save ElectricCoffee/01281d745e33823aff72 to your computer and use it in GitHub Desktop.
A very simple checksum generator written in scala, the following checksum types have been tested: MD2 MD5 SHA SHA-256 SHA-512
package your.pkg.here
import java.security.MessageDigest
import java.nio.file.{Files, Paths}
object Generator {
implicit class Helper(val sc: StringContext) extends AnyVal {
def md5(): String = generate("MD5", sc.parts(0))
def sha(): String = generate("SHA", sc.parts(0))
def sha256(): String = generate("SHA-256", sc.parts(0))
}
// t is the type of checksum, i.e. MD5, or SHA-512 or whatever
// path is the path to the file you want to get the hash of
def generate(t: String, path: String): String = {
val arr = Files readAllBytes (Paths get path)
val checksum = MessageDigest.getInstance(t) digest arr
checksum.map("%02X" format _).mkString
}
}
@Cokemonkey11
Copy link

Thanks!

@stsatlantis
Copy link

Thank you

@1311543
Copy link

1311543 commented Dec 27, 2018

thank you

@beckgael
Copy link

beckgael commented Mar 9, 2022

Thank you for sharing it !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment