You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.1 KiB

4 years ago
3 years ago
4 years ago
  1. package coscrypto_test
  2. import (
  3. "bytes"
  4. "crypto/rand"
  5. "encoding/base64"
  6. "encoding/json"
  7. "os"
  8. "git.ouxuan.net/tommy/cos-go-sdk-v5"
  9. coscrypto "git.ouxuan.net/tommy/cos-go-sdk-v5/crypto"
  10. "github.com/stretchr/testify/assert"
  11. "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common"
  12. kms "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/kms/v20190118"
  13. )
  14. func (s *CosTestSuite) TestMasterKmsCipher_TestKmsClient() {
  15. kmsclient, _ := coscrypto.NewKMSClient(&cos.Credential{
  16. SecretID: os.Getenv("COS_SECRETID"),
  17. SecretKey: os.Getenv("COS_SECRETKEY"),
  18. }, kRegion)
  19. originData := make([]byte, 1024)
  20. _, err := rand.Read(originData)
  21. ctx := make(map[string]string)
  22. ctx["desc"] = string(originData[:10])
  23. bs, _ := json.Marshal(ctx)
  24. ctxJson := string(bs)
  25. enReq := kms.NewEncryptRequest()
  26. enReq.KeyId = common.StringPtr(os.Getenv("KMSID"))
  27. enReq.EncryptionContext = common.StringPtr(ctxJson)
  28. enReq.Plaintext = common.StringPtr(base64.StdEncoding.EncodeToString(originData))
  29. enResp, err := kmsclient.Encrypt(enReq)
  30. assert.Nil(s.T(), err, "Encrypt Failed")
  31. encryptedData := []byte(*enResp.Response.CiphertextBlob)
  32. deReq := kms.NewDecryptRequest()
  33. deReq.CiphertextBlob = common.StringPtr(string(encryptedData))
  34. deReq.EncryptionContext = common.StringPtr(ctxJson)
  35. deResp, err := kmsclient.Decrypt(deReq)
  36. assert.Nil(s.T(), err, "Decrypt Failed")
  37. decryptedData, err := base64.StdEncoding.DecodeString(*deResp.Response.Plaintext)
  38. assert.Nil(s.T(), err, "base64 Decode Failed")
  39. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "originData != decryptedData")
  40. }
  41. func (s *CosTestSuite) TestMasterKmsCipher_TestNormal() {
  42. kmsclient, _ := coscrypto.NewKMSClient(&cos.Credential{
  43. SecretID: os.Getenv("COS_SECRETID"),
  44. SecretKey: os.Getenv("COS_SECRETKEY"),
  45. }, kRegion)
  46. desc := make(map[string]string)
  47. desc["test"] = "TestMasterKmsCipher_TestNormal"
  48. master, err := coscrypto.CreateMasterKMS(kmsclient, os.Getenv("KMSID"), desc)
  49. assert.Nil(s.T(), err, "CreateMasterKMS Failed")
  50. originData := make([]byte, 1024)
  51. _, err = rand.Read(originData)
  52. encryptedData, err := master.Encrypt(originData)
  53. assert.Nil(s.T(), err, "Encrypt Failed")
  54. decryptedData, err := master.Decrypt(encryptedData)
  55. assert.Nil(s.T(), err, "Decrypt Failed")
  56. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "originData != decryptedData")
  57. }
  58. func (s *CosTestSuite) TestMasterKmsCipher_TestError() {
  59. kmsclient, _ := coscrypto.NewKMSClient(&cos.Credential{
  60. SecretID: os.Getenv("COS_SECRETID"),
  61. SecretKey: os.Getenv("COS_SECRETKEY"),
  62. }, kRegion)
  63. desc := make(map[string]string)
  64. desc["test"] = "TestMasterKmsCipher_TestNormal"
  65. master, err := coscrypto.CreateMasterKMS(kmsclient, "ErrorKMSID", desc)
  66. assert.Nil(s.T(), err, "CreateMasterKMS Failed")
  67. originData := make([]byte, 1024)
  68. _, err = rand.Read(originData)
  69. encryptedData, err := master.Encrypt(originData)
  70. assert.NotNil(s.T(), err, "Encrypt Failed")
  71. decryptedData, err := master.Decrypt(encryptedData)
  72. assert.NotNil(s.T(), err, "Decrypt Failed")
  73. assert.NotEqual(s.T(), bytes.Compare(originData, decryptedData), 0, "originData != decryptedData")
  74. }