123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- 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()
- }
|