Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 1 | // 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 | |
| 7 | package uuid |
| 8 | |
| 9 | import ( |
| 10 | "bytes" |
| 11 | "crypto/rand" |
| 12 | "io" |
| 13 | ) |
| 14 | |
| 15 | // UUID represents a UUID. |
| 16 | type UUID [16]byte |
| 17 | |
| 18 | var rander = rand.Reader |
| 19 | |
| 20 | // New generates a new uuid. |
| 21 | func 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. |
| 35 | func Equal(a, b UUID) bool { |
| 36 | return bytes.Equal([]byte(a[:]), []byte(b[:])) |
| 37 | } |