@@ -21,7 +21,6 @@ import (
21
21
"crypto/aes"
22
22
"crypto/cipher"
23
23
"crypto/rand"
24
- "errors"
25
24
"io"
26
25
"os"
27
26
"path/filepath"
@@ -66,7 +65,7 @@ func newFileCache(cfg config.FileCacheConfig, expireDuration time.Duration, opti
66
65
67
66
// create cache directory
68
67
if err := os .MkdirAll (fc .cachePath , 0777 ); err != nil {
69
- return nil , err
68
+ return nil , cacheProcessingError ( "failed to create cache directory" , err )
70
69
}
71
70
72
71
// read or create key
@@ -77,7 +76,7 @@ func newFileCache(cfg config.FileCacheConfig, expireDuration time.Duration, opti
77
76
78
77
fc .cipherBlock , err = aes .NewCipher (key )
79
78
if err != nil {
80
- return nil , err
79
+ return nil , cacheProcessingError ( "failed to create cipher block" , err )
81
80
}
82
81
83
82
return fc , nil
@@ -145,7 +144,7 @@ func (f *fileCache) readOrCreateKey() ([]byte, error) {
145
144
146
145
// create key file
147
146
if _ , err := rand .Read (key ); err != nil {
148
- return nil , err
147
+ return nil , cacheProcessingError ( "failed to create key" , err )
149
148
}
150
149
if err := f .writeToFile (keyFileName , key , false ); err != nil {
151
150
return nil , err
@@ -178,15 +177,15 @@ func (f *fileCache) readOrCreateKey() ([]byte, error) {
178
177
179
178
// check key is not tampered
180
179
if err := bcrypt .CompareHashAndPassword (keyHash , key ); err != nil {
181
- return nil , err
180
+ return nil , cacheProcessingError ( "key is tampered" , err )
182
181
}
183
182
184
183
return key , nil
185
184
}
186
185
187
186
func (f * fileCache ) decryptCache (cipherText []byte ) ([]byte , error ) {
188
187
if len (cipherText ) < aes .BlockSize {
189
- return nil , errors . New ( "ciphertext too short" )
188
+ return nil , cacheProcessingError ( "cipher text is too short", nil )
190
189
}
191
190
192
191
iv := cipherText [:aes .BlockSize ]
@@ -202,7 +201,7 @@ func (f *fileCache) encryptCache(plainText []byte) ([]byte, error) {
202
201
cipherText := make ([]byte , aes .BlockSize + len (plainText ))
203
202
iv := cipherText [:aes .BlockSize ]
204
203
if _ , err := io .ReadFull (rand .Reader , iv ); err != nil {
205
- return nil , err
204
+ return nil , cacheProcessingError ( "failed to create initialization vector" , err )
206
205
}
207
206
208
207
stream := cipher .NewCFBEncrypter (f .cipherBlock , iv )
@@ -228,7 +227,7 @@ func (f fileCache) readFile(filename string, hidden bool) ([]byte, error) {
228
227
229
228
data , err := os .ReadFile (filepath .Join (f .cachePath , filename ))
230
229
if err != nil {
231
- return nil , err
230
+ return nil , cacheProcessingError ( "failed to read file" , err )
232
231
}
233
232
data = data [:len (data )- 1 ]
234
233
@@ -243,7 +242,7 @@ func (f fileCache) writeToFile(filename string, data []byte, hidden bool) error
243
242
244
243
file , err := os .Create (filepath .Join (f .cachePath , filename ))
245
244
if err != nil {
246
- return err
245
+ return cacheProcessingError ( "failed to create file" , err )
247
246
}
248
247
defer file .Close ()
249
248
0 commit comments