-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.go
115 lines (98 loc) · 3.01 KB
/
functions.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
package decoders
import (
"errors"
"io/ioutil"
"os"
"strings"
)
// function designed to create and initialize a new XORDecoder object.
func NewXORDecoder(options ...XORDecoderOptsFunc) (decoder *XORDecoder, err error) {
var optionstruct XORDecoderOptions = XORDecoderOptions{}
decoder = &XORDecoder{}
// loop through options and configure new decoder.
for _, fn := range options {
err = fn(&optionstruct)
if err != nil {
return nil, err
}
}
// switch statement to check the ciphertext.
switch {
case (len(optionstruct.Ciphertext) < 1) && (len(optionstruct.Filename) < 1):
return nil, errors.New("must specify file or ciphertext for XORDecoder")
case (len(optionstruct.Ciphertext) > 0) && (len(optionstruct.Filename) > 0):
return nil, errors.New("cannot specify both filename and ciphertext")
case (len(optionstruct.Filename) > 0):
decoder.ciphertext, err = readCiphertextFile(optionstruct.Filename)
if err != nil {
return nil, err
}
default:
decoder.ciphertext = optionstruct.Ciphertext
}
// switch statement to check the encryption key.
switch {
case (optionstruct.EncryptionKey == nil) && (len(optionstruct.EncryptionKeyFile) < 1):
decoder.EncryptionKey = decoder.ciphertext
case len(optionstruct.EncryptionKeyFile) > 0:
decoder.EncryptionKey, err = readCiphertextFile(optionstruct.EncryptionKeyFile)
if err != nil {
return nil, err
}
case optionstruct.EncryptionKey != nil:
decoder.EncryptionKey = optionstruct.EncryptionKey
default:
decoder.EncryptionKey = optionstruct.EncryptionKey
}
return decoder, nil
}
// function designed to read a file, extract the ciphertext from it and return it.
func readCiphertextFile(filename string) (ciphertext []byte, err error) {
var fptr *os.File
fptr, err = os.Open(filename)
if err != nil {
return nil, err
}
defer fptr.Close()
ciphertext, err = ioutil.ReadAll(fptr)
if err != nil {
return nil, err
}
ciphertext = []byte(strings.Trim(string(ciphertext), " \n\r\t"))
if (len(ciphertext) < 1) || (ciphertext == nil) {
return nil, errors.New("cipher file empty")
}
return ciphertext, nil
}
// xordecoderoptsfunc designed to specify the ciphertext to
// use when attempting to decode.
func WithCiphertext(ciphertext []byte) XORDecoderOptsFunc {
return func(xo *XORDecoderOptions) error {
xo.Ciphertext = ciphertext
return nil
}
}
// xordecoderoptsfunc designed to specify the name of the file
// in which the ciphertext is stored.
func WithFile(filename string) XORDecoderOptsFunc {
return func(xo *XORDecoderOptions) error {
xo.Filename = filename
return nil
}
}
// xordecoderoptsfunc designed to specify the encryption key
// to use.
func WithKey(keybytes []byte) XORDecoderOptsFunc {
return func(xo *XORDecoderOptions) error {
xo.EncryptionKey = keybytes
return nil
}
}
// xordecoderoptsfunc designed to specify the encryption key
// file to read the encryption key from.
func WithKeyFile(filename string) XORDecoderOptsFunc {
return func(xo *XORDecoderOptions) error {
xo.EncryptionKeyFile = filename
return nil
}
}