如何在 Scala 和 Play 框架中对 SHA-1 哈希进行 base64 编码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6332152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:05:05  来源:igfitidea点击:

How to base64 encode a SHA-1 hash in Scala and Play Framework?

scalabase64playframeworksha1java

提问by Jonas

I would like to base64 encode a generated SHA-1 hash in Scala using Play Framework.

我想使用 Play 框架在 Scala 中对生成的 SHA-1 哈希进行 base64 编码。

This works for me in Scala:

这在 Scala 中对我有用:

val md = java.security.MessageDigest.getInstance("SHA-1");
println(new sun.misc.BASE64Encoder().encode(md.digest("Foo".getBytes)))

But in Play Framework I get an error using:

但是在 Play Framework 中,我使用以下方法出现错误:

 type encode is not a member of object sun.misc.BASE64Encoder

when using:

使用时:

val md = java.security.MessageDigest.getInstance("SHA-1")
val ha = new sun.misc.BASE64Encoder.encode(md.digest(params.get("Foo").getBytes))

How can I generate a SHA-1 hash and base64 encode it using Scala and Play Framework?

如何使用 Scala 和 Play Framework 生成 SHA-1 哈希值并对其进行 base64 编码?

回答by SirDarius

You seem to have forgotten a pair of parenthesis:

你好像忘记了一对括号:

val md = java.security.MessageDigest.getInstance("SHA-1")
val ha = new sun.misc.BASE64Encoder().encode(md.digest(params.get("Foo").getBytes))

That should work better.

那应该效果更好。

回答by Régis

Faster and cleaner with Play api :

使用 Play api 更快更干净:

var signature = play.api.libs.Codecs.sha1(md.digest("Foo".getBytes))

Play 2.7(possible also older versions)

播放 2.7(也可能是旧版本)

var signature = play.api.libs.Codecs.sha1("Foo")