Skip to content

Commit

Permalink
Merge pull request #6 from twharmon/secure
Browse files Browse the repository at this point in the history
Use secure charsets only
  • Loading branch information
twharmon authored Jan 11, 2024
2 parents f1239d5 + aa79854 commit d00a55e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 53 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
)

func main() {
a := gouid.String(8, gouid.LowerCaseAlphaNum)
fmt.Println(a) // mp6nq37p
a := gouid.String(8, gouid.Secure32Char)
fmt.Println(a) // mp1nq34p

b := gouid.String(16, gouid.MixedCaseAlpha)
fmt.Println(b) // hCSoemLKaUQtoXgh
b := gouid.String(16, gouid.Secure64Char)
fmt.Println(b) // h-SoemLKa_QtoXgh

c := gouid.Bytes(16)
fmt.Println(c) // [244 188 217 137 122 245 94 126 80 119 87 170 6 178 228 179]
Expand All @@ -35,12 +35,16 @@ func main() {
## Benchmarks

```
BenchmarkString8 120 ns/op 8 B/op 1 allocs/op
BenchmarkString16 197 ns/op 16 B/op 1 allocs/op
BenchmarkString32 345 ns/op 32 B/op 1 allocs/op
BenchmarkBytes8 67.3 ns/op 8 B/op 1 allocs/op
BenchmarkBytes16 94.4 ns/op 16 B/op 1 allocs/op
BenchmarkBytes32 143 ns/op 32 B/op 1 allocs/op
goos: linux
goarch: amd64
pkg: github.com/twharmon/gouid
cpu: AMD Ryzen 7 7840HS w/ Radeon 780M Graphics
BenchmarkString8 337.6 ns/op 8 B/op 1 allocs/op
BenchmarkString16 359.1 ns/op 16 B/op 1 allocs/op
BenchmarkString32 363.5 ns/op 32 B/op 1 allocs/op
BenchmarkBytes8 327.0 ns/op 8 B/op 1 allocs/op
BenchmarkBytes16 334.0 ns/op 16 B/op 1 allocs/op
BenchmarkBytes32 334.7 ns/op 32 B/op 1 allocs/op
```

## Contribute
Expand Down
10 changes: 4 additions & 6 deletions gouid.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ type GOUID []byte

// Charsets for string gouids.
var (
LowerCaseAlphaNum = []byte("abcdefghijklmnopqrstuvwxyz0123456789")
UpperCaseAlphaNum = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
MixedCaseAlphaNum = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
LowerCaseAlpha = []byte("abcdefghijklmnopqrstuvwxyz")
UpperCaseAlpha = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
MixedCaseAlpha = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
// Cryptographically secure charsets should include N characters,
// where N is a factor of 256 (2, 4, 8, 16, 32, 64, 128, 256)
Secure32Char = []byte("abcdefghijklmnopqrstuvwxyz012345")
Secure64Char = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-")
)

// String returns a string with the given size made up of
Expand Down
46 changes: 9 additions & 37 deletions gouid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,53 +19,25 @@ import (

func TestString(t *testing.T) {
length := 32
id := gouid.String(length, gouid.MixedCaseAlphaNum)
id := gouid.String(length, gouid.Secure64Char)
if len(id) != length {
t.Error("lengh of id was not 32")
}
if id == gouid.String(length, gouid.MixedCaseAlphaNum) {
if id == gouid.String(length, gouid.Secure64Char) {
t.Error("collision")
}
}

func TestStringLowerCaseAlpha(t *testing.T) {
func TestStringSecure64Char(t *testing.T) {
length := 256
if !regexp.MustCompile(fmt.Sprintf("[a-z]{%d}", length)).Match([]byte(gouid.String(length, gouid.LowerCaseAlpha))) {
if !regexp.MustCompile(fmt.Sprintf("[a-zA-Z0-9-_]{%d}", length)).Match([]byte(gouid.String(length, gouid.Secure64Char))) {
t.Error("string did not match expected regexp")
}
}

func TestStringUpperCaseAlpha(t *testing.T) {
func TestStringSecure32Char(t *testing.T) {
length := 256
if !regexp.MustCompile(fmt.Sprintf("[A-Z]{%d}", length)).Match([]byte(gouid.String(length, gouid.UpperCaseAlpha))) {
t.Error("string did not match expected regexp")
}
}

func TestStringMixedCaseAlpha(t *testing.T) {
length := 256
if !regexp.MustCompile(fmt.Sprintf("[a-zA-Z]{%d}", length)).Match([]byte(gouid.String(length, gouid.MixedCaseAlpha))) {
t.Error("string did not match expected regexp")
}
}

func TestStringLowerCaseAlphaNum(t *testing.T) {
length := 256
if !regexp.MustCompile(fmt.Sprintf("[a-z0-9]{%d}", length)).Match([]byte(gouid.String(length, gouid.LowerCaseAlphaNum))) {
t.Error("string did not match expected regexp")
}
}

func TestStringUpperCaseAlphaNum(t *testing.T) {
length := 256
if !regexp.MustCompile(fmt.Sprintf("[A-Z0-9]{%d}", length)).Match([]byte(gouid.String(length, gouid.UpperCaseAlphaNum))) {
t.Error("string did not match expected regexp")
}
}

func TestStringMixedCaseAlphaNum(t *testing.T) {
length := 256
if !regexp.MustCompile(fmt.Sprintf("[a-zA-Z0-9]{%d}", length)).Match([]byte(gouid.String(length, gouid.MixedCaseAlphaNum))) {
if !regexp.MustCompile(fmt.Sprintf("[a-z0-5]{%d}", length)).Match([]byte(gouid.String(length, gouid.Secure32Char))) {
t.Error("string did not match expected regexp")
}
}
Expand Down Expand Up @@ -110,19 +82,19 @@ func TestBytesUnmarshal(t *testing.T) {

func BenchmarkString8(b *testing.B) {
for i := 0; i < b.N; i++ {
gouid.String(8, gouid.MixedCaseAlphaNum)
gouid.String(8, gouid.Secure64Char)
}
}

func BenchmarkString16(b *testing.B) {
for i := 0; i < b.N; i++ {
gouid.String(16, gouid.MixedCaseAlphaNum)
gouid.String(16, gouid.Secure64Char)
}
}

func BenchmarkString32(b *testing.B) {
for i := 0; i < b.N; i++ {
gouid.String(32, gouid.MixedCaseAlphaNum)
gouid.String(32, gouid.Secure64Char)
}
}
func BenchmarkBytes8(b *testing.B) {
Expand Down

0 comments on commit d00a55e

Please sign in to comment.