forked from iost-official/go-iost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrypto.go
57 lines (48 loc) · 968 Bytes
/
crypto.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
package common
import (
"encoding/hex"
"encoding/binary"
"hash/crc32"
"github.com/btcsuite/btcutil/base58"
"github.com/iost-official/go-iost/ilog"
"golang.org/x/crypto/sha3"
)
// Sha3 ...
func Sha3(raw []byte) []byte {
defer func() {
if e := recover(); e != nil {
ilog.Warnf("sha3 panic. err=%v", e)
}
}()
data := sha3.Sum256(raw)
return data[:]
}
// Base58Encode ...
func Base58Encode(raw []byte) string {
return base58.Encode(raw)
}
// Base58Decode ...
func Base58Decode(s string) []byte {
return base58.Decode(s)
}
// Parity ...
func Parity(bit []byte) []byte {
crc32q := crc32.MakeTable(crc32.Koopman)
crc := crc32.Checksum(bit, crc32q)
bs := make([]byte, 4)
binary.LittleEndian.PutUint32(bs, crc)
return bs
}
// ToHex ...
func ToHex(data []byte) string {
return hex.EncodeToString(data)
}
// ParseHex ...
func ParseHex(s string) []byte {
d, err := hex.DecodeString(s)
if err != nil {
println(err)
return nil
}
return d
}