Created
April 11, 2010 18:01
-
-
Save ussy/362932 to your computer and use it in GitHub Desktop.
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
import java.security.MessageDigest | |
abstract class Digest(algorithm: String) { | |
val digest = MessageDigest.getInstance(algorithm) | |
def hexdigest(value: String) : String = hexdigest(value.getBytes()) | |
def hexdigest(value: Array[Byte]) : String = { | |
byte2hexstring(digest.digest()) | |
} | |
private def byte2hexstring(value: Array[Byte]) : String = { | |
var hex = "" | |
value.foreach { b => | |
hex += Integer.toHexString((b >> 4) & 0x0f) | |
hex += Integer.toHexString(b & 0x0f) | |
} | |
hex | |
} | |
} | |
object MD5 extends Digest("MD5") | |
object SHA1 extends Digest("SHA1") | |
object SHA256 extends Digest("SHA-256") | |
object SHA384 extends Digest("SHA-384") | |
object SHA512 extends Digest("SHA-512") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment