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 55
/
Encryptor.scala
54 lines (36 loc) · 1.81 KB
/
Encryptor.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
47
48
49
50
51
52
53
54
package tsec.cipher.symmetric
import cats.Monad
trait Encryptor[F[_], A, K[_]] {
final def encrypt(plainText: PlainText, key: K[A])(
implicit ivStrategy: IvGen[F, A],
F: Monad[F]
): F[CipherText[A]] =
F.flatMap(ivStrategy.genIv)(encrypt(plainText, key, _))
def encrypt(plainText: PlainText, key: K[A], iv: Iv[A]): F[CipherText[A]]
def decrypt(cipherText: CipherText[A], key: K[A]): F[PlainText]
}
trait AuthEncryptor[F[_], A, K[_]] extends Encryptor[F, A, K] {
def encryptDetached(plainText: PlainText, key: K[A])(
implicit ivStrategy: IvGen[F, A],
F: Monad[F]
): F[(CipherText[A], AuthTag[A])] =
F.flatMap(ivStrategy.genIv)(encryptDetached(plainText, key, _))
def encryptDetached(plainText: PlainText, key: K[A], iv: Iv[A]): F[(CipherText[A], AuthTag[A])]
def decryptDetached(cipherText: CipherText[A], key: K[A], authTag: AuthTag[A]): F[PlainText]
}
trait AADEncryptor[F[_], A, K[_]] extends AuthEncryptor[F, A, K] {
def encryptWithAAD(plainText: PlainText, key: K[A], aad: AAD)(
implicit ivStrategy: IvGen[F, A],
F: Monad[F]
): F[CipherText[A]] =
F.flatMap(ivStrategy.genIv)(encryptWithAAD(plainText, key, _, aad))
def encryptWithAAD(plainText: PlainText, key: K[A], iv: Iv[A], aad: AAD): F[CipherText[A]]
def encryptWithAADDetached(plainText: PlainText, key: K[A], aad: AAD)(
implicit ivStrategy: IvGen[F, A],
F: Monad[F]
): F[(CipherText[A], AuthTag[A])] =
F.flatMap(ivStrategy.genIv)(encryptWithAADDetached(plainText, key, _, aad))
def encryptWithAADDetached(plainText: PlainText, key: K[A], iv: Iv[A], aad: AAD): F[(CipherText[A], AuthTag[A])]
def decryptWithAAD(cipherText: CipherText[A], key: K[A], aad: AAD): F[PlainText]
def decryptWithAADDetached(cipherText: CipherText[A], key: K[A], aad: AAD, authTag: AuthTag[A]): F[PlainText]
}