-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.go
More file actions
144 lines (106 loc) · 2.92 KB
/
Copy pathcrypto.go
File metadata and controls
144 lines (106 loc) · 2.92 KB
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
package main
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/md5"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
)
func generateRandomBytes(size int) (string, error) {
b := make([]byte, size)
_, err := rand.Read(b)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(b), nil
}
func deriveKey(password string, saltBase64 string, hashMode string) ([]byte, error) {
salt, err := base64.StdEncoding.DecodeString(saltBase64)
if err != nil {
return nil, err
}
data := append(salt, []byte(password)...)
var key []byte
if hashMode == "SHA256" {
hash := sha256.Sum256(data)
key = hash[:]
} else {
hash := md5.Sum(data)
key = append(hash[:], hash[:]...)
}
return key, nil
}
func pkcs7pad(data []byte, blockSize int) []byte {
padding := blockSize - len(data)%blockSize
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
return append(data, padtext...)
}
func pkcs7Unpad(data []byte) ([]byte, error) {
length := len(data)
if length == 0 {
return nil, errors.New("padding error: empty data")
}
padLen := int(data[length-1])
if padLen > length || padLen == 0 {
return nil, errors.New("padding error: invalid padding size")
}
for _, v := range data[length-padLen:] {
if int(v) != padLen {
return nil, errors.New("padding error: invalid padding bytes")
}
}
return data[:length-padLen], nil
}
func encryptNote(key []byte, plaintext string) (string, string, error) {
block, err := aes.NewCipher(key)
if err != nil {
return "", "", err
}
iv := make([]byte, aes.BlockSize)
if _, err := rand.Read(iv); err != nil {
return "", "", err
}
paddedData := pkcs7pad([]byte(plaintext), aes.BlockSize)
ciphertext := make([]byte, len(paddedData))
mode := cipher.NewCBCEncrypter(block, iv)
mode.CryptBlocks(ciphertext, paddedData)
return base64.StdEncoding.EncodeToString(iv), base64.StdEncoding.EncodeToString(ciphertext), nil
}
func decryptNote(key []byte, ivBase64 string, ciphertextBase64 string) (string, error) {
iv, err := base64.StdEncoding.DecodeString(ivBase64)
if err != nil {
return "", err
}
ciphertext, err := base64.StdEncoding.DecodeString(ciphertextBase64)
if err != nil {
return "", err
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
if len(ciphertext)%aes.BlockSize != 0 {
return "", errors.New("ciphertext is not a multiple of the block size")
}
mode := cipher.NewCBCDecrypter(block, iv)
plaintextPadded := make([]byte, len(ciphertext))
mode.CryptBlocks(plaintextPadded, ciphertext)
plaintext, err := pkcs7Unpad(plaintextPadded)
if err != nil {
return "", err
}
return string(plaintext), nil
}
func computeIntegrityHash(ivBase64, ciphertextBase64, hashMode string) string {
data := ivBase64 + ciphertextBase64
if hashMode == "SHA256" {
hash := sha256.Sum256([]byte(data))
return hex.EncodeToString(hash[:])
}
hash := md5.Sum([]byte(data))
return hex.EncodeToString(hash[:])
}