-
Notifications
You must be signed in to change notification settings - Fork 0
/
UUIDGenServiceImpl.go
69 lines (56 loc) · 1.4 KB
/
UUIDGenServiceImpl.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
package implservice
import (
"bytes"
"crypto/sha256"
"strconv"
"github.com/bitwormhole/starter/markup"
"github.com/bitwormhole/starter/util"
"github.com/bitwormhole/wpm/server/data/dxo"
"github.com/bitwormhole/wpm/server/service"
)
// UUIDGenServiceImpl ...
type UUIDGenServiceImpl struct {
markup.Component `id:"UUIDGenService" initMethod:"Init"`
t0 util.Time
index int64
}
func (inst *UUIDGenServiceImpl) _Impl() service.UUIDGenService {
return inst
}
// Init ...
func (inst *UUIDGenServiceImpl) Init() error {
inst.t0 = util.Now()
inst.index = 0
return nil
}
// GenerateUUID ...
func (inst *UUIDGenServiceImpl) GenerateUUID(seed string) dxo.UUID {
inst.index++
const sep = '\n'
t0 := inst.t0
now := util.Now()
index := inst.index
builder := bytes.Buffer{}
builder.WriteString(seed)
builder.WriteRune(sep)
builder.WriteString(inst.time2str(t0))
builder.WriteRune(sep)
builder.WriteString(inst.time2str(now))
builder.WriteRune(sep)
builder.WriteString(inst.int2str(index))
sum := inst.sum(builder.Bytes())
return dxo.UUID(util.HexFromBytes(sum))
}
func (inst *UUIDGenServiceImpl) sum(b []byte) []byte {
if b == nil {
b = make([]byte, 0)
}
sum := sha256.Sum256(b)
return sum[:]
}
func (inst *UUIDGenServiceImpl) int2str(n int64) string {
return strconv.FormatInt(n, 10)
}
func (inst *UUIDGenServiceImpl) time2str(t util.Time) string {
return strconv.FormatInt(t.Int64(), 10)
}