File tree 13 files changed +144
-112
lines changed
13 files changed +144
-112
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1
- package utils
1
+ package gencode
2
2
3
3
import (
4
4
"encoding/base64"
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- package utils
1
+ package gencrypt
2
2
3
3
import (
4
4
"crypto/hmac"
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- package utils
1
+ package gencrypt
2
2
3
3
import (
4
4
"crypto/rand"
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 1
- package utils
1
+ package grand
2
2
3
3
import (
4
4
"crypto/rand"
Original file line number Diff line number Diff line change 1
- package utils
1
+ package grand
2
2
3
3
import (
4
4
"testing"
@@ -9,11 +9,23 @@ func TestIntn(t *testing.T) {
9
9
t .Log (s )
10
10
}
11
11
12
+ func BenchmarkIntn (b * testing.B ) {
13
+ for i := 0 ; i < b .N ; i ++ {
14
+ Intn (500 )
15
+ }
16
+ }
17
+
12
18
func TestRandBytes (t * testing.T ) {
13
19
b := RandBytes (10 )
14
20
t .Log (b )
15
21
}
16
22
23
+ func BenchmarkRandBytes (b * testing.B ) {
24
+ for i := 0 ; i < b .N ; i ++ {
25
+ RandBytes (10 )
26
+ }
27
+ }
28
+
17
29
func TestRandRangeIntN (t * testing.T ) {
18
30
n := RandRangeIntN (1 , 4 )
19
31
t .Log (n )
@@ -29,6 +41,12 @@ func TestRandStr(t *testing.T) {
29
41
t .Log (randStr )
30
42
}
31
43
44
+ func BenchmarkRandStr (b * testing.B ) {
45
+ for i := 0 ; i < b .N ; i ++ {
46
+ RandStr ("01234你耗阿萨德绿卡结束勒肯定就看" , 5 )
47
+ }
48
+ }
49
+
32
50
func TestRandDigits (t * testing.T ) {
33
51
s := RandDigits (5 )
34
52
t .Log (s )
Original file line number Diff line number Diff line change
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
+ }
Load Diff This file was deleted.
Load Diff This file was deleted.
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments