-
Notifications
You must be signed in to change notification settings - Fork 1
/
encrypHandler.go
62 lines (50 loc) · 968 Bytes
/
encrypHandler.go
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
)
var key string
// operation 0 = enc 1 = dec
func CryptoHandler(path string, operation int) {
isDirectory := checkDirectory(path)
if isDirectory == true {
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}
for _, f := range files {
if isDirectory {
CryptoHandler(path+"/"+f.Name(), operation)
}
}
} else {
encryptAndWrite(path, operation)
}
}
func encryptAndWrite(path string, operation int) {
b, err := ioutil.ReadFile(path)
if err != nil {
fmt.Print(err)
}
if key == "" {
key = PasswordHandler(operation)
}
var text []byte = nil
if operation == 0 {
text = Encrypt(b, []byte(key))
}
if operation == 1 {
text = Decrypt(b, []byte(key))
}
ioutil.WriteFile(path, text, os.ModeAppend)
}
// checkDirectory ...
func checkDirectory(path string) bool {
stat, err := os.Stat(path)
if err != nil {
fmt.Println(err)
}
return stat.IsDir()
}