-
Notifications
You must be signed in to change notification settings - Fork 2
/
hash.go
71 lines (58 loc) · 1.15 KB
/
hash.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
package ds
import (
"encoding/binary"
"strconv"
"github.com/google/uuid"
)
const DefaultHash string = `0`
func IsDefaultHash(h string) bool {
return h == DefaultHash
}
func Int32ToByte(i int32) []byte {
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, uint32(i))
return buf
}
func ByteToInt32(b []byte) int32 {
if len(b) != 4 {
panic("illegal buf size")
}
r := binary.LittleEndian.Uint32(b)
return int32(r)
}
func Int64ToByte(i int64) []byte {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, uint64(i))
return buf
}
func Uint32ToByte(i uint32) []byte {
buf := make([]byte, 4)
binary.LittleEndian.PutUint32(buf, i)
return buf
}
func Uint64ToByte(i uint64) []byte {
buf := make([]byte, 8)
binary.LittleEndian.PutUint64(buf, i)
return buf
}
func BoolToByte(b bool) []byte {
if b {
return []byte{0x1}
}
return []byte{0x0}
}
func Int32ToStr(i int32) string {
return strconv.FormatInt(int64(i), 10)
}
func StrToInt32(s string) int32 {
i, err := strconv.ParseInt(s, 10, 32)
if err != nil {
return -1
}
return int32(i)
}
func UUIDToByte(s string) []byte {
id, _ := uuid.Parse(s)
buf, _ := id.MarshalBinary()
return buf
}