This repository has been archived by the owner on Nov 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathMacExamples.scala
46 lines (37 loc) · 1.58 KB
/
MacExamples.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
object MacExamples {
/** Example message authentication: Note, will use byteutils */
import cats.effect.IO
import tsec.common._
import tsec.mac._
import tsec.mac.jca._
//Necessary in 2.11
import cats.syntax.either._
type ET[PhoneHome] = Either[Throwable, PhoneHome]
val toMac: Array[Byte] = "hi!".utf8Bytes
val `mac'd`: Either[Throwable, Boolean] = for {
key <- HMACSHA256.generateKey[MacErrorM] //Generate our key.
macValue <- HMACSHA256.sign[ET](toMac, key) //Generate our MAC bytes
verified <- HMACSHA256.verifyBool[ET](toMac, macValue, key) //Verify a byte array with a signed, typed instance
} yield verified
import cats.effect.Sync
import cats.syntax.all._
/** For Interpretation into any F */
def `mac'd-pure`[F[_]: Sync]: F[Boolean] =
for {
key <- HMACSHA256.generateKey[F] //Generate our key.
macValue <- HMACSHA256.sign[F](toMac, key) //Generate our MAC bytes
verified <- HMACSHA256.verifyBool[F](toMac, macValue, key) //Verify a byte array with a signed, typed instance
} yield verified
/** Using the typeclass [[MessageAuth]],
* JCASigner is simply the class over java secret keys
*
*/
def usingTypeclass[F[_]: Sync, A](mToSign: Array[Byte], key: MacSigningKey[A])(
implicit messageAuth: JCAMessageAuth[F, A]
): F[Boolean] =
for {
signed <- messageAuth.sign(mToSign, key)
verified <- messageAuth.verifyBool(mToSign, signed, key)
} yield verified
HMACSHA512.generateKey[IO].flatMap(usingTypeclass[IO, HMACSHA512](toMac, _))
}