开发者

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

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

This works for me in 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:

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

when using:

val md = java.securit开发者_高级运维y.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?


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.


Faster and cleaner with Play api :

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

Play 2.7 (possible also older versions)

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