Skip to content

Commit

Permalink
Merge pull request #47 from xingcxb/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
xingcxb authored Dec 26, 2024
2 parents a1bd9ac + 7bfa116 commit 5e8fec9
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<p>
<strong>
一个用于偷懒少写代码的工具类
首先是一个为了嫖开源`JetBrains`激活码,然后才是一个用于偷懒少写代码的工具类
</strong>
</p>

Expand Down
10 changes: 9 additions & 1 deletion _examples/core/arrayKit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,13 @@ func TestCompare(t *testing.T) {
}

func TestSliceDiff(t *testing.T) {
fmt.Println(arrayKit.SliceDiff([]string{"4", "1", "3"}, []string{"1", "2", "3"}))
a := []string{
"1.1.1.1",
"2.2.2.2"}
b := []string{
"1.1.1.1",
"2.2.2.2",
"203.209.242.97"}
add, del := arrayKit.SliceDiff(a, b)
fmt.Println(add, "\n", del)
}
9 changes: 5 additions & 4 deletions _examples/core/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"fmt"
"github.com/xingcxb/goKit/core/cryptoKit"
"github.com/xingcxb/goKit/core/cryptoKit/urlKit"
"testing"
)

Expand Down Expand Up @@ -39,11 +40,11 @@ func TestUnicodeDecode(t *testing.T) {
}

func TestUrlEncode(t *testing.T) {
fmt.Println(cryptoKit.UrlEncode("https://www.baidu.com/s?wd=你好"))
fmt.Println(urlKit.UrlEncode("https://www.baidu.com/s?wd=你好"))
}

func TestUrlDecode(t *testing.T) {
fmt.Println(cryptoKit.UrlDecode("https%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3D%E4%BD%A0%E5%A5%BD"))
fmt.Println(urlKit.UrlDecode("https%3A%2F%2Fwww.baidu.com%2Fs%3Fwd%3D%E4%BD%A0%E5%A5%BD"))
}

