-
Notifications
You must be signed in to change notification settings - Fork 2
/
files.go
243 lines (225 loc) · 5.68 KB
/
files.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package files
import (
"bufio"
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"errors"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"runtime"
"strings"
)
func GetCurrentAbPathByCaller() string {
var abPath string
_, filename, _, ok := runtime.Caller(0)
if ok {
abPath = path.Dir(path.Dir(filename))
}
return abPath
}
func GetPathBySuffix(path string, suffix string) []string {
var allFiles []string
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
return allFiles
}
for _, file := range files {
if file.IsDir() {
allFiles = append(allFiles, GetPathBySuffix(path+"/"+file.Name(), suffix)...)
} else {
if strings.HasSuffix(file.Name(), suffix) {
allFiles = append(allFiles, path+"/"+file.Name())
}
}
}
return allFiles
}
func MoveChildToParent(dirPath string) error {
files, err := ioutil.ReadDir(dirPath)
if err != nil {
return err
}
for _, file := range files {
if file.IsDir() {
childPath := dirPath + string(filepath.Separator) + file.Name()
childFiles, err := ioutil.ReadDir(childPath)
if err != nil {
return err
}
for _, f := range childFiles {
err := os.Rename(childPath+string(filepath.Separator)+f.Name(), dirPath+string(filepath.Separator)+f.Name())
if err != nil {
return err
}
}
os.RemoveAll(childPath)
}
}
return nil
}
var PwdKey = []byte("ABCDABCDABCDABCD") //key不能泄露
//pkcs7Padding 填充
func pkcs7Padding(data []byte, blockSize int) []byte {
//判断缺少几位长度。最少1,最多 blockSize
padding := blockSize - len(data)%blockSize
//补足位数。把切片[]byte{byte(padding)}复制padding个
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(data, padText...)
}
//pkcs7UnPadding 填充的反向操作
func pkcs7UnPadding(data []byte) ([]byte, error) {
length := len(data)
if length == 0 {
return nil, errors.New("加密字符串错误!")
}
//获取填充的个数
unPadding := int(data[length-1])
return data[:(length - unPadding)], nil
}
//AesEncrypt 加密
//加密过程:
// 1、处理数据,对数据进行填充,采用PKCS7(当密钥长度不够时,缺几位补几个几)的方式。
// 2、对数据进行加密,采用AES加密方法中CBC加密模式
// 3、对得到的加密数据,进行base64加密,得到字符串
func AesEncrypt(data []byte, key []byte) ([]byte, error) {
//创建加密实例
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
//判断加密快的大小
blockSize := block.BlockSize()
//填充
encryptBytes := pkcs7Padding(data, blockSize)
//初始化加密数据接收切片
crypted := make([]byte, len(encryptBytes))
//使用cbc加密模式
blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
//执行加密
blockMode.CryptBlocks(crypted, encryptBytes)
return crypted, nil
}
//AesDecrypt 解密
// 解密过程相反
//16,24,32位字符串的话,分别对应AES-128,AES-192,AES-256 加密方法
func AesDecrypt(data []byte, key []byte) ([]byte, error) {
//创建实例
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
//获取块的大小
blockSize := block.BlockSize()
//使用cbc
blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
//初始化解密数据接收切片
crypted := make([]byte, len(data))
//执行解密
blockMode.CryptBlocks(crypted, data)
//去除填充
crypted, err = pkcs7UnPadding(crypted)
if err != nil {
return nil, err
}
return crypted, nil
}
//EncryptByAes Aes加密 后 base64 再加
func EncryptByAes(data []byte) (string, error) {
res, err := AesEncrypt(data, PwdKey)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(res), nil
}
//DecryptByAes Aes 解密
func DecryptByAes(data string) ([]byte, error) {
dataByte, err := base64.StdEncoding.DecodeString(data)
if err != nil {
return nil, err
}
return AesDecrypt(dataByte, PwdKey)
}
// 更新 文件 的加解密
//EncryptFile 文件加密,filePath 需要加密的文件路径 ,fName加密后文件名
func EncryptFile(filePath, fName string) (err error) {
f, err := os.Open(filePath)
if err != nil {
return err
}
defer f.Close()
fInfo, _ := f.Stat()
fLen := fInfo.Size()
//100mb 每 100mb 进行加密一次
maxLen := 1024 * 1024 * 100
var forNum int64 = 0
getLen := fLen
if fLen > int64(maxLen) {
getLen = int64(maxLen)
forNum = fLen / int64(maxLen)
}
//加密后存储的文件
ff, err := os.OpenFile(fName, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
defer ff.Close()
//循环加密,并写入文件
for i := 0; i < int(forNum+1); i++ {
a := make([]byte, getLen)
n, err := f.Read(a)
if err != nil {
return err
}
getByte, err := EncryptByAes(a[:n])
if err != nil {
return err
}
//换行处理
getBytes := append([]byte(getByte), []byte("\n")...)
//写入
buf := bufio.NewWriter(ff)
buf.WriteString(string(getBytes[:]))
buf.Flush()
}
//ffInfo, _ := ff.Stat()
//fmt.Printf("文件加密成功,生成文件名为:%s,文件大小为:%v Byte \n", ffInfo.Name(), ffInfo.Size())
return nil
}
//DecryptFile 文件解密
func DecryptFile(sourceFile, destFile string) (err error) {
f1, err := os.Open(sourceFile)
if err != nil {
return err
}
defer f1.Close()
//fInfo, _ := f.Stat()
br := bufio.NewReader(f1)
f2, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
defer f2.Close()
num := 0
//逐行读取密文,进行解密,写入文件
for {
num = num + 1
a, err := br.ReadString('\n')
if err != nil {
break
}
getByte, err := DecryptByAes(a)
if err != nil {
return err
}
buf := bufio.NewWriter(f2)
buf.Write(getByte)
buf.Flush()
}
return
}