在 Scala 中计算字符串的 MD5 哈希值

编程入门 行业动态 更新时间:2024-10-28 08:22:46
本文介绍了在 Scala 中计算字符串的 MD5 哈希值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

可能的重复:在 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 哈希值

本文发布于:2023-10-22 22:47:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1518953.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   Scala   哈希值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!