/
cipher.go
61 lines (51 loc) · 1.14 KB
/
cipher.go
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
55
56
57
58
59
60
61
package cipher
// Type describes the type of encryption of a Cipher
type Type string
type ciphersEnum struct {
Mock Type
Aes Type
}
// Has checks if cipherType is in Ciphers
func (c ciphersEnum) Has(cipherType Type) bool {
return cipherType == c.Mock ||
cipherType == c.Aes
}
// Ciphers enumerates the supported Cipher types
var Ciphers = &ciphersEnum{
Mock: "MOCK",
Aes: "AES",
}
// Cipher is a component to interact with the different Cipher services
type Cipher interface {
init() error
Type() Type
PrepareKey(key string) ([]byte, error)
Encrypt(data []byte, key []byte) ([]byte, error)
Decrypt(data []byte, key []byte) ([]byte, error)
}
// Store maintains Ciphers with it's dependencies
type Store struct {
mock Cipher
aes Cipher
}
// NewStore creates an instance of Store
func NewStore() (*Store, error) {
store := &Store{
mock: NewMockCipher(),
aes: NewAesCipher(),
}
err := store.aes.init()
if err != nil {
return nil, err
}
return store, nil
}
// Get retrieves the Cipher by it's type
func (s Store) Get(cipherType Type) Cipher {
switch cipherType {
case Ciphers.Aes:
return s.aes
default:
return s.mock
}
}