-
-
Notifications
You must be signed in to change notification settings - Fork 614
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3792 from Feoramund/core-uuid
Add `core:encoding/uuid`
- Loading branch information
Showing
11 changed files
with
1,526 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
BSD 3-Clause License | ||
|
||
Copyright (c) 2024, Feoramund | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package uuid | ||
|
||
// A RFC 4122 Universally Unique Identifier | ||
Identifier :: distinct [16]u8 | ||
|
||
EXPECTED_LENGTH :: 8 + 4 + 4 + 4 + 12 + 4 | ||
|
||
VERSION_BYTE_INDEX :: 6 | ||
VARIANT_BYTE_INDEX :: 8 | ||
|
||
// The number of 100-nanosecond intervals between 1582-10-15 and 1970-01-01. | ||
HNS_INTERVALS_BETWEEN_GREG_AND_UNIX :: 141427 * 24 * 60 * 60 * 1000 * 1000 * 10 | ||
|
||
VERSION_7_TIME_MASK :: 0xffffffff_ffff0000_00000000_00000000 | ||
VERSION_7_TIME_SHIFT :: 80 | ||
VERSION_7_COUNTER_MASK :: 0x00000000_00000fff_00000000_00000000 | ||
VERSION_7_COUNTER_SHIFT :: 64 | ||
|
||
@(private) | ||
NO_CSPRNG_ERROR :: "The context random generator is not cryptographic. See the documentation for an example of how to set one up." | ||
@(private) | ||
BIG_CLOCK_ERROR :: "The clock sequence can only hold 14 bits of data, therefore no number greater than 16,383 (0x3FFF)." | ||
@(private) | ||
VERSION_7_BIG_COUNTER_ERROR :: "This implementation of the version 7 UUID counter can only hold 12 bits of data, therefore no number greater than 4,095 (0xFFF)." | ||
|
||
Read_Error :: enum { | ||
None, | ||
Invalid_Length, | ||
Invalid_Hexadecimal, | ||
Invalid_Separator, | ||
} | ||
|
||
Variant_Type :: enum { | ||
Unknown, | ||
Reserved_Apollo_NCS, // 0b0xx | ||
RFC_4122, // 0b10x | ||
Reserved_Microsoft_COM, // 0b110 | ||
Reserved_Future, // 0b111 | ||
} | ||
|
||
// Name string is a fully-qualified domain name. | ||
@(rodata) | ||
Namespace_DNS := Identifier { | ||
0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, | ||
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8, | ||
} | ||
|
||
// Name string is a URL. | ||
@(rodata) | ||
Namespace_URL := Identifier { | ||
0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, | ||
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8, | ||
} | ||
|
||
// Name string is an ISO OID. | ||
@(rodata) | ||
Namespace_OID := Identifier { | ||
0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, | ||
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8, | ||
} | ||
|
||
// Name string is an X.500 DN (in DER or a text output format). | ||
@(rodata) | ||
Namespace_X500 := Identifier { | ||
0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, | ||
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
package uuid implements Universally Unique Identifiers according to the | ||
standard originally outlined in RFC 4122 with additions from RFC 9562. | ||
The UUIDs are textually represented and read in the following string format: | ||
`00000000-0000-v000-V000-000000000000` | ||
`v` is where the version bits reside, and `V` is where the variant bits reside. | ||
The meaning of the other bits is version-dependent. | ||
Outside of string representations, UUIDs are represented in memory by a 128-bit | ||
structure organized as an array of 16 bytes. | ||
Of the UUID versions which may make use of random number generation, a | ||
requirement is placed upon them that the underlying generator be | ||
cryptographically-secure, per RFC 9562's suggestion. | ||
- Version 1 without a node argument. | ||
- Version 4 in all cases. | ||
- Version 6 without either a clock or node argument. | ||
- Version 7 in all cases. | ||
Here's an example of how to set up one: | ||
import "core:crypto" | ||
import "core:encoding/uuid" | ||
main :: proc() { | ||
my_uuid: uuid.Identifier | ||
{ | ||
// This scope will have a CSPRNG. | ||
context.random_generator = crypto.random_generator() | ||
my_uuid = uuid.generate_v7() | ||
} | ||
// Back to the default random number generator. | ||
} | ||
For more information on the specifications, see here: | ||
- https://www.rfc-editor.org/rfc/rfc4122.html | ||
- https://www.rfc-editor.org/rfc/rfc9562.html | ||
*/ | ||
package uuid |
Oops, something went wrong.