parent
2119548124
commit
bff09a4670
@ -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
|
||||
}
|
||||
@ -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!
|
||||
|
||||
}
|
||||
@ -0,0 +1,4 @@
|
||||
package crypto
|
||||
|
||||
type Crypto struct {
|
||||
}
|
||||
@ -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))
|
||||
}
|
||||
@ -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
|
||||
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
@ -0,0 +1,11 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestSha256Encode(t *testing.T) {
|
||||
fmt.Println(cryptoTool.Sha256Encode("hello world!"))
|
||||
//7509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
package datime
|
||||
@ -0,0 +1 @@
|
||||
package datime
|
||||
@ -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
|
||||
)
|
||||
|
||||
@ -0,0 +1,4 @@
|
||||
package rand
|
||||
|
||||
type Rand struct {
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
@ -0,0 +1,13 @@
|
||||
package rand
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var randTool Rand
|
||||
|
||||
func TestRand_GetPassword(t *testing.T) {
|
||||
fmt.Println(randTool.GetPassword(32))
|
||||
|
||||
}
|
||||
@ -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))
|
||||
}
|
||||
Loading…
Reference in new issue