-
Notifications
You must be signed in to change notification settings - Fork 0
/
security.go
60 lines (49 loc) · 1.14 KB
/
security.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
package gogossip
import (
"encoding/json"
"github.com/protocol-diver/go-gossip/crypto"
)
const (
NON_SECURE_TYPE = 0x00
AES256_CBC_TYPE = 0x01
)
type EncryptType byte
func (e EncryptType) String() string {
switch e {
case NON_SECURE_TYPE:
return "NO-SECURE"
case AES256_CBC_TYPE:
return "AES256-CBC"
}
return ""
}
type CipherMethod interface {
Encrypt(string, []byte) ([]byte, error)
Decrypt(string, []byte) ([]byte, error)
}
type Cipher struct {
CipherMethod
kind EncryptType
}
func (s *Cipher) Is(kind EncryptType) bool {
return s.kind == kind
}
func newCipher(kind EncryptType) Cipher {
switch kind {
case NON_SECURE_TYPE:
return Cipher{crypto.NON_SECURE{}, kind}
case AES256_CBC_TYPE:
return Cipher{crypto.AES256_CBC{}, kind}
}
panic("not supported encryption type")
}
func encryptPacket(encType EncryptType, passphrase string, p packet) ([]byte, error) {
b, err := json.Marshal(p)
if err != nil {
return nil, err
}
return newCipher(encType).Encrypt(passphrase, b)
}
func decryptPayload(payload []byte, encType EncryptType, passpharse string) ([]byte, error) {
return newCipher(encType).Decrypt(passpharse, payload)
}