-
Notifications
You must be signed in to change notification settings - Fork 1
/
base64.go
61 lines (51 loc) · 1.65 KB
/
base64.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
package obfuscate
import (
"encoding/base64"
)
// Base64 structure maintains a rounds value and satisfies Obfuscator interface.
type Base64 struct {
Rounds uint `json:"rounds"`
}
// Obfuscate Base64 encodes input bytes by a number of rounds. Returns encoded output and an error value.
func (b *Base64) Obfuscate(input []byte) (output []byte, err error) {
return b.run(input, "obf")
}
// Deobfuscate Base64 decodes input bytes by a number of rounds. Returns decoded output and an error value.
func (b *Base64) Deobfuscate(input []byte) (output []byte, err error) {
return b.run(input, "deobf")
}
// run enables the "rounds" loop to be implemented only once.
//
// The act parameter determines if obfuscation or deobfuscation occurs.
//
// - obf - Indicates obfuscation.
// - deobf - Indicates deobfuscation.
func (b *Base64) run(input []byte, act string) (output []byte, err error) {
for r := b.Rounds; r > 0; r-- {
if act == "obf" {
output = Base64Encode(input)
} else {
if output, err = Base64Decode(input); err != nil {
break
}
}
if r != 1 {
input = make([]byte, len(output))
copy(input, output)
}
}
return output, err
}
func Base64Encode(in []byte) (out []byte) {
out = make([]byte, base64.StdEncoding.EncodedLen(len(in)))
base64.StdEncoding.Encode(out, in)
return out
}
func Base64Decode(in []byte) (out []byte, err error) {
out = make([]byte, base64.StdEncoding.DecodedLen(len(in)))
var n int
if n, err = base64.StdEncoding.Decode(out, in); err == nil {
out = out[:n]
}
return out, err
}