1
1
package S_DES
2
2
3
3
import (
4
+ "encoding/json"
4
5
"io"
6
+ "io/ioutil"
5
7
"log"
6
8
"os"
9
+ "sync"
10
+ "time"
7
11
)
8
12
9
13
const (
@@ -16,6 +20,9 @@ type DesEncryptor struct {
16
20
cipher DES_8encryption
17
21
decryptionFileConnector * os.File
18
22
decryptionFilename string
23
+ Fastmode bool `json:"fastmode"`
24
+ ConcurrentPaths int `json:"threadcount"`
25
+ Chunksize int `json:"buffersize"`
19
26
}
20
27
21
28
func (encryptor * DesEncryptor ) getBinaryByteArray (byteVal byte ) []byte {
@@ -114,6 +121,63 @@ func (engine *DesEncryptor) writeDecryptionBufferToFile(decryptionBuffer *[][]by
114
121
}
115
122
}
116
123
124
+ func (encryptor * DesEncryptor ) concurrentChunkEncryption (waitGroup * sync.WaitGroup , buffer * []byte , bufferDataSize int , encryptionBuffer * [][]byte ) {
125
+ defer waitGroup .Done ()
126
+ encryptor .encryptChunk (buffer , bufferDataSize , encryptionBuffer )
127
+ }
128
+
129
+ /**
130
+ Uses goroutines to encrypt the file in chunks
131
+ */
132
+ func (encryptor * DesEncryptor ) fastEncrypt (file * os.File ) {
133
+ // Concurrent encryption of multiple chunks
134
+ dataBuffer := make ([][]byte , encryptor .ConcurrentPaths ) // holds the primary data to be encypted.
135
+ for i := 0 ; i < len (dataBuffer ); i ++ {
136
+ dataBuffer [i ] = make ([]byte , encryptor .Chunksize )
137
+ }
138
+ // var encryptionBuffer [][][]byte
139
+ encryptionBuffer := make ([][][]byte , encryptor .ConcurrentPaths )
140
+ for i := 0 ; i < len (encryptionBuffer ); i ++ {
141
+ encryptionBuffer [i ] = make ([][]byte , encryptor .Chunksize )
142
+ for j := 0 ; j < len (encryptionBuffer [i ][j ]); j ++ {
143
+ encryptionBuffer [i ][j ] = make ([]byte , 8 )
144
+ }
145
+ }
146
+
147
+ waitGroup := new (sync.WaitGroup )
148
+ chunkMap := make ([]int , len (dataBuffer )) // stores the count of bytes w.r.t every chunk.
149
+ var err error
150
+ cnt := 0
151
+ for err != io .EOF {
152
+ err = nil
153
+ bytesread := 0
154
+ chunkCount := 0
155
+ for i := 0 ; i < len (dataBuffer ); i ++ {
156
+ bytesread , err = file .Read (dataBuffer [i ])
157
+ if err != nil {
158
+ if err != io .EOF {
159
+ log .Println ("Problem reading from primary file" , err )
160
+ }
161
+ break
162
+ }
163
+ waitGroup .Add (1 )
164
+ go encryptor .concurrentChunkEncryption (waitGroup , & dataBuffer [i ], bytesread , & encryptionBuffer [i ])
165
+ chunkCount += 1
166
+ chunkMap [i ] = bytesread
167
+ }
168
+ waitGroup .Wait ()
169
+ // if chunkCount == len(dataBuffer) {
170
+ // fmt.Println("chunkCount and len(dataBuffer) are same")
171
+ // }
172
+ // Write encryption buffer into the file
173
+ for i := 0 ; i < chunkCount ; i ++ {
174
+ encryptor .writeEncryptionBufferToFile (& encryptionBuffer [i ], chunkMap [i ], encryptor .encryptionFilename )
175
+ }
176
+ cnt += 1
177
+ // fmt.Println("End of a loop, cnt:", cnt)
178
+ }
179
+ }
180
+
117
181
/**
118
182
Reads in a chunk of byte-data from file.
119
183
Encrypts it.
@@ -136,27 +200,37 @@ func (encryptor *DesEncryptor) runEncryption(filename string) {
136
200
}
137
201
138
202
var buffer []byte
139
- buffer = make ([]byte , bufferSize )
203
+ buffer = make ([]byte , encryptor . Chunksize )
140
204
var encryptionBuffer [][]byte
141
- encryptionBuffer = make ([][]byte , bufferSize )
142
- for i := 0 ; i < bufferSize ; i ++ {
205
+ encryptionBuffer = make ([][]byte , encryptor . Chunksize )
206
+ for i := 0 ; i < encryptor . Chunksize ; i ++ {
143
207
encryptionBuffer [i ] = make ([]byte , 8 )
144
208
}
145
209
146
- for {
147
- bytesread , err := file .Read (buffer )
148
- if err != nil {
149
- if err != io .EOF {
150
- log .Fatalln ("Problem while reading the contents of the file." )
210
+ if encryptor .Fastmode == true {
211
+ log .Println ("Fastmode enabled encryption..." )
212
+ concurrentStart := time .Now ()
213
+ encryptor .fastEncrypt (file )
214
+ concurrentElapsed := time .Since (concurrentStart )
215
+ log .Println ("The total time for fast mode: " , concurrentElapsed )
216
+ } else {
217
+ log .Println ("Using normal mode encryption..." )
218
+ normalStart := time .Now ()
219
+ for {
220
+ bytesread , err := file .Read (buffer )
221
+ if err != nil {
222
+ if err != io .EOF {
223
+ log .Fatalln ("Problem while reading the contents of the file." )
224
+ }
225
+ break
151
226
}
152
- break
227
+ encryptor .encryptChunk (& buffer , bytesread , & encryptionBuffer )
228
+ // Write encryptionBuffer into file.
229
+ encryptor .writeEncryptionBufferToFile (& encryptionBuffer , bytesread , encryptor .encryptionFilename )
230
+ // fmt.Println("bytesread:", bytesread, "bytes to string:", string(buffer[:bytesread]))
153
231
}
154
-
155
- encryptor .encryptChunk (& buffer , bytesread , & encryptionBuffer )
156
- // Write encryptionBuffer into file.
157
- encryptor .writeEncryptionBufferToFile (& encryptionBuffer , bytesread , encryptor .encryptionFilename )
158
-
159
- // fmt.Println("bytesread:", bytesread, "bytes to string:", string(buffer[:bytesread]))
232
+ normalElapsed := time .Since (normalStart )
233
+ log .Println ("The total time for normal mode: " , normalElapsed )
160
234
}
161
235
}
162
236
@@ -224,12 +298,12 @@ func (encryptor *DesEncryptor) EncryptFile(filename string) bool {
224
298
return false
225
299
}
226
300
227
- // File encryption is successful. Delete the primary file from disk.
228
- err = os .Remove (filename )
229
- if err != nil {
230
- log .Println ("Encryption failure for file:" , encryptor .encryptionFilename , err )
231
- return false
232
- }
301
+ // // File encryption is successful. Delete the primary file from disk.
302
+ // err = os.Remove(filename)
303
+ // if err != nil {
304
+ // log.Println("Encryption failure for file:", encryptor.encryptionFilename, err)
305
+ // return false
306
+ // }
233
307
234
308
log .Println ("File-Encryption procedure complete..." )
235
309
return true
@@ -281,8 +355,20 @@ func (engine *DesEncryptor) DecryptFile(filename string) bool {
281
355
return true
282
356
}
283
357
358
+ /**
359
+ Reads the DES configuration file. Initializes the struct members.
360
+ */
361
+ func (engine * DesEncryptor ) readConfigurationFile (configFilename string ) {
362
+ data , err := ioutil .ReadFile (configFilename )
363
+ if err != nil {
364
+ log .Fatalln ("Problem reading the DES configuration file..." , err )
365
+ }
366
+ err = json .Unmarshal (data , & engine )
367
+ }
368
+
284
369
func (engine * DesEncryptor ) Init (filename string ) {
285
370
log .Println ("Initializing FES engine..." )
371
+ engine .readConfigurationFile ("des_config.json" )
286
372
engine .cipher .Init (filename )
287
373
log .Println ("FES engine intialization complete..." )
288
374
}
0 commit comments