-
Notifications
You must be signed in to change notification settings - Fork 21
/
tools.go
143 lines (129 loc) · 3.22 KB
/
tools.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package tool
import (
"crypto/md5"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcutil/base58"
"golang.org/x/crypto/ripemd160"
"io"
"log"
"obd/config"
"os"
"regexp"
"strconv"
"strings"
)
func CheckIsString(str *string) bool {
if str == nil {
return false
}
*str = strings.Trim(*str, " ")
if len(*str) == 0 {
return false
}
return true
}
func VerifyEmailFormat(email string) bool {
isString := CheckIsString(&email)
if isString == false {
return false
}
pattern := `\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*` //匹配电子邮箱
reg := regexp.MustCompile(pattern)
return reg.MatchString(email)
}
func SignMsgWithSha256(msg []byte) string {
hash := sha256.New()
hash.Write(msg)
return fmt.Sprintf("%x", hash.Sum(nil))
}
func SignMsgWithBase58(msg []byte) string {
hash := base58.Encode(msg)
return fmt.Sprintf("%x", hash)
}
func SignMsgWithRipemd160(msg []byte) string {
hash := ripemd160.New()
hash.Write(msg)
return fmt.Sprintf("%x", hash.Sum(nil))
}
func SignMsgWithMd5(msg []byte) string {
hash := md5.New()
hash.Write(msg)
return fmt.Sprintf("%x", hash.Sum(nil))
}
func GetAddressFromPubKey(pubKey string) (address string, err error) {
serializedPubKey, err := hex.DecodeString(pubKey)
if err != nil {
log.Println(err)
return "", errors.New("invalid pubkey")
}
// test TestNet3Params
// main MainNetParams
var net *chaincfg.Params
if config.ChainNode_Type == "test" {
net = &chaincfg.TestNet3Params
} else {
net = &chaincfg.MainNetParams
}
netAddr, err := btcutil.NewAddressPubKey(serializedPubKey, net)
if err != nil {
log.Println(err)
return "", errors.New("invalid pubkey")
}
netAddr.SetFormat(btcutil.PKFCompressed)
address = netAddr.EncodeAddress()
log.Println("BitCoin Address:", address)
return address, nil
}
func GetPubKeyFromWifAndCheck(privKeyHex string, pubKey string) (pubKeyFromWif string, err error) {
if CheckIsString(&privKeyHex) == false {
return "", errors.New("wrong private key")
}
if CheckIsString(&pubKey) == false {
return "", errors.New("wrong pubKey")
}
wif, err := btcutil.DecodeWIF(privKeyHex)
if err != nil {
return "", errors.New("wrong private key")
}
pubKeyFromWif = hex.EncodeToString(wif.PrivKey.PubKey().SerializeCompressed())
if pubKeyFromWif != pubKey {
return "", errors.New("private key and pubkey are not parent relationship")
}
return pubKeyFromWif, nil
}
func RandBytes(size int) (string, error) {
arr := make([]byte, size)
if _, err := io.ReadFull(rand.Reader, arr); err != nil {
return "", err
}
log.Println(arr)
return base64.StdEncoding.EncodeToString(arr), nil
}
// 判断文件夹是否存在,如果不存在,则创建
func PathExistsAndCreate(path string) error {
_, err := os.Stat(path)
if os.IsNotExist(err) {
err := os.Mkdir(path, os.ModePerm)
if err != nil {
log.Println(err)
return err
}
} else {
return nil
}
return errors.New("fail to create")
}
func FloatToString(input_num float64, prec int) string {
return strconv.FormatFloat(input_num, 'f', prec, 64)
}
func CheckPsw(psw string) (flag bool) {
reg := regexp.MustCompile("^[a-zA-Z0-9]{6,32}$")
return reg.MatchString(psw)
}