1
This commit is contained in:
17
pkg/cryptohelper/md5helper/md5helper.go
Normal file
17
pkg/cryptohelper/md5helper/md5helper.go
Normal file
@ -0,0 +1,17 @@
|
||||
package md5helper
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
// MD5 md5加密
|
||||
func MD5(input string) string {
|
||||
cc := md5.Sum([]byte(input))
|
||||
return hex.EncodeToString(cc[:])
|
||||
}
|
||||
|
||||
// MD52 md52次
|
||||
func MD52(input string) string {
|
||||
return MD5(MD5(input))
|
||||
}
|
||||
55
pkg/cryptohelper/md5helper/md5helper_test.go
Normal file
55
pkg/cryptohelper/md5helper/md5helper_test.go
Normal file
@ -0,0 +1,55 @@
|
||||
package md5helper
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestMD52(t *testing.T) {
|
||||
got := MD52("Qq123456")
|
||||
fmt.Println(got)
|
||||
}
|
||||
|
||||
func TestMD5(t *testing.T) {
|
||||
got := []byte("Qq123456")
|
||||
s := md5.New()
|
||||
s.Write(got)
|
||||
fmt.Println(hex.EncodeToString(s.Sum(nil)))
|
||||
|
||||
cc := md5.Sum(got)
|
||||
fmt.Println(hex.EncodeToString(cc[:]))
|
||||
|
||||
fmt.Printf("%x\n", md5.Sum(got))
|
||||
|
||||
}
|
||||
|
||||
// go test -bench=_QE_ -benchmem -run=^$
|
||||
func Benchmark_QE_1(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
input := `Qq123456`
|
||||
s := md5.New()
|
||||
s.Write([]byte(input))
|
||||
hex.EncodeToString(s.Sum(nil))
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_QE_2(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
input := `Qq123456`
|
||||
cc := md5.Sum([]byte(input))
|
||||
hex.EncodeToString(cc[:])
|
||||
}
|
||||
}
|
||||
|
||||
func Benchmark_QE_3(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
input := `Qq123456`
|
||||
fmt.Sprintf("%x\n", md5.Sum([]byte(input)))
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark_QE_1-6 6354160 189.8 ns/op 48 B/op 2 allocs/op
|
||||
// Benchmark_QE_2-6 7352328 162.9 ns/op 32 B/op 1 allocs/op
|
||||
// Benchmark_QE_3-6 3007480 396.9 ns/op 80 B/op 3 allocs/op
|
||||
Reference in New Issue
Block a user