Skip to content

Commit cd1bf7e

Browse files
committed
Code cleanup; Move some functions to smallFunctions.go file
1 parent cf1666e commit cd1bf7e

File tree

3 files changed

+66
-59
lines changed

3 files changed

+66
-59
lines changed

functions/cli.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ import (
55
"crypto/sha256"
66
"crypto/sha512"
77
"fmt"
8-
"log"
98
"os"
10-
"strings"
119
)
1210

1311
//Function to create a user option interface for encryption purposes
@@ -65,10 +63,3 @@ func Ui() (encryption Parameters, meta Metadata) {
6563

6664
return encryption, meta
6765
}
68-
69-
func CheckEmptyString(str string) {
70-
if strings.TrimSpace(str) == "" {
71-
log.Fatal("empty input")
72-
os.Exit(1)
73-
}
74-
}

functions/pbkdf.go

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@ package helper
22

33
import (
44
"bufio"
5-
"bytes"
65
"crypto/aes"
76
"crypto/cipher"
87
"crypto/des"
98
"crypto/hmac"
109
"crypto/rand"
1110
"crypto/sha256"
1211
"crypto/sha512"
13-
"encoding/base64"
1412
"errors"
1513
"golang.org/x/crypto/pbkdf2"
1614
"hash"
1715
"io"
18-
"log"
1916
"os"
2017
"strings"
2118
)
@@ -130,16 +127,6 @@ func Encryption(password string, plainText string, cli Parameters, meta Metadata
130127

131128
}
132129

133-
func encodeBase64(input []byte) string {
134-
return base64.StdEncoding.EncodeToString(input)
135-
}
136-
137-
func DecodeBase64(input string) []byte {
138-
str, err := base64.StdEncoding.DecodeString(input)
139-
CheckError(err)
140-
return str
141-
}
142-
143130
/*
144131
This function will take in the encrypted file path and the password from the user.
145132
The rest of the parameters required for decryptio, will be extracted from the metadata, written during the encryption process
@@ -258,40 +245,3 @@ func Decryption(file string, password string) (text string, err error) {
258245

259246
return string(unpadText), err
260247
}
261-
262-
//Function to check errors, to prevent a lot if statements in-code
263-
func CheckError(err error) {
264-
if err != nil {
265-
log.Fatal(err)
266-
//panic(err)
267-
}
268-
}
269-
270-
//Function to pad the data and figure out the block size by repeating the bytes
271-
func Pad(src []byte, algorithm string) []byte {
272-
var padding int
273-
274-
if (algorithm == "aes128") || algorithm == "aes256" {
275-
padding = aes.BlockSize - len(src)%aes.BlockSize
276-
} else if algorithm == "3des" {
277-
padding = des.BlockSize - len(src)%des.BlockSize
278-
} else {
279-
err := errors.New("invalid encryption choice")
280-
CheckError(err)
281-
}
282-
283-
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
284-
return append(src, padtext...)
285-
}
286-
287-
//Function to unpad the data
288-
func Unpad(src []byte) ([]byte, error) {
289-
length := len(src)
290-
unpadding := int(src[length-1])
291-
292-
if unpadding > length {
293-
return nil, errors.New("un-padding error. ")
294-
}
295-
296-
return src[:(length - unpadding)], nil
297-
}

functions/smallFunctions.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package helper
2+
3+
import (
4+
"bytes"
5+
"crypto/aes"
6+
"crypto/des"
7+
"encoding/base64"
8+
"errors"
9+
"log"
10+
"os"
11+
"strings"
12+
)
13+
14+
//Function to check errors, to prevent a lot if statements in-code
15+
func CheckError(err error) {
16+
if err != nil {
17+
log.Fatal(err)
18+
//panic(err)
19+
}
20+
}
21+
22+
//Function to pad the data and figure out the block size by repeating the bytes
23+
func Pad(src []byte, algorithm string) []byte {
24+
var padding int
25+
26+
if (algorithm == "aes128") || algorithm == "aes256" {
27+
padding = aes.BlockSize - len(src)%aes.BlockSize
28+
} else if algorithm == "3des" {
29+
padding = des.BlockSize - len(src)%des.BlockSize
30+
} else {
31+
err := errors.New("invalid encryption choice")
32+
CheckError(err)
33+
}
34+
35+
padtext := bytes.Repeat([]byte{byte(padding)}, padding)
36+
return append(src, padtext...)
37+
}
38+
39+
//Function to unpad the data
40+
func Unpad(src []byte) ([]byte, error) {
41+
length := len(src)
42+
unpadding := int(src[length-1])
43+
44+
if unpadding > length {
45+
return nil, errors.New("un-padding error. ")
46+
}
47+
48+
return src[:(length - unpadding)], nil
49+
}
50+
51+
func encodeBase64(input []byte) string {
52+
return base64.StdEncoding.EncodeToString(input)
53+
}
54+
55+
func DecodeBase64(input string) []byte {
56+
str, err := base64.StdEncoding.DecodeString(input)
57+
CheckError(err)
58+
return str
59+
}
60+
61+
func CheckEmptyString(str string) {
62+
if strings.TrimSpace(str) == "" {
63+
log.Fatal("empty input")
64+
os.Exit(1)
65+
}
66+
}

0 commit comments

Comments
 (0)