forked from hyperledger-archives/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
83 lines (65 loc) · 2.24 KB
/
crypto.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package ecies
import (
"errors"
"io"
)
var (
// ErrInvalidKeyParameter Invalid Key Parameter
ErrInvalidKeyParameter = errors.New("Invalid Key Parameter.")
// ErrInvalidKeyGeneratorParameter Invalid Key Generator Parameter
ErrInvalidKeyGeneratorParameter = errors.New("Invalid Key Generator Parameter.")
)
// Parameter is common interface for all the parameters
type Parameter interface {
GetRand() io.Reader
}
// CipherParameters is common interface to represent cipher parameters
type CipherParameters interface {
Parameter
}
// AsymmetricKeyParameter is common interface to represent asymmetric cipher parameters
type AsymmetricCipherParameter interface {
Parameter
IsPublic() bool
}
// PublicKey is common interface to represent public asymmetric cipher parameters
type PublicKey interface {
AsymmetricCipherParameter
}
// PrivateKey is common interface to represent private asymmetric cipher parameters
type PrivateKey interface {
AsymmetricCipherParameter
GetPublicKey() PublicKey
}
// KeyGeneratorParameter is common interface to represent key generation parameters
type KeyGeneratorParameter interface {
Parameter
}
// KeyGenerator defines a key generator
type KeyGenerator interface {
Init(params KeyGeneratorParameter) error
GenerateKey() (PrivateKey, error)
}
// KeyGenerator defines an asymmetric cipher
type AsymmetricCipher interface {
// Init initializes this cipher with the passed parameters
Init(params AsymmetricCipherParameter) error
// Process processes the byte array given in input
Process(msg []byte) ([]byte, error)
}
// KeySerializer defines a key serializer/deserializer
type KeySerializer interface {
// ToBytes converts a key to bytes
ToBytes(key interface{}) ([]byte, error)
// ToBytes converts bytes to a key
FromBytes([]byte) (interface{}, error)
}
// SPI is the ECIES Service Provider Interface
type SPI interface {
NewAsymmetricCipherFromPrivateKey(priv PrivateKey) (AsymmetricCipher, error)
NewAsymmetricCipherFromPublicKey(pub PublicKey) (AsymmetricCipher, error)
NewPrivateKey(rand io.Reader, params interface{}) (PrivateKey, error)
NewPublicKey(rand io.Reader, params interface{}) (PublicKey, error)
SerializePrivateKey(priv PrivateKey) ([]byte, error)
DeserializePrivateKey(bytes []byte) (PrivateKey, error)
}