-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryption.go
217 lines (190 loc) · 10.4 KB
/
encryption.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
package encryption
import (
"os"
"io"
"fmt"
filepath "path/filepath"
base64 "encoding/base64"
hex "encoding/hex"
random "crypto/rand"
bcrypt "golang.org/x/crypto/bcrypt"
secretbox "golang.org/x/crypto/nacl/secretbox"
chacha "golang.org/x/crypto/chacha20poly1305"
)
func SecretBoxGenerateRandomKey() ( key [32]byte ) {
random.Read( key[:] )
// fmt.Printf( "%x\n" , key )
return
}
func GenerateRandomString( byte_length int ) ( result string ) {
b := make( []byte , byte_length )
random.Read( b )
result = hex.EncodeToString( b )
return
}
func SecretBoxGenerateKey( password string ) ( key [32]byte ) {
password_bytes := []byte( password )
hashed_password , _ := bcrypt.GenerateFromPassword( password_bytes , ( bcrypt.DefaultCost + 3 ) )
copy( key[ : ] , hashed_password[ : 32 ] )
// fmt.Printf( "%x\n" , key )
return
}
func SecretBoxEncrypt( key string , plain_text string ) ( result string ) {
key_hex , _ := hex.DecodeString( key )
var key_bytes [32]byte
copy( key_bytes[ : ], key_hex )
plain_text_bytes := []byte( plain_text )
var nonce [24]byte
io.ReadFull( random.Reader , nonce[ : ] )
encrypted_bytes := secretbox.Seal( nonce[ : ] , plain_text_bytes , &nonce , &key_bytes )
// encrypted_hex_string := hex.EncodeToString( encrypted_bytes[ : ] )
result = base64.StdEncoding.EncodeToString( encrypted_bytes )
return
}
func SecretBoxDecrypt( key string , encrypted string ) ( result string ) {
key_hex , _ := hex.DecodeString( key )
var key_bytes [32]byte
copy( key_bytes[ : ], key_hex )
encrypted_bytes , _ := base64.StdEncoding.DecodeString( encrypted )
var nonce [24]byte
copy( nonce[ : ] , encrypted_bytes[ 0 : 24 ] )
decrypted , _ := secretbox.Open( nil , encrypted_bytes[ 24 : ] , &nonce , &key_bytes )
result = string( decrypted )
return
}
func TestSecretBoxKeyGeneration() {
x := SecretBoxGenerateRandomKey()
x_hex := hex.EncodeToString( x[ : ] )
// x_b64 := base64.StdEncoding.EncodeToString( x )
fmt.Printf( "%x === %s === %d\n" , x , x_hex , len( x ) )
y := SecretBoxGenerateKey( "2432612431332431436c754a424778736e66796a794b466c32356e794f614836" )
y_hex := hex.EncodeToString( y[ : ] )
// y_b64 := base64.StdEncoding.EncodeToString( y )
fmt.Printf( "%x === %s === %d\n" , y , y_hex , len( y ) )
}
func TestSecretBoxEncryptAndDecrypt() {
key_test := "243261243133246a6a515235666e306545754a5233712e4247787a5865455048"
encrypted_test := SecretBoxEncrypt( key_test , "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Lorem ipsum dolor sit" )
fmt.Println( encrypted_test )
fmt.Println( SecretBoxDecrypt( key_test , encrypted_test ) )
}
func ChaChaGenerateKey( password string ) ( key [32]byte ) {
password_bytes := []byte( password )
hashed_password , _ := bcrypt.GenerateFromPassword( password_bytes , ( bcrypt.DefaultCost + 3 ) )
copy( key[ : ] , hashed_password[ : 32 ] )
// fmt.Printf( "%x\n" , key )
return
}
func ChaChaEncryptString( key string , plain_text string ) ( result string ) {
key_hex , _ := hex.DecodeString( key )
var key_bytes [32]byte
copy( key_bytes[ : ], key_hex )
plain_text_bytes := []byte( plain_text )
aead , _ := chacha.New( key_bytes[ : ] )
nonce := make( []byte , aead.NonceSize() )
io.ReadFull( random.Reader , nonce[ : ] )
encrypted_bytes := aead.Seal( nil , nonce , plain_text_bytes , nil )
encrypted_bytes_with_nonce := append( nonce[:] , encrypted_bytes... )
result = base64.StdEncoding.EncodeToString( encrypted_bytes_with_nonce )
return
}
func ChaChaDecryptBase64String( key string , encrypted string ) ( result string ) {
key_hex , _ := hex.DecodeString( key )
var key_bytes [32]byte
copy( key_bytes[ : ], key_hex )
encrypted_bytes , _ := base64.StdEncoding.DecodeString( encrypted )
aead , _ := chacha.New( key_bytes[ : ] )
nonce := make( []byte , aead.NonceSize() )
copy( nonce[ : ] , encrypted_bytes[ 0 : aead.NonceSize() ] )
decrypted , _ := aead.Open( nil , nonce , encrypted_bytes[ aead.NonceSize() : ] , nil )
result = string( decrypted )
return
}
func ChaChaEncryptBytes( key string , plain_text_bytes []byte ) ( result []byte ) {
key_hex , _ := hex.DecodeString( key )
var key_bytes [32]byte
copy( key_bytes[ : ], key_hex )
aead , _ := chacha.New( key_bytes[ : ] )
nonce := make( []byte , aead.NonceSize() )
io.ReadFull( random.Reader , nonce[ : ] )
encrypted_bytes := aead.Seal( nil , nonce , plain_text_bytes , nil )
result = append( nonce[:] , encrypted_bytes... )
return
}
func ChaChaDecryptBytes( key string , encrypted_bytes []byte ) ( result []byte ) {
key_hex , _ := hex.DecodeString( key )
var key_bytes [32]byte
copy( key_bytes[ : ], key_hex )
aead , _ := chacha.New( key_bytes[ : ] )
nonce := make( []byte , aead.NonceSize() )
copy( nonce[ : ] , encrypted_bytes[ 0 : aead.NonceSize() ] )
decrypted , _ := aead.Open( nil , nonce , encrypted_bytes[ aead.NonceSize() : ] , nil )
result = decrypted
return
}
func Test_ChaChaEncryptDecrypt() {
// ChaChaGenerateKey( "2432612431332431436c754a424778736e66796a794b466c32356e794f614836" )
x := ChaChaEncryptString( "2432612431332431436c754a424778736e66796a794b466c32356e794f614836" , "asdf" )
y := ChaChaDecryptBase64String( "2432612431332431436c754a424778736e66796a794b466c32356e794f614836" , x )
fmt.Printf( "%+v\n" , x )
fmt.Println( y )
}
func ChaChaEncryptFile( key string , file_path string ) error {
key_hex , _ := hex.DecodeString( key )
var key_bytes [32]byte
copy( key_bytes[:] , key_hex )
aead, _ := chacha.New( key_bytes[:] )
in_file , err := os.Open( file_path )
if err != nil { return err }
defer in_file.Close()
outFile, err := os.Create( file_path + ".encrypted" )
if err != nil {
return err
}
defer outFile.Close()
nonce := make( []byte , aead.NonceSize() )
io.ReadFull( random.Reader, nonce[:] )
_ , err = outFile.Write( nonce )
if err != nil { return err }
chunk_size := 1024
buffer := make( []byte , chunk_size )
for {
n , err := in_file.Read( buffer )
if err == io.EOF { break }
if err != nil { return err }
encrypted_bytes := aead.Seal( nil , nonce , buffer[ :n ] , nil )
_, err = outFile.Write( encrypted_bytes )
if err != nil { return err }
}
return nil
}
func ChaChaDecryptFile( key string , file_path string ) error {
key_hex, _ := hex.DecodeString(key)
var key_bytes [32]byte
copy( key_bytes[:], key_hex )
aead, _ := chacha.New( key_bytes[:] )
in_file , err := os.Open( file_path )
if err != nil { return err }
defer in_file.Close()
output_file_path := file_path[ :len( file_path )-len( ".encrypted" ) ]
original_extension := filepath.Ext( output_file_path )
decrypted_file_name := output_file_path + ".decrypted" + original_extension
out_file , err := os.Create( decrypted_file_name )
if err != nil { return err }
defer out_file.Close()
nonce := make([]byte, aead.NonceSize())
_ , err = io.ReadFull( in_file , nonce)
if err != nil { return err }
chunk_size := 1024
buffer := make( []byte , ( chunk_size + aead.Overhead() ) )
for {
n, err := in_file.Read( buffer )
if err == io.EOF { break }
if err != nil { return err }
decrypted_bytes , err := aead.Open( nil , nonce , buffer[ :n ] , nil )
if err != nil { return err }
_ , err = out_file.Write( decrypted_bytes )
if err != nil { return err }
}
return nil
}