Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/twbs/rorschach.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/com/getbootstrap/rorschach/util/HmacSha1.scala')
-rw-r--r--src/main/scala/com/getbootstrap/rorschach/util/HmacSha1.scala35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/main/scala/com/getbootstrap/rorschach/util/HmacSha1.scala b/src/main/scala/com/getbootstrap/rorschach/util/HmacSha1.scala
new file mode 100644
index 0000000..2856fea
--- /dev/null
+++ b/src/main/scala/com/getbootstrap/rorschach/util/HmacSha1.scala
@@ -0,0 +1,35 @@
+package com.chrisrebert.lmvtfy.util
+
+import javax.crypto.Mac
+import javax.crypto.spec.SecretKeySpec
+import java.security.{NoSuchAlgorithmException, InvalidKeyException, SignatureException}
+import java.security.MessageDigest
+
+object HmacSha1 {
+ private val HmacSha1Algorithm = "HmacSHA1"
+
+ implicit class HexByteArray(array: Array[Byte]) {
+ import javax.xml.bind.DatatypeConverter
+ def asHexBytes: String = DatatypeConverter.printHexBinary(array).toLowerCase
+ }
+}
+
+case class HmacSha1(mac: Array[Byte], secretKey: Array[Byte], data: Array[Byte]) {
+ import HmacSha1.HmacSha1Algorithm
+ import HmacSha1.HexByteArray
+
+ @throws[NoSuchAlgorithmException]("if HMAC-SHA1 is not supported")
+ @throws[InvalidKeyException]("if the secret key is malformed")
+ @throws[SignatureException]("under unknown circumstances")
+ private lazy val correct: Array[Byte] = {
+ val key = new SecretKeySpec(secretKey, HmacSha1Algorithm)
+ val mac = Mac.getInstance(HmacSha1Algorithm)
+ mac.init(key)
+ mac.doFinal(data)
+ }
+
+ lazy val isValid: Boolean = MessageDigest.isEqual(mac, correct)
+
+ def givenHex = mac.asHexBytes
+ def correctHex = correct.asHexBytes
+}