commit 4299eb69498941cb6eb84ffcf99ce0167a3d950b Author: 李光春 Date: Tue May 10 21:23:48 2022 +0800 init diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..511f0fc --- /dev/null +++ b/.drone.yml @@ -0,0 +1,11 @@ +kind: pipeline +type: docker +name: clone + +steps: + - name: test-golang + image: golang:1.18 + commands: + - go env -w GO111MODULE=on + - go env -w GOPROXY=https://goproxy.cn,direct + - go test -v ./... \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2b895f4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +.env +.git +.svn +.idea +.vscode +*.log +gitmod.sh \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..a0d0e5b --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2018 茂名聚合科技有限公司 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..07d5974 --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +

+Golang Md5 +

+ +📦 Golang 字符串组件 + +[comment]: <> (go) +[![godoc](https://pkg.go.dev/badge/go.dtapp.net/gomd5?status.svg)](https://pkg.go.dev/go.dtapp.net/gomd5) +[![goproxy.cn](https://goproxy.cn/stats/go.dtapp.net/gomd5/badges/download-count.svg)](https://goproxy.cn/stats/go.dtapp.net/gomd5) +[![goreportcard.com](https://goreportcard.com/badge/go.dtapp.net/gomd5)](https://goreportcard.com/report/go.dtapp.net/gomd5) +[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/go.dtapp.net/gomd5) + +#### 安装使用 + +```go +go get -v -u go.dtapp.net/gomd5 +``` + +#### 导入 + +```go +import ( + "go.dtapp.net/gomd5" +) +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..105f87d --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module go.dtapp.net/gomd5 + +go 1.18 diff --git a/gomd5.go b/gomd5.go new file mode 100644 index 0000000..2a9b9e8 --- /dev/null +++ b/gomd5.go @@ -0,0 +1,54 @@ +package gomd5 + +import ( + "crypto/md5" + "encoding/hex" + "fmt" + "io" + "strings" +) + +func Php(str string) string { + h := md5.New() + io.WriteString(h, str) + return fmt.Sprintf("%x", h.Sum(nil)) +} + +func Md5(str string) string { + s := md5.New() + s.Write([]byte(str)) + return hex.EncodeToString(s.Sum(nil)) +} + +// ToUpper md5加密后转大写 +func ToUpper(str string) string { + h := md5.New() + h.Write([]byte(str)) + cipherStr := h.Sum(nil) + return strings.ToUpper(hex.EncodeToString(cipherStr)) +} + +// ToLower md5加密后转小写 +func ToLower(str string) string { + h := md5.New() + h.Write([]byte(str)) + cipherStr := h.Sum(nil) + return strings.ToLower(hex.EncodeToString(cipherStr)) +} + +// GetMD5Encode 返回一个32位md5加密后的字符串 +func GetMD5Encode(data string) string { + h := md5.New() + h.Write([]byte(data)) + return hex.EncodeToString(h.Sum(nil)) +} + +// Get16MD5Encode 返回一个16位md5加密后的字符串 +func Get16MD5Encode(data string) string { + return GetMD5Encode(data)[8:24] +} + +// Get16MD5EncodeToUpper 返回一个16位md5加密后的大写字符串 +func Get16MD5EncodeToUpper(data string) string { + return strings.ToUpper(Get16MD5Encode(data)) +} diff --git a/gomd5_test.go b/gomd5_test.go new file mode 100644 index 0000000..8ec2b35 --- /dev/null +++ b/gomd5_test.go @@ -0,0 +1,22 @@ +package gomd5 + +import ( + "testing" +) + +func TestMd5(t *testing.T) { + t.Logf(GetMD5Encode("测试")) + t.Logf(ToUpper(GetMD5Encode("测试"))) + t.Logf(ToUpper("测试")) +} + +func TestGet16MD5EncodeToUpper(t *testing.T) { + t.Logf("A" + Get16MD5EncodeToUpper("10000,admin")) + t.Logf("A" + Get16MD5EncodeToUpper("10001,agent")) + t.Logf("A" + Get16MD5EncodeToUpper("10007,agent")) + t.Logf("A" + Get16MD5EncodeToUpper("10008,agent")) + t.Logf("A" + Get16MD5EncodeToUpper("10010,agent")) + t.Logf("M" + Get16MD5EncodeToUpper("10011,merchant")) + t.Logf("M" + Get16MD5EncodeToUpper("10015,merchant")) + t.Logf("U" + Get16MD5EncodeToUpper("AE4E9C88A45813D4D,oojLm5Xv9iJ36WZGgJHooHcNyFGs,mini_user")) +} diff --git a/gomod.sh b/gomod.sh new file mode 100644 index 0000000..c03f9f0 --- /dev/null +++ b/gomod.sh @@ -0,0 +1,3 @@ +go get -u && go mod tidy +go get -u all +go mod vendor \ No newline at end of file