开发者

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

开发者 https://www.devze.com 2023-03-12 07:31 出处:网络
I would like to base64 encode a generated SHA-1 hash in Scala using Play Framework. This works for me in Scala:

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

精彩评论

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

关注公众号