-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec.go
196 lines (164 loc) · 6.15 KB
/
codec.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package codec
import (
"fmt"
"math/rand"
"time"
checksum "github.com/multiverse-os/codec/checksum"
compression "github.com/multiverse-os/codec/compression"
encoding "github.com/multiverse-os/codec/encoding"
)
// MarshalUnmarshaler represents a codec used to marshal and unmarshal entities.
type MarshalUnmarshaler interface {
Marshal(v interface{}) ([]byte, error)
Unmarshal(b []byte, v interface{}) error
// name of this codec
Name() string
}
////////////////////////////////////////////////////////////////////////////////
type Codec struct {
encoding encoding.Codec
compression compression.Codec
checksum checksum.Hash
//keyring crypto.Keyring
}
// Initialization with raw/no compression, that can be defined
func Initialize() Codec {
rand.Seed(time.Now().UTC().UnixNano())
return Codec{
encoding: encoding.Format(encoding.Raw),
compression: compression.Algorithm(compression.None),
checksum: checksum.Algorithm(checksum.None),
//keyring: crypto.EmptyKeyring(),
}
}
func (self Codec) String() string {
return fmt.Sprintf("ecoding=%s,compression=%s,checksum=%s", self.encoding.String(), self.compression.String(), self.checksum.String())
//return fmt.Sprintf("ecoding=%s,compression=%s,checksum=%s,cryptography=%s", self.encoding.String(), self.compression.String(), self.checksum.String(), self.cryptography.Algorithm.String())
}
// Initialize Codec ////////////////////////////////////////////////////////////
func EncodingFormat(encodingType encoding.Type) Codec {
return Codec{
encoding: encoding.Format(encodingType),
compression: compression.Algorithm(compression.None),
checksum: checksum.Algorithm(checksum.None),
//keyring: crypto.EmptyKeyring(),
}
}
func CompressionAlgorithm(c compression.Type) Codec {
return Codec{
encoding: encoding.Format(encoding.Raw),
compression: compression.Algorithm(c),
checksum: checksum.Algorithm(checksum.None),
//keyring: crypto.EmptyKeyring(),
}
}
func ChecksumHash(c checksum.Type) Codec {
return Codec{
encoding: encoding.Format(encoding.Raw),
compression: compression.Algorithm(compression.None),
checksum: checksum.Algorithm(c),
//keyring: crypto.EmptyKeyring(),
}
}
//func CryptoKey(a crypto.Algorithm) Codec {
// return Codec{
// encoding: encoding.Format(encoding.Raw),
// compression: compression.Algorithm(compression.None),
// checksum: checksum.Algorithm(checksum.None),
// keyring: crypto.EmptyKeyring(),
// }
//}
// Chain-able codec defintion //////////////////////////////////////////////////
func (self Codec) EncodingFormat(encodingType encoding.Type) Codec {
self.encoding = encoding.Format(encodingType)
return self
}
func (self Codec) CompressionAlgorithm(c compression.Type) Codec {
self.compression = compression.Algorithm(c)
return self
}
func (self Codec) ChecksumAlgorithm(c checksum.Type) Codec {
self.checksum = checksum.Algorithm(c)
return self
}
//func (self Codec) CryptoKey(a crypto.Algorithm) Codec {
// self.cryptography = crypto.CryptoKey(a)
// return self
//}
// Encode/Decode Format ////////////////////////////////////////////////////////
//
// Encode(input interface{}) ([]byte, error)
func (self Codec) Encode(input interface{}) ([]byte, error) {
return self.encoding.Encode(input)
}
// Decode(data []byte, output interface{}) error
func (self Codec) Decode(input []byte, output interface{}) error {
return self.encoding.Decode(input, output)
}
// Compress / Uncompress ///////////////////////////////////////////////////////
func (self Codec) Compress(input []byte) ([]byte, error) {
return self.compression.Compress(input)
}
func (self Codec) Uncompress(input []byte) ([]byte, error) {
return self.compression.Uncompress(input)
}
// Cryptography Functions //////////////////////////////////////////////////////
//func (self Codec) Encrypt(input []byte) ([]byte, error) {
// return self.cryptography.Key.Encrypt(input)
//}
//
//func (self Codec) Decrypt(input []byte) ([]byte, error) {
// return self.cryptography.Key.Decrypt(input)
//}
//
//func (self Codec) Sign(input []byte) ([]byte, error) {
// return self.cryptography.Key.Sign(input)
//}
//
//func (self Codec) VerifySignature(input []byte) (bool, error) {
// return self.cryptography.Key.Verify(input)
//}
////////////////////////////////////////////////////////////////////////////////
// Checksum ////////////////////////////////////////////////////////////////////
func (self Codec) Checksum(input []byte) []byte {
return self.checksum.Encode(input)
}
// TODO Add a validation checking.
// MAYBE Add ability to take a large file, and break
// it up into chunks and essentially merkle checksum it.
// //////////////////////////////////////////////////////////////////////////////
// No Codec Access /////////////////////////////////////////////////////////////
func Encode(e encoding.Type, input interface{}) ([]byte, error) {
return encoding.Format(e).Encode(input)
}
func Decode(e encoding.Type, input []byte, value interface{}) error {
return encoding.Format(e).Decode(input, value)
}
// ----------------------------------------------------------------------------//
func Compress(c compression.Type, input []byte) ([]byte, error) {
return compression.Algorithm(c).Compress(input)
}
func Uncompress(c compression.Type, input []byte) ([]byte, error) {
return compression.Algorithm(c).Uncompress(input)
}
// ----------------------------------------------------------------------------//
func Checksum(c checksum.Type, input []byte) []byte {
return checksum.Algorithm(c).Encode(input)
}
//----------------------------------------------------------------------------//
//func Encrypt(a crypto.Algorithm, key, input []byte) ([]byte, error) {
// return crypto.CryptoKey(a).Encrypt(key, input)
//}
//
//func Decrypt(a crypto.Algorithm, key, input []byte) ([]byte, error) {
// return crypto.Cipher(a).Decrypt(key, input)
//}
//
//func Sign(a crypto.Algorithm, privateKey, input []byte) ([]byte, error) {
// return crypto.Cipher(a).Sign(privateKey, input)
//}
//
//func VerifySignature(a crypto.Algorithm, publicKey, input []byte) (bool, error) {
// return crypto.Cipher(a).VerifySignature(publicKey, input)
//}
////////////////////////////////////////////////////////////////////////////////