diff --git a/crypto/base64.go b/crypto/base64.go new file mode 100644 index 0000000..c9c5f63 --- /dev/null +++ b/crypto/base64.go @@ -0,0 +1,30 @@ +package crypto + +import ( + "encoding/base64" + "strings" +) + +// Base64Encode +func (c *Crypto) Base64Encode(str string) string { + byteStr := []byte(str) + return base64.StdEncoding.EncodeToString(byteStr) +} + +// Base64Decode +func (c *Crypto) Base64Decode(str string) string { + reader := strings.NewReader(str) + decoder := base64.NewDecoder(base64.RawStdEncoding, reader) + // 以流式解码 + buf := make([]byte, 1024) + // 保存解码后的数据 + dst := "" + for { + n, err := decoder.Read(buf) + dst += string(buf[:n]) + if n == 0 || err != nil { + break + } + } + return dst +} diff --git a/crypto/base64_test.go b/crypto/base64_test.go new file mode 100644 index 0000000..bbd29cd --- /dev/null +++ b/crypto/base64_test.go @@ -0,0 +1,14 @@ +package crypto + +import ( + "fmt" + "testing" +) + +func TestCrypto_Base64Decode(t *testing.T) { + fmt.Println(cryptoTool.Base64Encode("hello world!")) + //aGVsbG8gd29ybGQh + fmt.Println(cryptoTool.Base64Decode("aGVsbG8gd29ybGQh!")) + //hello world! + +} diff --git a/crypto/entry.go b/crypto/entry.go new file mode 100644 index 0000000..bf26c89 --- /dev/null +++ b/crypto/entry.go @@ -0,0 +1,4 @@ +package crypto + +type Crypto struct { +} diff --git a/crypto/md5.go b/crypto/md5.go new file mode 100644 index 0000000..7ff59e8 --- /dev/null +++ b/crypto/md5.go @@ -0,0 +1,19 @@ +package crypto + +import ( + "crypto/md5" + "encoding/hex" + "strings" +) + +// Md5Check md5 Check method +func (c *Crypto) Md5Check(content, encrypted string) bool { + return strings.EqualFold(c.Md5Encode(content), encrypted) +} + +// Md5Encode md5 Signature function +func (c *Crypto) Md5Encode(data string) string { + h := md5.New() + h.Write([]byte(data)) + return hex.EncodeToString(h.Sum(nil)) +} diff --git a/crypto/md5_test.go b/crypto/md5_test.go new file mode 100644 index 0000000..754e566 --- /dev/null +++ b/crypto/md5_test.go @@ -0,0 +1,16 @@ +package crypto + +import ( + "fmt" + "testing" +) + +var cryptoTool Crypto + +func TestMd5(tt *testing.T) { + fmt.Println(cryptoTool.Md5Encode("hello world!")) + //fc3ff98e8c6a0d3087d515c0473f8677 + fmt.Println(cryptoTool.Md5Check("hello world!", "fc3ff98e8c6a0d3087d515c0473f8677")) + //true + +} diff --git a/crypto/sha256.go b/crypto/sha256.go new file mode 100644 index 0000000..3286a44 --- /dev/null +++ b/crypto/sha256.go @@ -0,0 +1,14 @@ +package crypto + +import ( + "crypto/sha256" + "encoding/hex" +) + +// Sha256加密 +func (c *Crypto) Sha256Encode(src string) string { + m := sha256.New() + m.Write([]byte(src)) + res := hex.EncodeToString(m.Sum(nil)) + return res +} diff --git a/crypto/sha256_test.go b/crypto/sha256_test.go new file mode 100644 index 0000000..87d107a --- /dev/null +++ b/crypto/sha256_test.go @@ -0,0 +1,11 @@ +package crypto + +import ( + "fmt" + "testing" +) + +func TestSha256Encode(t *testing.T) { + fmt.Println(cryptoTool.Sha256Encode("hello world!")) + //7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9 +} diff --git a/datime/date.go b/datime/date.go new file mode 100644 index 0000000..9fe6f21 --- /dev/null +++ b/datime/date.go @@ -0,0 +1 @@ +package datime diff --git a/datime/date_test.go b/datime/date_test.go new file mode 100644 index 0000000..9fe6f21 --- /dev/null +++ b/datime/date_test.go @@ -0,0 +1 @@ +package datime diff --git a/gtool.go b/gtool.go index a5f08a9..25de909 100644 --- a/gtool.go +++ b/gtool.go @@ -1,8 +1,14 @@ package gtool -import "git.sre.ink/go/gtool/str" +import ( + "git.sre.ink/go/gtool/crypto" + "git.sre.ink/go/gtool/rand" + "git.sre.ink/go/gtool/str" +) var ( //string tools - Str str.Str + Str str.Str + Crypt crypto.Crypto + Rand rand.Rand ) diff --git a/rand/entry.go b/rand/entry.go new file mode 100644 index 0000000..435469d --- /dev/null +++ b/rand/entry.go @@ -0,0 +1,4 @@ +package rand + +type Rand struct { +} diff --git a/rand/password.go b/rand/password.go new file mode 100644 index 0000000..6708a81 --- /dev/null +++ b/rand/password.go @@ -0,0 +1,41 @@ +package rand + +import ( + "bytes" + "math/rand" + "time" +) + +func (rr *Rand) randomGetSomeChar(str string, l int) string { + bytes := []byte(str) + result := []byte{} + r := rand.New(rand.NewSource(time.Now().UnixNano())) + for i := 0; i < l; i++ { + result = append(result, bytes[r.Intn(len(bytes))]) + } + return string(result) +} + +func (r *Rand) GetPassword(len int) string { + base_num := "023456789" + base_char_low := "abcdefghijkmnpqrstuvwxyz" + base_char_high := "ABCDEFGHJKLMNOPQRSTUVWXYZ" + base_sign_spec := "_" + str := "023456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ_" + var stringBuilder bytes.Buffer + if len < 5 { + len = 5 + } + stringBuilder.WriteString(r.randomGetSomeChar(base_sign_spec, 1)) + stringBuilder.WriteString(r.randomGetSomeChar(base_num, 1)) + stringBuilder.WriteString(r.randomGetSomeChar(base_char_low, 1)) + stringBuilder.WriteString(r.randomGetSomeChar(base_char_high, 1)) + stringBuilder.WriteString(r.randomGetSomeChar(str, len-4)) + str_result := stringBuilder.String() + arr_result := []byte(str_result) + for i := 0; i < 4; i++ { + num := rand.Intn(len - 1) + arr_result[i], arr_result[num] = arr_result[num], arr_result[i] + } + return string(arr_result) +} diff --git a/rand/password_test.go b/rand/password_test.go new file mode 100644 index 0000000..e41b6e8 --- /dev/null +++ b/rand/password_test.go @@ -0,0 +1,13 @@ +package rand + +import ( + "fmt" + "testing" +) + +var randTool Rand + +func TestRand_GetPassword(t *testing.T) { + fmt.Println(randTool.GetPassword(32)) + +} diff --git a/rand/random.go b/rand/random.go new file mode 100644 index 0000000..0c5f03e --- /dev/null +++ b/rand/random.go @@ -0,0 +1,52 @@ +package rand + +import ( + "math/rand" + "strconv" + "time" +) + +// 包含大小写和数字 +func (r *Rand) RandomString(n int) string { + letters := []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789") + //var letters = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789") + result := make([]byte, n) + rand.Seed(time.Now().Unix()) + for i := range result { + result[i] = letters[rand.Intn(len(letters))] + } + + return string(result) + +} + +// 生成指定位数的数字字符串,第一位不为0 +func (r *Rand) RandomCode(n int) string { + numbers := []byte("0123456789") + numbers_no_zero := []byte("123456789") + result := make([]byte, n) + rand.Seed(time.Now().Unix()) + //code := fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000)) + for i := range result { + result[i] = numbers[rand.Intn(len(numbers))] + } + //阿里大于验证码短信首字母不能为0 + if result[0] == 0 { + { + result[0] = numbers_no_zero[rand.Intn(len(numbers_no_zero))] + } + } + return string(result) + +} + +// 6位随机验证码 +func (r *Rand) GetNum6() string { + //deltaNumber := exponent(10,6) + //baseNumber := exponent(10,7) + rand.Seed(time.Now().UnixNano()) + code := rand.Intn(899999) + 100000 + //code := rand.Intn(baseNumber-baseNumber-1) + deltaNumber + res := strconv.Itoa(code) //转字符串返回 + return res +} diff --git a/rand/random_test.go b/rand/random_test.go new file mode 100644 index 0000000..43c3a58 --- /dev/null +++ b/rand/random_test.go @@ -0,0 +1,21 @@ +package rand + +import ( + "fmt" + "testing" +) + +func TestGetNum6(tt *testing.T) { + for i := 0; i < 1000; i++ { + fmt.Println(randTool.GetNum6()) + } + +} +func TestRandomCode(tt *testing.T) { + fmt.Println(randTool.RandomCode(6)) + +} + +func TestRandomString(tt *testing.T) { + fmt.Println(randTool.RandomString(32)) +} diff --git a/readme.md b/readme.md index 3634033..36d540c 100644 --- a/readme.md +++ b/readme.md @@ -1,3 +1,8 @@ gtool ======= Golang tool set + +usage: +```bash +go get git.sre.ink/go/gtool +```