forked from aliyun/aliyun-oss-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sample_crypto.go
304 lines (256 loc) · 11 KB
/
sample_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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
kms "github.com/aliyun/alibaba-cloud-sdk-go/services/kms"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
"github.com/aliyun/aliyun-oss-go-sdk/oss/crypto"
)
func SampleRsaNormalObject() {
// create oss client
client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create a description of the master key. Once created, it cannot be modified. The master key description and the master key are one-to-one correspondence.
// If all objects use the same master key, the master key description can also be empty, but subsequent replacement of the master key is not supported.
// Because if the description is empty, it is impossible to determine which master key is used when decrypting object.
// It is strongly recommended that: configure the master key description(json string) for each master key, and the client should save the correspondence between them.
// The server does not save their correspondence
// Map converted by the master key description information (json string)
materialDesc := make(map[string]string)
materialDesc["desc"] = "<your master encrypt key material describe information>"
// Create a master key object based on the master key description
masterRsaCipher, err := osscrypto.CreateMasterRsa(materialDesc, "<your rsa public key>", "<your rsa private key>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an interface for encryption based on the master key object, encrypt using aec ctr mode
contentProvider := osscrypto.CreateAesCtrCipher(masterRsaCipher)
// Get a storage space for client encryption, the bucket has to be created
// Client-side encrypted buckets have similar usages to ordinary buckets.
cryptoBucket, err := osscrypto.GetCryptoBucket(client, "<yourBucketName>", contentProvider)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// put object ,will be automatically encrypted
err = cryptoBucket.PutObject("<yourObjectName>", bytes.NewReader([]byte("yourObjectValueByteArrary")))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// get object ,will be automatically decrypted
body, err := cryptoBucket.GetObject("<yourObjectName>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
defer body.Close()
data, err := ioutil.ReadAll(body)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("data:", string(data))
}
func SampleRsaMultiPartObject() {
// create oss client
client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create a description of the master key. Once created, it cannot be modified. The master key description and the master key are one-to-one correspondence.
// If all objects use the same master key, the master key description can also be empty, but subsequent replacement of the master key is not supported.
// Because if the description is empty, it is impossible to determine which master key is used when decrypting object.
// It is strongly recommended that: configure the master key description(json string) for each master key, and the client should save the correspondence between them.
// The server does not save their correspondence
// Map converted by the master key description information (json string)
materialDesc := make(map[string]string)
materialDesc["desc"] = "<your master encrypt key material describe information>"
// Create a master key object based on the master key description
masterRsaCipher, err := osscrypto.CreateMasterRsa(materialDesc, "<your rsa public key>", "<your rsa private key>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an interface for encryption based on the master key object, encrypt using aec ctr mode
contentProvider := osscrypto.CreateAesCtrCipher(masterRsaCipher)
// Get a storage space for client encryption, the bucket has to be created
// Client-side encrypted buckets have similar usages to ordinary buckets.
cryptoBucket, err := osscrypto.GetCryptoBucket(client, "<yourBucketName>", contentProvider)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fileName := "<yourLocalFilePath>"
fileInfo, err := os.Stat(fileName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fileSize := fileInfo.Size()
// Encryption context information
var cryptoContext osscrypto.PartCryptoContext
cryptoContext.DataSize = fileSize
// The expected number of parts, the actual number of parts is subject to subsequent calculations.
expectPartCount := int64(10)
//Currently aes ctr encryption block size requires 16 byte alignment
cryptoContext.PartSize = (fileSize / expectPartCount / 16) * 16
imur, err := cryptoBucket.InitiateMultipartUpload("<yourObjectName>", &cryptoContext)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
chunks, err := oss.SplitFileByPartSize(fileName, cryptoContext.PartSize)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
var partsUpload []oss.UploadPart
for _, chunk := range chunks {
part, err := cryptoBucket.UploadPartFromFile(imur, fileName, chunk.Offset, chunk.Size, (int)(chunk.Number), cryptoContext)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
partsUpload = append(partsUpload, part)
}
// Complete
_, err = cryptoBucket.CompleteMultipartUpload(imur, partsUpload)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
// Query the master key according to the master key description information.
// If you need to decrypt different master key encryption objects, you need to provide this interface.
type MockRsaManager struct {
}
func (mg *MockRsaManager) GetMasterKey(matDesc map[string]string) ([]string, error) {
// to do
keyList := []string{"<yourRsaPublicKey>", "<yourRsaPrivatKey>"}
return keyList, nil
}
// Decrypt the object encrypted by different master keys
func SampleMultipleMasterRsa() {
// create oss client
client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create a description of the master key. Once created, it cannot be modified. The master key description and the master key are one-to-one correspondence.
// If all objects use the same master key, the master key description can also be empty, but subsequent replacement of the master key is not supported.
// Because if the description is empty, it is impossible to determine which master key is used when decrypting object.
// It is strongly recommended that: configure the master key description(json string) for each master key, and the client should save the correspondence between them.
// The server does not save their correspondence
// Map converted by the master key description information (json string)
materialDesc := make(map[string]string)
materialDesc["desc"] = "<your master encrypt key material describe information>"
// Create a master key object based on the master key description
masterRsaCipher, err := osscrypto.CreateMasterRsa(materialDesc, "<your rsa public key>", "<your rsa private key>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an interface for encryption based on the master key object, encrypt using aec ctr mode
contentProvider := osscrypto.CreateAesCtrCipher(masterRsaCipher)
// If you need to decrypt objects encrypted by different ma keys, you need to provide this interface.
var mockRsaManager MockRsaManager
var options []osscrypto.CryptoBucketOption
options = append(options, osscrypto.SetMasterCipherManager(&mockRsaManager))
// Get a storage space for client encryption, the bucket has to be created
// Client-side encrypted buckets have similar usages to ordinary buckets.
cryptoBucket, err := osscrypto.GetCryptoBucket(client, "<yourBucketName>", contentProvider, options...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// put object ,will be automatically encrypted
err = cryptoBucket.PutObject("<yourObjectName>", bytes.NewReader([]byte("yourObjectValueByteArrary")))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// get object ,will be automatically decrypted
body, err := cryptoBucket.GetObject("<otherObjectNameEncryptedWithOtherRsa>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
defer body.Close()
data, err := ioutil.ReadAll(body)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("data:", string(data))
}
func SampleKmsNormalObject() {
// create oss client
client, err := oss.New("<yourEndpoint>", "<yourAccessKeyId>", "<yourAccessKeySecret>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// create kms client
kmsClient, err := kms.NewClientWithAccessKey("<yourKmsRegion>", "<yourKmsAccessKeyId>", "<yourKmsAccessKeySecret>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create a description of the master key. Once created, it cannot be modified. The master key description and the master key are one-to-one correspondence.
// If all objects use the same master key, the master key description can also be empty, but subsequent replacement of the master key is not supported.
// Because if the description is empty, it is impossible to determine which master key is used when decrypting object.
// It is strongly recommended that: configure the master key description(json string) for each master key, and the client should save the correspondence between them.
// The server does not save their correspondence
// Map converted by the master key description information (json string)
materialDesc := make(map[string]string)
materialDesc["desc"] = "<your kms encrypt key material describe information>"
// Create a master key object based on the master key description
masterkmsCipher, err := osscrypto.CreateMasterAliKms(materialDesc, "<YourKmsId>", kmsClient)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an interface for encryption based on the master key object, encrypt using aec ctr mode
contentProvider := osscrypto.CreateAesCtrCipher(masterkmsCipher)
// Get a storage space for client encryption, the bucket has to be created
// Client-side encrypted buckets have similar usages to ordinary buckets.
cryptoBucket, err := osscrypto.GetCryptoBucket(client, "<yourBucketName>", contentProvider)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// put object ,will be automatically encrypted
err = cryptoBucket.PutObject("<yourObjectName>", bytes.NewReader([]byte("yourObjectValueByteArrary")))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// get object ,will be automatically decrypted
body, err := cryptoBucket.GetObject("<yourObjectName>")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
defer body.Close()
data, err := ioutil.ReadAll(body)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("data:", string(data))
}
func main() {
SampleRsaNormalObject()
SampleRsaMultiPartObject()
SampleMultipleMasterRsa()
SampleKmsNormalObject()
}