-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.go
48 lines (40 loc) · 845 Bytes
/
utils.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
package mixin
import (
"crypto/md5"
"crypto/rand"
"encoding/binary"
"io"
"strconv"
"strings"
"github.com/gofrs/uuid"
)
func newUUID() string {
return uuid.Must(uuid.NewV4()).String()
}
func UniqueConversationID(userID, recipientID string) string {
minID, maxID := userID, recipientID
if strings.Compare(userID, recipientID) > 0 {
maxID, minID = userID, recipientID
}
h := md5.New()
_, _ = io.WriteString(h, minID+maxID)
sum := h.Sum(nil)
sum[6] = (sum[6] & 0x0f) | 0x30
sum[8] = (sum[8] & 0x3f) | 0x80
return uuid.FromBytesOrNil(sum).String()
}
func RandomPin() string {
var b [8]byte
_, err := rand.Read(b[:])
if err != nil {
panic(err)
}
c := binary.LittleEndian.Uint64(b[:]) % 1000000
if c < 100000 {
c = 100000 + c
}
return strconv.FormatUint(c, 10)
}
func RandomTraceID() string {
return newUUID()
}