diff options
| -rw-r--r-- | token.go | 19 | ||||
| -rw-r--r-- | token_test.go | 30 |
2 files changed, 49 insertions, 0 deletions
diff --git a/token.go b/token.go new file mode 100644 index 0000000..bdabcc4 --- /dev/null +++ b/token.go @@ -0,0 +1,19 @@ +package main + +import ( + "crypto/rand" +) + +type Token struct { + data []byte +} + +func NewToken() Token { + t := Token{} + + t.data = make([]byte, 32) + + _, _ = rand.Read(t.data) + + return t +} diff --git a/token_test.go b/token_test.go new file mode 100644 index 0000000..66c131b --- /dev/null +++ b/token_test.go @@ -0,0 +1,30 @@ +package main + +import ( + "testing" + "reflect" +) + +func TestNewTokenNoInit(t *testing.T) { + empty := Token{} + + token := NewToken() + + if reflect.DeepEqual(empty, token) { + t.Fatalf("New token is not initialized.") + } +} + +func TestNewTokenLowEntropy(t *testing.T) { + token := NewToken() + + first := token.data[0] + + for _, v := range token.data { + if v != first { + return // at least one other byte contained + } + } + + t.Fatalf("All bytes in new token are the same.") +} |
