-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.go
More file actions
36 lines (29 loc) · 813 Bytes
/
file.go
File metadata and controls
36 lines (29 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package crypt
import (
"fmt"
"io/ioutil"
"os"
)
// EncryptFile encrypts srcFile using cipher enc into dstFile.
// DstFile must not exists.
func EncryptFile(srcFile, dstFile string, key []byte, enc Cipher) error {
return transform(srcFile, dstFile, key, enc)
}
// DecryptFile decrypts srcFile using cipher dec and key into dstFile.
func DecryptFile(srcFile, dstFile string, key []byte, dec Cipher) error {
return transform(srcFile, dstFile, key, dec)
}
func transform(srcFile, dstFile string, key []byte, op Cipher) error {
if _, err := os.Stat(dstFile); err == nil {
return fmt.Errorf("transform: destination exists")
}
src, err := ioutil.ReadFile(srcFile)
if err != nil {
return err
}
dst, err := op(src, key)
if err != nil {
return err
}
return ioutil.WriteFile(dstFile, dst, 0644)
}