-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
93 lines (81 loc) · 2.01 KB
/
crypto.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
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"encoding/hex"
"io"
)
// generateID 生成一个随机的 ID
func generateID() string {
buf := make([]byte, 32)
io.ReadFull(rand.Reader, buf)
return hex.EncodeToString(buf)
}
// hashKey 生成一个 key 的 hash
func hashKey(key string) string {
hash := md5.Sum([]byte(key))
return hex.EncodeToString(hash[:])
}
// newEncryptionKey 生成一个新的加密 key
func newEncryptionKey() []byte {
keyBuf := make([]byte, 32)
io.ReadFull(rand.Reader, keyBuf)
return keyBuf
}
// copyStream 从 src 中读取数据,加密后写入到 dst 中
func copyStream(stream cipher.Stream, blockSize int, src io.Reader, dst io.Writer) (int, error) {
var (
buf = make([]byte, 32*1024)
nw = blockSize
)
for {
n, err := src.Read(buf)
if n > 0 {
stream.XORKeyStream(buf, buf[:n])
nn, err := dst.Write(buf[:n])
if err != nil {
return 0, err
}
nw += nn
}
if err == io.EOF {
break
}
if err != nil {
return 0, err
}
}
return nw, nil
}
// copyDecrypt 从 src 中读取数据,解密后写入到 dst 中
func copyDecrypt(key []byte, src io.Reader, dst io.Writer) (int, error) {
block, err := aes.NewCipher(key)
if err != nil {
return 0, err
}
// 从给定的 io.Reader 读取 IV,在示例中,它应该是读取的块.BlockSize()字节
iv := make([]byte, block.BlockSize())
if _, err := src.Read(iv); err != nil {
return 0, err
}
stream := cipher.NewCTR(block, iv)
return copyStream(stream, block.BlockSize(), src, dst)
}
func copyEncrypt(key []byte, src io.Reader, dst io.Writer) (int, error) {
block, err := aes.NewCipher(key)
if err != nil {
return 0, err
}
iv := make([]byte, block.BlockSize()) // 16 bytes
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return 0, err
}
// 在 dst 中写入 IV,这样我们就可以在解密时读取它
if _, err := dst.Write(iv); err != nil {
return 0, err
}
stream := cipher.NewCTR(block, iv)
return copyStream(stream, block.BlockSize(), src, dst)
}