Skip to content

Commit

Permalink
Corrected generation of UUID v4
Browse files Browse the repository at this point in the history
  • Loading branch information
jaswdr committed Aug 10, 2018
1 parent 62e54f8 commit 5f00643
Showing 1 changed file with 11 additions and 30 deletions.
41 changes: 11 additions & 30 deletions uuid.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
package faker

import (
"crypto/rand"
"fmt"
"io"
)

type UUID struct {
Faker *Faker
}

func (u UUID) randomAFNumber() string {
group := []string{
"a", "b", "c", "d", "e", "f",
"A", "B", "C", "D", "E", "F",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
}

return u.Faker.RandomStringElement(group)
}

func (u UUID) randomAFNumberCount(count int) (result string) {
for i := 0; i < count; i++ {
result += u.randomAFNumber()
}

return
}

func (u UUID) V4() (uuid string) {
uuid += u.randomAFNumberCount(8)
uuid += "-"
uuid += u.randomAFNumberCount(4)
uuid += "-"
uuid += "4"
uuid += u.randomAFNumberCount(3)
uuid += "-"
uuid += u.Faker.RandomStringElement([]string{"8", "9", "a", "A", "b", "B"})
uuid += u.randomAFNumberCount(3)
uuid += "-"
uuid += u.randomAFNumberCount(12)
return
var uiq [16]byte
io.ReadFull(rand.Reader, uiq[:])
uiq[6] = (uiq[6] & 0x0f) | 0x40 // Version 4
uiq[8] = (uiq[8]&(0xff>>2) | (0x02 << 6)) // Variant RFC4122
return fmt.Sprintf("%x%x%x%x%x", uiq[0:4], uiq[4:6], uiq[6:8], uiq[8:10], uiq[10:])
}

0 comments on commit 5f00643

Please sign in to comment.