AesDecrypt AES decryption
(crypted, key []byte)
| 118 | |
| 119 | // AesDecrypt AES decryption |
| 120 | func AesDecrypt(crypted, key []byte) ([]byte, errors.Error) { |
| 121 | // Uniformly use sha256 to process as 32-bit Byte (256-bit bit) |
| 122 | sha256Key := sha256.Sum256(key) |
| 123 | key = sha256Key[:] |
| 124 | block, err := aes.NewCipher(key) |
| 125 | if err != nil { |
| 126 | return nil, errors.Convert(err) |
| 127 | } |
| 128 | // Get the block size and check whether the ciphertext length is legal |
| 129 | blockSize := block.BlockSize() |
| 130 | if len(crypted)%blockSize != 0 { |
| 131 | return nil, errors.Default.New(fmt.Sprintf("The length of the data to be decrypted is [%d], so cannot match the required block size [%d]", len(crypted), blockSize)) |
| 132 | } |
| 133 | |
| 134 | // Decrypt and unalign data |
| 135 | blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) |
| 136 | origData := make([]byte, len(crypted)) |
| 137 | blockMode.CryptBlocks(origData, crypted) |
| 138 | origData = PKCS7UnPadding(origData) |
| 139 | return origData, nil |
| 140 | } |
| 141 | |
| 142 | // RandomEncryptionSecret will return a random string of length 128 |
| 143 | func RandomEncryptionSecret() (string, errors.Error) { |
no test coverage detected