可能的重复:在 Java 中生成 MD5 哈希
我想在我的 Scala 代码中计算一个字符串的 MD5 哈希值.除了常规的 java.security.MessageDigest 方式之外,是否有任何 Scala 或 Java 库可以用来快速执行此操作?
I want to compute the MD5 hash of a string in my scala code. Is there any scala or java library i can use to do this quickly, apart from the regular java.security.MessageDigest way ?
请帮忙谢谢
推荐答案您可能在这里重新发明了一个非常小的轮子,但只需编写一个函数来做您想做的事:获取一个字符串,使用 MessageDigest,然后返回任何内容(十六进制字符串,字节数组)你需要.
You may be reinventing a very tiny wheel here, but just write a function to do what you want: take a string, use MessageDigest, and return whatever (hex string, byte array) you need.
import java.security.MessageDigest def md5(s: String) = { MessageDigest.getInstance("MD5").digest(s.getBytes) } md5("Hello")附言我不写 Scala,但这是有效的,它留给读者作为练习,将其转换为 Array[Byte] 以外的任何内容
P.S. I don't write Scala, but this works and it's left as an exercise to the reader to turn it into anything other than an Array[Byte]
更多推荐
在 Scala 中计算字符串的 MD5 哈希值
发布评论