Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

feat: describe UUID v4 algorithm #6

Merged
merged 12 commits into from
Feb 4, 2021
106 changes: 105 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,114 @@ <h3>Extensions to the <code>Crypto</code> interface</h3>
</p>
<pre class="idl">
[Exposed=(Window,Worker)]
partial interface <dfn id="dfn-Crypto"></dfn>Crypto</dfn> {
partial interface Crypto {
DOMString randomUUID();
};
</pre>
<p class="note" role="note"><span>Note:
<code><a href="#random-uuid" data-link-type="dfn">randomUUID()</a></code> generates a new
<a data-cite="RFC4122#section-4.4">version 4 UUID</a> and returns its
<a data-cite="RFC2141#section-2.2">namespace specific string representation</a> described in
<a data-cite="RFC2141#section-2.2">namespace specific string</a> representation of a UUID described in
<a data-cite="RFC4122#section-3">RFC4122</a>.
</span>
<section>
<h4>The <code class="idl"><a href="#random-uuid" data-link-type="dfn">randomUUID()</a></code> method.</h4>
The <dfn class="dfn-paneled idl-code" data-dfn-for="Crypto" data-dfn-type="method" data-export data-lt="randomUUID" id="random-uuid">
<code>randomUUID()</code>
</dfn> method steps are:
<ol>
<li>
<p>
Let <var>array</var> be a <a data-cite="INFRA#list">list</a> with 16 elements of the type <a data-cite="INFRA#byte">byte</a>.
</p>
</li>
<li>
<p>
Overwrite all elements of <var>array</var> with cryptographically secure random values of
the type <a data-cite="INFRA#byte">byte</a>.
</p>
</li>
<li>
<p>
Let <var>timeLow</var> be the <a data-type="typedef" href="https://infra.spec.whatwg.org/#string-concatenate">concatenation</a>
of « <a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> of <var>array</var>[0],
<a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> of <var>array</var>[1], <a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> of <var>array</var>[2], <a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> of <var>array</var>[3] ».
</p>
</li>
<li>
<p>
Let <var>timeMid</var> be the <a data-type="typedef" href="https://infra.spec.whatwg.org/#string-concatenate">concatenation</a>
of « <a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> of <var>array</var>[4],
<a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> of <var>array</var>[5] ».
</p>
</li>
<li>
<p>
Let <var>timeHighAndVersion</var> be a 4 character <a data-type="typedef" href="https://infra.spec.whatwg.org/#string">string</a>
populated as follows:
</p>
<ol>
<li>
<p>
Set the 4 most significant bits of <var>array</var>[6], which represent the UUID <a data-cite="RFC4122#section-4.1.3">version</a>, to <code>0b0100</code>.
</p>
</li>
<li>
<p>
Set <var>timeHighAndVersion</var> be the <a data-type="typedef" href="https://infra.spec.whatwg.org/#string-concatenate">concatenation</a>
of « <a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> of <var>array</var>[6],
<a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> of <var>array</var>[7] ».
</p>
</li>
</ol>
</li>
<li>
<p>
Let <var>clockSeqAndReservedClockSeqLow</var> be a 4 character <a data-type="typedef" href="https://infra.spec.whatwg.org/#string">string</a>
populated as follows:
</p>
<ol>
<li>
<p>
Set the 2 most significant bits of <var>array</var>[8]
to the <code>0b10</code> <a data-cite="RFC4122#section-4.1.1">variant</a>.
bcoe marked this conversation as resolved.
Show resolved Hide resolved
</p>
</li>
<li>
<p>
Set <var>clockSeqAndReservedClockSeqLow</var> be the <a data-type="typedef" href="https://infra.spec.whatwg.org/#string-concatenate">concatenation</a>
of « <a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> <var>array</var>[8],
<a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> <var>array</var>[9] ».
</p>
</li>
</ol>
</li>
<li>
<p>
Let <var>node</var> be the <a data-type="typedef" href="https://infra.spec.whatwg.org/#string-concatenate">concatenation</a>
of « <a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> <var>array</var>[10],
<a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> <var>array</var>[11], <a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> <var>array</var>[12], <a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> <var>array</var>[13],
<a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> <var>array</var>[14], <a href="#hex-representation" data-link-type="dfn">hexadecimal representation</a> <var>array</var>[15] ».
</p>
</li>
<li>
<p>
Return the <a data-type="typedef" href="https://infra.spec.whatwg.org/#string-concatenate">concatenation</a> of
« <var>timeLow</var>, <var>timeMid</var>, <var>timeHighAndVersion</var>,
<var>clockSeqAndReservedClockSeqLow</var>, <var>node</var> » with separator <code>"-"</code>.
</p>
</li>
</ol>
<p>
For the steps described in the <code><a href="#random-uuid" data-link-type="dfn">randomUUID()</a></code> algorithm,
the <dfn class="dfn-paneled" data-dfn-type="dfn" data-export data-lt="hexadecimal representation" id="hex-representation">hexadecimal representation</dfn>
of a <a data-cite="INFRA#byte">byte</a> <var>value</var> is the
two-character string created by expressing <var>value</var> in hexadecimal
using <a data-cite="INFRA#ascii-lower-hex-digit">ASCII lower hex digits</a>,
left-padded with <code>"0"</code> to reach two characters.
</p>
</section>
</section>
</section>
</body>
Expand Down