Skip to content

Commit fe78708

Browse files
committed
修改代码
1 parent 1bbeb9c commit fe78708

File tree

13 files changed

+144
-112
lines changed

13 files changed

+144
-112
lines changed

utils/base64_test.go

-8
This file was deleted.

utils/base64.go utils/gencode/base64.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package gencode
22

33
import (
44
"encoding/base64"

utils/gencode/base64_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package gencode
2+
3+
import "testing"
4+
5+
func TestB64EncodeString(t *testing.T) {
6+
s := B64EncodeString("123456")
7+
t.Log(s)
8+
}
9+
10+
func BenchmarkB64EncodeString(b *testing.B) {
11+
for i := 0; i < b.N; i++ {
12+
B64EncodeString("123456")
13+
}
14+
}

utils/hmac.go utils/gencrypt/hmac.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package gencrypt
22

33
import (
44
"crypto/hmac"

utils/gencrypt/md5.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package gencrypt
2+
3+
import (
4+
"crypto/md5"
5+
"encoding/hex"
6+
"io/ioutil"
7+
)
8+
9+
func Md5Bytes(data []byte) (res string, err error) {
10+
h := md5.New()
11+
if _, err = h.Write(data); err != nil {
12+
return "", err
13+
}
14+
return hex.EncodeToString(h.Sum(nil)), nil
15+
}
16+
17+
func Md5String(data string) (res string, err error) {
18+
return Md5Bytes([]byte(data))
19+
}
20+
21+
func Md5File(path string) (res string, err error) {
22+
var fileBytes []byte
23+
fileBytes, err = ioutil.ReadFile(path)
24+
if err != nil {
25+
return "", err
26+
}
27+
return Md5Bytes(fileBytes)
28+
}

utils/rsa.go utils/gencrypt/rsa.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package gencrypt
22

33
import (
44
"crypto/rand"

utils/gencrypt/sha.go

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package gencrypt
2+
3+
import (
4+
"crypto/sha1"
5+
"crypto/sha256"
6+
"crypto/sha512"
7+
"encoding/hex"
8+
"io/ioutil"
9+
)
10+
11+
func Sha1File(path string) (encrypt string, err error) {
12+
var fileBytes []byte
13+
fileBytes, err = ioutil.ReadFile(path)
14+
if err != nil {
15+
return "", err
16+
}
17+
h := sha1.New()
18+
h.Write(fileBytes)
19+
encrypt = hex.EncodeToString(h.Sum(nil))
20+
return
21+
}
22+
23+
func Sha1Bytes(data []byte) string {
24+
h := sha1.New()
25+
h.Write(data)
26+
return hex.EncodeToString(h.Sum(nil))
27+
}
28+
29+
func Sha1String(data string) (encrypt string) {
30+
return Sha1Bytes([]byte(data))
31+
}
32+
33+
func Sha256String(data string) string {
34+
return Sha256Bytes([]byte(data))
35+
}
36+
37+
func Sha256Bytes(data []byte) string {
38+
return hex.EncodeToString(Sha256(data))
39+
}
40+
41+
func Sha256(data []byte) []byte {
42+
digest := sha256.New()
43+
digest.Write(data)
44+
return digest.Sum(nil)
45+
}
46+
47+
func Sha512String(data string) string {
48+
return Sha512Bytes([]byte(data))
49+
}
50+
51+
func Sha512Bytes(data []byte) string {
52+
return hex.EncodeToString(Sha512(data))
53+
}
54+
55+
func Sha512(data []byte) []byte {
56+
digest := sha512.New()
57+
digest.Write(data)
58+
return digest.Sum(nil)
59+
}

utils/rand.go utils/grand/rand.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package grand
22

33
import (
44
"crypto/rand"

utils/rand_test.go utils/grand/rand_test.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package utils
1+
package grand
22

33
import (
44
"testing"
@@ -9,11 +9,23 @@ func TestIntn(t *testing.T) {
99
t.Log(s)
1010
}
1111

12+
func BenchmarkIntn(b *testing.B) {
13+
for i := 0; i < b.N; i++ {
14+
Intn(500)
15+
}
16+
}
17+
1218
func TestRandBytes(t *testing.T) {
1319
b := RandBytes(10)
1420
t.Log(b)
1521
}
1622

23+
func BenchmarkRandBytes(b *testing.B) {
24+
for i := 0; i < b.N; i++ {
25+
RandBytes(10)
26+
}
27+
}
28+
1729
func TestRandRangeIntN(t *testing.T) {
1830
n := RandRangeIntN(1, 4)
1931
t.Log(n)
@@ -29,6 +41,12 @@ func TestRandStr(t *testing.T) {
2941
t.Log(randStr)
3042
}
3143

44+
func BenchmarkRandStr(b *testing.B) {
45+
for i := 0; i < b.N; i++ {
46+
RandStr("01234你耗阿萨德绿卡结束勒肯定就看", 5)
47+
}
48+
}
49+
3250
func TestRandDigits(t *testing.T) {
3351
s := RandDigits(5)
3452
t.Log(s)

utils/gstr/str.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package gstr
2+
3+
const (
4+
NotFoundIndex = -1
5+
)
6+
7+
// SearchArray 查询子串在字符串列表里的索引,不存在则返回-1
8+
func SearchArray(strList []string, s string) int {
9+
for i, v := range strList {
10+
if s == v {
11+
return i
12+
}
13+
}
14+
return NotFoundIndex
15+
}
16+
17+
// InArray 查询子串是否在字符串列表里
18+
func InArray(a []string, s string) bool {
19+
return SearchArray(a, s) != NotFoundIndex
20+
}

utils/md5.go

-34
This file was deleted.

utils/sha1.go

-36
This file was deleted.

utils/str.go

-29
This file was deleted.

0 commit comments

Comments
 (0)