// aes cbc模式加密
Expand Down Expand Up @@ -72,7 +73,7 @@ func TestAESCBCDecrypt(t *testing.T) {
// aes cfb模式加密
func TestAESEncryptCFB(t *testing.T) {
padding := 0
v, err := cryptoKit.AESEncryptCFB([]byte("pibigstar"), []byte("1234567891234567"), &padding, []byte("1231231231231231"))
v, err := cryptoKit.AESEncryptCFB([]byte("E6!@ik^*ufD9Ru"), []byte("1234567891234567"), &padding, []byte("1231231231231231"))
if err != nil {
fmt.Println(err)
return
Expand All @@ -83,7 +84,7 @@ func TestAESEncryptCFB(t *testing.T) {

// aes cfb模式解密
func TestAESDecryptCFB(t *testing.T) {
value, _ := cryptoKit.Base64Decode("qDJPT/XQZNJo5YRfitRUCg==")
value, _ := cryptoKit.Base64Decode("nW0MZvvITplvg8Bm2KE=")
v, err := cryptoKit.AESDecryptCFB([]byte(value), []byte("1234567891234567"), 16-len(value), []byte("1231231231231231"))
if err != nil {
fmt.Println(err)
Expand Down
4 changes: 2 additions & 2 deletions core/arrayKit/sliceKit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func Compare(a, b []string) bool {

// SliceDiff 切片差集
/*
* @param originalSlice 原始切片
* @param originalSlice 待比较的切片
* @param compareSlice 比较切片
* @return added, deleted 新增的数据, 删除的数据
* @return added, deleted 新增的数据[基于待比较切片而言], 删除的数据[基于待比较切片而言]
*/
func SliceDiff(originalSlice, compareSlice []string) (added, deleted []string) {
coreMap := make(map[string]bool)
Expand Down
22 changes: 6 additions & 16 deletions core/cryptoKit/aesKit.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,10 @@ func AESEncryptCFB(plainText []byte, key []byte, padding *int, iv ...[]byte) ([]
if err != nil {
return nil, err
}
blockSize := block.BlockSize()
plainText, *padding = ZeroPadding(plainText, blockSize)
ivValue := ([]byte)(nil)
if len(iv) > 0 {
ivValue = iv[0]
} else {
return nil, errors.New("Iv must exist")
if len(iv) == 0 {
return nil, errors.New("IV must exist")
}
ivValue := iv[0]
stream := cipher.NewCFBEncrypter(block, ivValue)
cipherText := make([]byte, len(plainText))
stream.XORKeyStream(cipherText, plainText)
Expand All @@ -142,19 +138,13 @@ func AESDecryptCFB(cipherText []byte, key []byte, unPadding int, iv ...[]byte) (
if err != nil {
return nil, err
}
if len(cipherText) < aes.BlockSize {
return nil, errors.New("cipherText too short")
}
ivValue := ([]byte)(nil)
if len(iv) > 0 {
ivValue = iv[0]
} else {
return nil, errors.New("Iv must exist")
if len(iv) == 0 {
return nil, errors.New("IV must exist")
}
ivValue := iv[0]
stream := cipher.NewCFBDecrypter(block, ivValue)
plainText := make([]byte, len(cipherText))
stream.XORKeyStream(plainText, cipherText)
plainText = ZeroUnPadding(plainText, unPadding)
return plainText, nil
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cryptoKit
package urlKit

import (
"github.com/xingcxb/goKit/core/strKit"
Expand Down
6 changes: 6 additions & 0 deletions core/osKit/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package osKit

const (
// CmdOsSyncLocalTime 同步本地时间命令
CmdOsSyncLocalTime = "nohup ntpdate -u ntp1.aliyun.com >/dev/null 2>&1 &"
)
10 changes: 10 additions & 0 deletions core/osKit/osKit.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/shirou/gopsutil/v4/disk"
"github.com/shirou/gopsutil/v4/host"
"github.com/xingcxb/goKit/core/dateKit"
"github.com/xingcxb/goKit/core/runTimeKit"
"net"
)

Expand Down Expand Up @@ -105,3 +106,12 @@ func GetMacAddress() string {
}
return ""
}

// SyncLocalTime 同步本地时间
func SyncLocalTime() (bool, error) {
_, err := runTimeKit.ExecuteCmd(CmdOsSyncLocalTime)
if err != nil {
return false, err
}
return true, nil
}
15 changes: 15 additions & 0 deletions core/runTimeKit/cmdKit.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package runTimeKit

import (
"fmt"
"github.com/xingcxb/goKit/core/dateKit"
"os/exec"
"time"
)

// ExecuteCmd 执行shell命令
Expand All @@ -16,3 +19,15 @@ func ExecuteCmd(shell string, parameter ...string) (string, error) {
output, err := cmd.CombinedOutput()
return string(output), err
}

// DelayExecCmd 延迟执行命令
/*
* @param nextSecond 下次执行秒数
* @param command 执行的命令
*/
func DelayExecCmd(nextSecond int, command string) (string, error) {
timeAfter, _ := dateKit.OffsetSecond(time.Now(), nextSecond)
atCmd := fmt.Sprintf("echo '%s' | at %s", command, timeAfter)
output, err := ExecuteCmd(atCmd)
return string(output), err
}
42 changes: 42 additions & 0 deletions core/strKit/strKit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package strKit
import (
"fmt"
"github.com/xingcxb/goKit/core/arrayKit"
"github.com/xingcxb/goKit/core/cryptoKit"
"github.com/xingcxb/goKit/core/regKit"
"math"
"strconv"
Expand Down Expand Up @@ -328,3 +329,44 @@ func AsciiToStr(str, separator string) string {
}
return newStr
}

// AnalyzeProxyAuth 解析代理认证的数据
/*
* @param rawUP string 原始的代理认证数据
* @return u 用户名;p 密码
*/
func AnalyzeProxyAuth(rawUP string) (u, p string) {
if rawUP == "" {
return
}
c, err := cryptoKit.Base64Decode(strings.TrimPrefix(rawUP, "Basic "))
if err != nil {
return
}
cs := string(c)
s := strings.IndexByte(cs, ':')
if s < 0 {
return
}

return cs[:s], cs[s+1:]
}

// AnalyzeHeader 转换请求头为map
/*
* @param s 请求头
* @return map[string]string
*/
func AnalyzeHeader(s string) map[string]string {
headers := make(map[string]string)
headerArray := strings.Split(s, "\r\n")
for _, header := range headerArray {
splits := strings.Split(header, ":")
if len(splits) > 1 {
headers[splits[0]] = strings.TrimSpace(splits[1])
} else {
headers[splits[0]] = ""
}
}
return headers
}

0 comments on commit 5e8fec9

Please sign in to comment.