forked from dnanexus/dxfuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nonce.go
57 lines (48 loc) · 1.3 KB
/
nonce.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 dxfuse
// based on:
// https://www.calhoun.io/creating-random-strings-in-go, and
// https://github.com/dnanexus/dxWDL/blob/master/src/main/java/com/dnanexus/Nonce.java
//
// Create a nonce according to the dnanexus rules (https://documentation.dnanexus.com/developer/api/nonces)
import (
"fmt"
"math/rand"
"sync/atomic"
"time"
)
type Nonce struct {
seed *rand.Rand
counter uint64
}
const (
charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
nonceLen = 32
maxLen = 128
)
func NewNonce() *Nonce {
nano := time.Now().UnixNano()
source := rand.NewSource(nano)
return &Nonce{
seed : rand.New(source),
counter : 0,
}
}
// Create a random nonce, no longer than 128 bytes
//
func (n *Nonce) String() string {
b := make([]byte, nonceLen)
// create a string that contains only readable characters. It can only
// include characters that can be passed in http requests.
for i := range b {
b[i] = charset[n.seed.Intn(len(charset))]
}
// add a counter, to better distinguish requests
cnt := atomic.AddUint64(&n.counter, 1)
// add the time, for improved randomness
retval := fmt.Sprintf("%s_%d_%d", string(b), time.Now().UnixNano(), cnt)
if len(retval) > maxLen {
// Make sure we don't go over the dnanexus limit
return retval[0 : maxLen-1]
}
return retval
}