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

Jonas picture Jonas · Jun 13, 2011 · Viewed 14.5k times · Source

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.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?

Answer

SirDarius picture SirDarius · Jun 13, 2011

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.