This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 279
/
random.go
110 lines (97 loc) · 2.58 KB
/
random.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package types
import (
"fmt"
)
// A Rand is a source of random int64 numbers
type Rand interface {
Int63() int64
}
func randRead(r Rand, b []byte) (n int, err error) {
// Adapted from go stdlib https://goo.gl/i3vwnE
pos := 7
val := r.Int63()
for n := range b {
if pos == 0 {
val = r.Int63()
pos = 7
}
b[n] = byte(val)
val >>= 8
pos--
}
return
}
// NewPopulatedDevAddr returns random DevAddr
func NewPopulatedDevAddr(r Rand) (devAddr *DevAddr) {
devAddr = &DevAddr{}
if _, err := randRead(r, devAddr[:]); err != nil {
panic(fmt.Errorf("types.NewPopulatedDevAddr: %s", err))
}
return
}
// NewPopulatedAppEUI returns random AppEUI
func NewPopulatedAppEUI(r Rand) (appEUI *AppEUI) {
appEUI = &AppEUI{}
if _, err := randRead(r, appEUI[:]); err != nil {
panic(fmt.Errorf("types.NewPopulatedAppEUI: %s", err))
}
return
}
// NewPopulatedDevEUI returns random DevEUI
func NewPopulatedDevEUI(r Rand) (devEUI *DevEUI) {
devEUI = &DevEUI{}
if _, err := randRead(r, devEUI[:]); err != nil {
panic(fmt.Errorf("types.NewPopulatedDevEUI: %s", err))
}
return
}
// NewPopulatedAppKey returns random AppKey
func NewPopulatedAppKey(r Rand) (key *AppKey) {
key = &AppKey{}
if _, err := randRead(r, key[:]); err != nil {
panic(fmt.Errorf("types.NewPopulatedAppKey: %s", err))
}
return
}
// NewPopulatedAppSKey returns random AppSKey
func NewPopulatedAppSKey(r Rand) (key *AppSKey) {
key = &AppSKey{}
if _, err := randRead(r, key[:]); err != nil {
panic(fmt.Errorf("types.NewPopulatedAppSKey: %s", err))
}
return
}
// NewPopulatedNwkSKey returns random NwkSKey
func NewPopulatedNwkSKey(r Rand) (key *NwkSKey) {
key = &NwkSKey{}
if _, err := randRead(r, key[:]); err != nil {
panic(fmt.Errorf("types.NewPopulatedNwkSKey: %s", err))
}
return
}
// NewPopulatedDevNonce returns random DevNonce
func NewPopulatedDevNonce(r Rand) (nonce *DevNonce) {
nonce = &DevNonce{}
if _, err := randRead(r, nonce[:]); err != nil {
panic(fmt.Errorf("types.NewPopulatedDevNonce: %s", err))
}
return
}
// NewPopulatedAppNonce returns random AppNonce
func NewPopulatedAppNonce(r Rand) (nonce *AppNonce) {
nonce = &AppNonce{}
if _, err := randRead(r, nonce[:]); err != nil {
panic(fmt.Errorf("types.NewPopulatedAppNonce: %s", err))
}
return
}
// NewPopulatedNetID returns random NetID
func NewPopulatedNetID(r Rand) (id *NetID) {
id = &NetID{}
if _, err := randRead(r, id[:]); err != nil {
panic(fmt.Errorf("types.NewPopulatedNetID: %s", err))
}
return
}