-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
64 lines (58 loc) · 1.31 KB
/
util.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
package main
import (
"encoding/hex"
"fmt"
"log"
"math/rand"
"syscall"
"time"
)
func SetUlimit(number uint64) {
var rLimit syscall.Rlimit
err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
log.Println("[Error]: Getting Rlimit ", err)
}
rLimit.Max = number
rLimit.Cur = number
err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
log.Println("[Error]: Setting Rlimit ", err)
}
err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &rLimit)
if err != nil {
log.Println("[Error]: Getting Rlimit ", err)
}
log.Println("set file limit done:", rLimit)
}
func decodeString(data string) string {
keybyte, err := hex.DecodeString(string(data[:2]))
if err != nil {
return ""
}
key := uint(keybyte[0])
decodeStr := ""
if len(data)%2 != 0 {
return ""
}
for i := 2; i < len(data)-1; i += 2 {
value := data[i : i+2]
byte, err := hex.DecodeString(value)
if err != nil {
continue
}
decodeStr += string(uint(byte[0]) ^ key)
}
return decodeStr
}
func encodeString(data string, key int) string {
r := rand.New(rand.NewSource(time.Now().Unix()))
if key == 0 || key > 99 {
key = r.Intn(99)
}
encodeStr := fmt.Sprintf("%02s", fmt.Sprintf("%x", key))
for _, v := range data {
encodeStr += fmt.Sprintf("%02s", fmt.Sprintf("%x", int(v)^key))
}
return encodeStr
}