blob: 7ad5eea0c987b5a5dbdf7214c5d99c98f7cc8ead [file] [log] [blame]
Don Newton379ae252019-04-01 12:17:06 -04001// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package uuid
8
9import (
10 "bytes"
11 "crypto/rand"
12 "io"
13)
14
15// UUID represents a UUID.
16type UUID [16]byte
17
18var rander = rand.Reader
19
20// New generates a new uuid.
21func New() (UUID, error) {
22 var uuid [16]byte
23
24 _, err := io.ReadFull(rander, uuid[:])
25 if err != nil {
26 return [16]byte{}, err
27 }
28 uuid[6] = (uuid[6] & 0x0f) | 0x40 // Version 4
29 uuid[8] = (uuid[8] & 0x3f) | 0x80 // Variant is 10
30
31 return uuid, nil
32}
33
34// Equal returns true if two UUIDs are equal.
35func Equal(a, b UUID) bool {
36 return bytes.Equal([]byte(a[:]), []byte(b[:]))
37}