Repository navigation
Expand file tree
/
Copy pathutilities.go
More file actions
executable file
·146 lines (121 loc) · 2.82 KB
/
Copy pathutilities.go
File metadata and controls
executable file
·146 lines (121 loc) · 2.82 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package manager
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"strconv"
"strings"
"unicode"
"net"
"fmt"
"os"
"log"
"github.com/joho/godotenv"
)
func CleanString(data string) string {
data = strings.TrimFunc(data, func(r rune) bool {
return !unicode.IsGraphic(r)
})
return data
}
func ParseLoc(data string) string {
s := Split(data)
locY, _ := strconv.Atoi(s[8])
width := 900
var result string
for i := 0; i < 11; i++ {
if i == 8 {
y := width - locY
s[i] = strconv.Itoa(y)
}
result += s[i]
if i != 10 {
result += ","
}
}
return result
}
func Split(data string) []string {
d := strings.Split(data, ",")
return d
}
func Swap(data string) string {
datas := []rune(data)
pointer1 := 0
pointer2 := len(datas) - 1
for pointer1 < pointer2 {
datas[pointer1], datas[pointer2] = datas[pointer2], datas[pointer1]
pointer1 += 1
pointer2 -= 1
}
datas[len(datas)-1] = 0
return string(datas)
}
func GetID(data string) string {
id := strings.Split(data, ",")
return id[0]
}
func GetIP() string {
ifaces, _ := net.Interfaces()
// handle err
var ip net.IP
for _, i := range ifaces {
addrs, _ := i.Addrs()
// handle err
for _, addr := range addrs {
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
}
}
return string(ip)
}
func DecryptAESGCM(ivHex, tagHex, cipherHex string) (string, error) {
err := godotenv.Load()
if err != nil {
log.Fatal(".env tidak ditemukan")
}
keyEnv := os.Getenv("AES_KEY_GCM")
if len(keyEnv) != 32 {
log.Fatal("AES_KEY_GCM tidak valid")
}
key := []byte(keyEnv)
// Convert HEX → BYTES
iv, err := hex.DecodeString(ivHex)
if err != nil {
return "", fmt.Errorf("[ERR] hex iv decode: %v", err)
}
tag, err := hex.DecodeString(tagHex)
if err != nil {
return "", fmt.Errorf("[ERR] hex tag decode: %v", err)
}
ciphertext, err := hex.DecodeString(cipherHex)
if err != nil {
return "", fmt.Errorf("[ERR] hex cipher decode: %v", err)
}
// Validate decoded sizes (bytes)
if len(iv) != 12 {
return "", fmt.Errorf("[ERR] IV must be 12 bytes, got: %s", strconv.Itoa(len(iv)))
}
if len(tag) != 16 {
return "", fmt.Errorf("[ERR] Tag must be 16 bytes, got: %s", strconv.Itoa(len(tag)))
}
// Append tag to ciphertext (GCM expects ciphertext|tag passed to Open)
ciphertext = append(ciphertext, tag...)
block, err := aes.NewCipher(key)
if err != nil {
return "", fmt.Errorf("[ERR] new cipher: %v", err)
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", fmt.Errorf("[ERR] new GCM: %v", err)
}
plain, err := gcm.Open(nil, iv, ciphertext, nil)
if err != nil {
return "", fmt.Errorf("[ERR] gcm open: %v", err)
}
return string(plain), nil
}