Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7b39ccd
commit 776e882
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package play.api.libs | ||
|
||
import java.security._ | ||
import javax.crypto._ | ||
import javax.crypto.spec.SecretKeySpec | ||
|
||
import play.api.Play | ||
import play.api.PlayException | ||
import play.api.Configuration.Config | ||
|
||
object Crypto { | ||
def sign(message: String, key: Array[Byte]): String = { | ||
val mac = Mac.getInstance("HmacSHA1") | ||
mac.init(new SecretKeySpec(key, "HmacSHA1")) | ||
Codec.toHexString(mac.doFinal(message.getBytes("utf-8"))) | ||
} | ||
|
||
def sign(message: String): String = { | ||
Play.maybeApplication.get.configuration.get("application.secret") match { | ||
case Some(Config(key, value, file)) => sign(message, value.getBytes) | ||
case _ => throw PlayException("Configuration error", "Missing application.secret") | ||
} | ||
} | ||
} |