1package faker
 2
 3import (
 4	"fmt"
 5	"io"
 6	"reflect"
 7
 8	"github.com/go-faker/faker/v4/pkg/options"
 9)
10
11// GetIdentifier returns a new Identifier
12func GetIdentifier() Identifier {
13	return &UUID{}
14}
15
16// Identifier ...
17type Identifier interface {
18	Digit(v reflect.Value) (interface{}, error)
19	Hyphenated(v reflect.Value) (interface{}, error)
20}
21
22// UUID struct
23type UUID struct{}
24
25// createUUID returns a 16 byte slice with random values
26func createUUID() ([]byte, error) {
27	b := make([]byte, 16)
28	_, err := io.ReadFull(crypto, b)
29	if err != nil {
30		return b, err
31	}
32	// variant bits; see section 4.1.1
33	b[8] = b[8]&^0xc0 | 0x80
34	// version 4 (pseudo-random); see section 4.1.3
35	b[6] = b[6]&^0xf0 | 0x40
36	return b, nil
37}
38
39func (u UUID) hyphenated() (string, error) {
40	b, err := createUUID()
41	if err != nil {
42		return "", err
43	}
44	uuid := fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:])
45	return uuid, err
46}
47
48// Hyphenated returns a 36 byte hyphenated UUID
49func (u UUID) Hyphenated(v reflect.Value) (interface{}, error) {
50	return u.hyphenated()
51}
52
53// UUIDHyphenated get fake Hyphenated UUID
54func UUIDHyphenated(opts ...options.OptionFunc) string {
55	return singleFakeData(HyphenatedID, func() interface{} {
56		u := UUID{}
57		res, _ := u.hyphenated()
58		return res
59	}, opts...).(string)
60}
61
62func (u UUID) digit() (string, error) {
63	b, err := createUUID()
64	if err != nil {
65		return "", err
66	}
67	uuid := fmt.Sprintf("%x", b)
68	return uuid, err
69}
70
71// Digit returns a 32 bytes UUID
72func (u UUID) Digit(v reflect.Value) (interface{}, error) {
73	return u.digit()
74}
75
76// UUIDDigit get fake Digit UUID
77func UUIDDigit(opts ...options.OptionFunc) string {
78	return singleFakeData(ID, func() interface{} {
79		u := UUID{}
80		res, _ := u.digit()
81		return res
82	}, opts...).(string)
83}