@@ -73,6 +73,9 @@ func encryptAES256(data []byte) ([]byte, []byte, error) {
73
73
}
74
74
plaintext := buf .Bytes ()
75
75
76
+ sum := sha256 .Sum256 (plaintext )
77
+ plaintext = append (sum [:], plaintext ... )
78
+
76
79
block , err := aes .NewCipher (key )
77
80
if err != nil {
78
81
return nil , nil , err
@@ -86,7 +89,6 @@ func encryptAES256(data []byte) ([]byte, []byte, error) {
86
89
87
90
mode := cipher .NewCBCEncrypter (block , iv )
88
91
mode .CryptBlocks (ciphertext [aes .BlockSize :], plaintext )
89
-
90
92
return key , ciphertext , nil
91
93
}
92
94
@@ -111,7 +113,13 @@ func decryptAES(key, ciphertext []byte) ([]byte, error) {
111
113
// works inplace when both args are the same
112
114
mode .CryptBlocks (ciphertext , ciphertext )
113
115
114
- buf := bytes .NewReader (ciphertext )
116
+ expectedSum := ciphertext [:32 ]
117
+ actualSum := sha256 .Sum256 (ciphertext [32 :])
118
+ if ! bytes .Equal (expectedSum , actualSum [:]) {
119
+ return nil , fmt .Errorf ("sha256 mismatch %v vs %v" , expectedSum , actualSum )
120
+ }
121
+
122
+ buf := bytes .NewReader (ciphertext [32 :])
115
123
var n uint64
116
124
if err = binary .Read (buf , binary .LittleEndian , & n ); err != nil {
117
125
return nil , err
0 commit comments