-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmods.go
76 lines (67 loc) · 2.04 KB
/
mods.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
package tardigrade
// Built Sat 4 Mar 12:32:07 GMT 2023
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"encoding/json"
)
// MyMarshal function is adapted to SetEscapeHTML to false before encoding
func (tar *Tardigrade) MyMarshal(t interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
err := encoder.Encode(t)
return buffer.Bytes(), err
}
// MyIndent function is adapted to SetEscapeHTML to false before encoding and indenting
func (tar *Tardigrade) MyIndent(v interface{}, prefix, indent string) ([]byte, error) {
b, err := tar.MyMarshal(v)
if err != nil {
return nil, err
}
var buffer bytes.Buffer
err = json.Indent(&buffer, b, prefix, indent)
if err != nil {
return nil, err
}
return buffer.Bytes(), nil
}
// MyEncode returns the base64 encoding of source
func (tar *Tardigrade) MyEncode(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}
// MyDecode returns the bytes represented by the base64 string s
func (tar *Tardigrade) MyDecode(s string) []byte {
data, err := base64.StdEncoding.DecodeString(s)
if err != nil {
panic(err)
}
return data
}
var bytez = []byte{33, 45, 67, 28, 75, 15, 26, 77, 97, 25, 28, 91, 55, 31, 44, 69}
// Encrypt method is to encrypt or hide any classified text
func (tar *Tardigrade) MyEncrypt(text, Password string) (string, error) {
block, err := aes.NewCipher([]byte(Password))
if err != nil {
return "", err
}
plainText := []byte(text)
cfb := cipher.NewCFBEncrypter(block, bytez)
cipherText := make([]byte, len(plainText))
cfb.XORKeyStream(cipherText, plainText)
return tar.MyEncode(cipherText), nil
}
// Decrypt method is to extract back the encrypted text
func (tar *Tardigrade) MyDecrypt(text, Password string) (string, error) {
block, err := aes.NewCipher([]byte(Password))
if err != nil {
return "", err
}
cipherText := tar.MyDecode(text)
cfb := cipher.NewCFBDecrypter(block, bytez)
plainText := make([]byte, len(cipherText))
cfb.XORKeyStream(plainText, cipherText)
return string(plainText), nil
}