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.

108 lines
3.5 KiB

4 years ago
3 years ago
4 years ago
  1. package coscrypto_test
  2. import (
  3. "bytes"
  4. "crypto/aes"
  5. "crypto/cipher"
  6. "crypto/rand"
  7. "io/ioutil"
  8. math_rand "math/rand"
  9. coscrypto "git.ouxuan.net/tommy/cos-go-sdk-v5/crypto"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. type EmptyMasterCipher struct{}
  13. func (mc EmptyMasterCipher) Encrypt(b []byte) ([]byte, error) {
  14. return b, nil
  15. }
  16. func (mc EmptyMasterCipher) Decrypt(b []byte) ([]byte, error) {
  17. return b, nil
  18. }
  19. func (mc EmptyMasterCipher) GetWrapAlgorithm() string {
  20. return "Test/EmptyWrapAlgo"
  21. }
  22. func (mc EmptyMasterCipher) GetMatDesc() string {
  23. return "Empty Desc"
  24. }
  25. func (s *CosTestSuite) TestCryptoObjectService_EncryptAndDecrypt() {
  26. var masterCipher EmptyMasterCipher
  27. builder := coscrypto.CreateAesCtrBuilder(masterCipher)
  28. contentCipher, err := builder.ContentCipher()
  29. assert.Nil(s.T(), err, "CryptoObject.CreateAesCtrBuilder Failed")
  30. dataSize := math_rand.Int63n(1024 * 1024 * 32)
  31. originData := make([]byte, dataSize)
  32. rand.Read(originData)
  33. // 加密
  34. r1 := bytes.NewReader(originData)
  35. reader1, err := contentCipher.EncryptContent(r1)
  36. assert.Nil(s.T(), err, "CryptoObject.contentCipher.Encrypt Failed")
  37. encryptedData, err := ioutil.ReadAll(reader1)
  38. assert.Nil(s.T(), err, "CryptoObject.Read Failed")
  39. // 解密
  40. r2 := bytes.NewReader(encryptedData)
  41. reader2, err := contentCipher.DecryptContent(r2)
  42. decryptedData, err := ioutil.ReadAll(reader2)
  43. assert.Nil(s.T(), err, "CryptoObject.Read Failed")
  44. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  45. }
  46. func (s *CosTestSuite) TestCryptoObjectService_Encrypt() {
  47. var masterCipher EmptyMasterCipher
  48. builder := coscrypto.CreateAesCtrBuilder(masterCipher)
  49. contentCipher, err := builder.ContentCipher()
  50. assert.Nil(s.T(), err, "CryptoObject.CreateAesCtrBuilder Failed")
  51. dataSize := math_rand.Int63n(1024 * 1024 * 32)
  52. originData := make([]byte, dataSize)
  53. rand.Read(originData)
  54. // 加密
  55. r := bytes.NewReader(originData)
  56. reader, err := contentCipher.EncryptContent(r)
  57. assert.Nil(s.T(), err, "CryptoObject.contentCipher.Encrypt Failed")
  58. encryptedData, err := ioutil.ReadAll(reader)
  59. assert.Nil(s.T(), err, "CryptoObject.Read Failed")
  60. // 直接解密
  61. cd := contentCipher.GetCipherData()
  62. block, err := aes.NewCipher(cd.Key)
  63. assert.Nil(s.T(), err, "CryptoObject.NewCipher Failed")
  64. decrypter := cipher.NewCTR(block, cd.IV)
  65. decryptedData := make([]byte, len(originData))
  66. decrypter.XORKeyStream(decryptedData, encryptedData)
  67. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  68. }
  69. func (s *CosTestSuite) TestCryptoObjectService_Decrypt() {
  70. var masterCipher EmptyMasterCipher
  71. builder := coscrypto.CreateAesCtrBuilder(masterCipher)
  72. contentCipher, err := builder.ContentCipher()
  73. assert.Nil(s.T(), err, "CryptoObject.CreateAesCtrBuilder Failed")
  74. dataSize := math_rand.Int63n(1024 * 1024 * 32)
  75. originData := make([]byte, dataSize)
  76. rand.Read(originData)
  77. // 直接加密
  78. cd := contentCipher.GetCipherData()
  79. block, err := aes.NewCipher(cd.Key)
  80. assert.Nil(s.T(), err, "CryptoObject.NewCipher Failed")
  81. encrypter := cipher.NewCTR(block, cd.IV)
  82. encryptedData := make([]byte, len(originData))
  83. encrypter.XORKeyStream(encryptedData, originData)
  84. // 解密
  85. r := bytes.NewReader(encryptedData)
  86. reader, err := contentCipher.DecryptContent(r)
  87. assert.Nil(s.T(), err, "CryptoObject.contentCipher.Encrypt Failed")
  88. decryptedData, err := ioutil.ReadAll(reader)
  89. assert.Nil(s.T(), err, "CryptoObject.Read Failed")
  90. assert.Equal(s.T(), bytes.Compare(originData, decryptedData), 0, "decryptData != originData")
  91. }