/
transform.go
189 lines (147 loc) · 3.72 KB
/
transform.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
package icepacker
import (
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"crypto/sha1"
"golang.org/x/crypto/pbkdf2"
)
// ChiperSettings records the settings of encryption/decryption
type CipherSettings struct {
Key string
Salt string
Iteration int
}
// NewCipherSettings created a new CipherSettings instance with default values
func NewCipherSettings(key string) CipherSettings {
return CipherSettings{
Key: key,
Salt: "icepacker",
Iteration: 10000,
}
}
// HashingKey is hashing the key with CipherSettings values
func HashingKey(settings CipherSettings) []byte {
if settings.Iteration > 0 {
k := pbkdf2.Key([]byte(settings.Key), []byte(settings.Salt), settings.Iteration, 16, sha1.New)
return k
}
return []byte(settings.Key)
}
// decrypt is decrypting the content with the key
func decrypt(ciphertext []byte, key []byte) []byte {
// Create the AES cipher
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// Before even testing the decryption,
// if the text is too small, then it is incorrect
if len(ciphertext) < aes.BlockSize {
panic(fmt.Errorf("Text is too short: %d", len(ciphertext)))
}
// Get the 16 byte IV
iv := ciphertext[:aes.BlockSize]
// Remove the IV from the ciphertext
ciphertext = ciphertext[aes.BlockSize:]
// Return a decrypted stream
stream := cipher.NewCFBDecrypter(block, iv)
// Decrypt bytes from ciphertext
stream.XORKeyStream(ciphertext, ciphertext)
return ciphertext
}
// encrypt is encrypting the content with the key
func encrypt(plaintext []byte, key []byte) []byte {
// Create the AES cipher
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
// Empty array of 16 + plaintext length
// Include the IV at the beginning
ciphertext := make([]byte, aes.BlockSize+len(plaintext))
// Slice of first 16 bytes
iv := ciphertext[:aes.BlockSize]
// Write 16 rand bytes to fill iv
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
panic(err)
}
// Return an encrypted stream
stream := cipher.NewCFBEncrypter(block, iv)
// Encrypt bytes from plaintext to ciphertext
stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
return ciphertext
}
// compress is compress the data with GZIP
func compress(data []byte) []byte {
var b bytes.Buffer
gz, err := gzip.NewWriterLevel(&b, gzip.BestCompression)
if err != nil {
panic(err)
}
if _, err := gz.Write(data); err != nil {
panic(err)
}
if err := gz.Flush(); err != nil {
panic(err)
}
if err := gz.Close(); err != nil {
panic(err)
}
return b.Bytes()
}
// decompress is decompress the data with GUNZIP
func decompress(data []byte) []byte {
b := bytes.NewReader(data)
gz, err := gzip.NewReader(b)
if err != nil {
panic(err)
}
defer gz.Close()
content, err := ioutil.ReadAll(gz)
if err != nil {
panic(err)
}
return []byte(content)
}
// TransformPack is transform the content of file to the package (encrypt, compress)
func TransformPack(data []byte, compression byte, encryption byte, key []byte) ([]byte, error) {
var res []byte
if len(data) == 0 {
return data, nil
}
// Compression
if compression == COMPRESS_GZIP {
res = compress(data)
} else {
res = data
}
// Encryption
if encryption == ENCRYPT_AES {
res = encrypt(res, key)
}
return res, nil
}
// TransformUnpack is transform back the transformed file content to the real file
func TransformUnpack(data []byte, compression byte, encryption byte, key []byte) ([]byte, error) {
var res []byte
if len(data) == 0 {
return data, nil
}
// Encryption
if encryption == ENCRYPT_AES {
res = decrypt(data, key)
} else {
res = data
}
// Compression
if compression == COMPRESS_GZIP {
res = decompress(res)
}
return res, nil
}