diff --git a/daes/go.mod b/daes/go.mod new file mode 100644 index 00000000..92ab1c76 --- /dev/null +++ b/daes/go.mod @@ -0,0 +1,3 @@ +module gopkg.in/dtapps/go-library.v2/daes + +go 1.16 diff --git a/daes/v20210726/aes.go b/daes/v20210726/aes.go new file mode 100644 index 00000000..e4bd387a --- /dev/null +++ b/daes/v20210726/aes.go @@ -0,0 +1,57 @@ +package v20210726 + +import ( + "bytes" + "crypto/aes" + "crypto/cipher" + "encoding/base64" +) + +// Encrypt 加密 aes_128_cbc +func Encrypt(encryptStr string, key []byte, iv string) (string, error) { + encryptBytes := []byte(encryptStr) + block, err := aes.NewCipher(key) + if err != nil { + return "", err + } + + blockSize := block.BlockSize() + encryptBytes = pkcs5Padding(encryptBytes, blockSize) + + blockMode := cipher.NewCBCEncrypter(block, []byte(iv)) + encrypted := make([]byte, len(encryptBytes)) + blockMode.CryptBlocks(encrypted, encryptBytes) + return base64.URLEncoding.EncodeToString(encrypted), nil +} + +// Decrypt 解密 +func Decrypt(decryptStr string, key []byte, iv string) (string, error) { + decryptBytes, err := base64.URLEncoding.DecodeString(decryptStr) + if err != nil { + return "", err + } + + block, err := aes.NewCipher(key) + if err != nil { + return "", err + } + + blockMode := cipher.NewCBCDecrypter(block, []byte(iv)) + decrypted := make([]byte, len(decryptBytes)) + + blockMode.CryptBlocks(decrypted, decryptBytes) + decrypted = pkcs5UnPadding(decrypted) + return string(decrypted), nil +} + +func pkcs5Padding(cipherText []byte, blockSize int) []byte { + padding := blockSize - len(cipherText)%blockSize + padText := bytes.Repeat([]byte{byte(padding)}, padding) + return append(cipherText, padText...) +} + +func pkcs5UnPadding(decrypted []byte) []byte { + length := len(decrypted) + unPadding := int(decrypted[length-1]) + return decrypted[:(length - unPadding)] +} diff --git a/dtime/go.mod b/dtime/go.mod new file mode 100644 index 00000000..a52e53c1 --- /dev/null +++ b/dtime/go.mod @@ -0,0 +1,3 @@ +module gopkg.in/dtapps/go-library.v2/dtime + +go 1.16 diff --git a/dtime/v20210726/time.go b/dtime/v20210726/time.go new file mode 100644 index 00000000..62c327a4 --- /dev/null +++ b/dtime/v20210726/time.go @@ -0,0 +1,42 @@ +package v20210726 + +import "time" + +const ( + RFC822Format = "Mon, 02 Jan 2006 15:04:05 MST" + ISO8601Format = "2006-01-02T15:04:05Z" +) + +func NowUTCSeconds() int64 { return time.Now().UTC().Unix() } + +func NowUTCNanoSeconds() int64 { return time.Now().UTC().UnixNano() } + +// GetCurrentDate 获取当前的时间 - 字符串 +func GetCurrentDate() string { + return time.Now().Format("2006/01/02 15:04:05") +} + +// GetCurrentUnix 获取当前的时间 - Unix时间戳 +func GetCurrentUnix() int64 { + return time.Now().Unix() +} + +// GetCurrentMilliUnix 获取当前的时间 - 毫秒级时间戳 +func GetCurrentMilliUnix() int64 { + return time.Now().UnixNano() / 1000000 +} + +// GetCurrentNanoUnix 获取当前的时间 - 纳秒级时间戳 +func GetCurrentNanoUnix() int64 { + return time.Now().UnixNano() +} + +// GetCurrentWjDate 获取当前的时间 - 字符串 - 没有间隔 +func GetCurrentWjDate() string { + return time.Now().Format("20060102") +} + +func FormatISO8601Date(timestampSecond int64) string { + tm := time.Unix(timestampSecond, 0).UTC() + return tm.Format(ISO8601Format) +} diff --git a/dtime/v20210726/time_example.go b/dtime/v20210726/time_example.go new file mode 100644 index 00000000..57b0dc94 --- /dev/null +++ b/dtime/v20210726/time_example.go @@ -0,0 +1,11 @@ +package v20210726 + +import "fmt" + +func main() { + fmt.Println(GetCurrentDate()) + fmt.Println(GetCurrentUnix()) + fmt.Println(GetCurrentMilliUnix()) + fmt.Println(GetCurrentNanoUnix()) + fmt.Println(GetCurrentWjDate()) +} diff --git a/dtime/v20210726/time_test.go b/dtime/v20210726/time_test.go new file mode 100644 index 00000000..f669352e --- /dev/null +++ b/dtime/v20210726/time_test.go @@ -0,0 +1,14 @@ +package v20210726 + +import ( + "fmt" + "testing" +) + +func TestName(t *testing.T) { + fmt.Println(GetCurrentDate()) + fmt.Println(GetCurrentUnix()) + fmt.Println(GetCurrentMilliUnix()) + fmt.Println(GetCurrentNanoUnix()) + fmt.Println(GetCurrentWjDate()) +} diff --git a/request/request.go b/request/request.go index c449413e..7b930d49 100644 --- a/request/request.go +++ b/request/request.go @@ -6,6 +6,7 @@ import ( "net" "net/http" "strings" + "time" ) // ClientIp 尽最大努力实现获取客户端 IP 的算法。 @@ -50,6 +51,11 @@ func GetUserAgent() string { return userAgentList[rand.Intn(len(userAgentList))] } +func GetRandomUserAgent() string { + r := rand.New(rand.NewSource(time.Now().UnixNano())) + return userAgentList[r.Intn(len(userAgentList))] +} + var userAgentList = []string{ "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.77 Safari/537.36", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML like Gecko) Chrome/44.0.2403.155 Safari/537.36", diff --git a/time/time_example.go b/time/time_example.go new file mode 100644 index 00000000..925fcd5f --- /dev/null +++ b/time/time_example.go @@ -0,0 +1,11 @@ +package time + +import "fmt" + +func main() { + fmt.Println(GetCurrentDate()) + fmt.Println(GetCurrentUnix()) + fmt.Println(GetCurrentMilliUnix()) + fmt.Println(GetCurrentNanoUnix()) + fmt.Println(GetCurrentWjDate()) +} diff --git a/time/time_test.go b/time/time_test.go index 66995b1c..52e9d146 100644 --- a/time/time_test.go +++ b/time/time_test.go @@ -1,14 +1,14 @@ package time import ( - "log" + "fmt" "testing" ) func TestName(t *testing.T) { - log.Println(GetCurrentDate()) - log.Println(GetCurrentUnix()) - log.Println(GetCurrentMilliUnix()) - log.Println(GetCurrentNanoUnix()) - log.Println(GetCurrentWjDate()) + fmt.Println(GetCurrentDate()) + fmt.Println(GetCurrentUnix()) + fmt.Println(GetCurrentMilliUnix()) + fmt.Println(GetCurrentNanoUnix()) + fmt.Println(GetCurrentWjDate()) }