Skip to content

Commit

Permalink
Bug 1723674 - Implement crypto.randomUUID(). r=keeler,nika
Browse files Browse the repository at this point in the history
Google shipped crypto.randomUUID() in Chrome 92.

WICG draft specification for crypto.randomUUID():
https://wicg.github.io/uuid/

Differential Revision: https://phabricator.services.mozilla.com/D124313
  • Loading branch information
cpeterso committed Oct 14, 2021
1 parent c67e007 commit 2cc714c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 22 deletions.
14 changes: 14 additions & 0 deletions dom/base/Crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Crypto.h"
#include "js/ScalarType.h"
#include "js/experimental/TypedData.h" // JS_GetArrayBufferViewType
#include "nsCOMPtr.h"
#include "nsIRandomGenerator.h"
#include "nsReadableUtils.h"

#include "mozilla/dom/CryptoBinding.h"
#include "mozilla/dom/SubtleCrypto.h"
Expand Down Expand Up @@ -90,6 +92,18 @@ void Crypto::GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray,
aRetval.set(view);
}

void Crypto::RandomUUID(nsAString& aRetVal) {
// NSID_LENGTH == 39 == 36 UUID chars + 2 curly braces + 1 NUL byte
static_assert(NSID_LENGTH == 39);

nsIDToCString uuidString(nsID::GenerateUUID());
MOZ_ASSERT(strlen(uuidString.get()) == NSID_LENGTH - 1);

// Convert UUID chars to UTF-16 retval, omitting the curly braces and NUL.
CopyASCIItoUTF16(Substring(uuidString.get() + 1, NSID_LENGTH - 3), aRetVal);
MOZ_ASSERT(aRetVal.Length() == NSID_LENGTH - 3);
}

SubtleCrypto* Crypto::Subtle() {
if (!mSubtle) {
mSubtle = new SubtleCrypto(GetParentObject());
Expand Down
5 changes: 4 additions & 1 deletion dom/base/Crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_Crypto_h
#define mozilla_dom_Crypto_h

#include "mozilla/dom/SubtleCrypto.h"
#include "nsIGlobalObject.h"

#include "nsString.h"
#include "nsWrapperCache.h"
#include "mozilla/dom/TypedArray.h"

Expand All @@ -31,6 +32,8 @@ class Crypto final : public nsISupports, public nsWrapperCache {
void GetRandomValues(JSContext* aCx, const ArrayBufferView& aArray,
JS::MutableHandle<JSObject*> aRetval, ErrorResult& aRv);

void RandomUUID(nsAString& aRetVal);

SubtleCrypto* Subtle();

nsIGlobalObject* GetParentObject() const { return mParent; }
Expand Down
3 changes: 3 additions & 0 deletions dom/webidl/Crypto.webidl
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ interface Crypto {

[Throws]
ArrayBufferView getRandomValues(ArrayBufferView array);

[SecureContext, Pref="dom.crypto.randomUUID.enabled"]
DOMString randomUUID();
};
5 changes: 5 additions & 0 deletions modules/libpref/init/StaticPrefList.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,11 @@
value: false
mirror: always

- name: dom.crypto.randomUUID.enabled
type: RelaxedAtomicBool
value: true
mirror: always

# Is support for CSSPseudoElement enabled?
- name: dom.css_pseudo_element.enabled
type: bool
Expand Down
21 changes: 0 additions & 21 deletions testing/web-platform/meta/WebCryptoAPI/randomUUID.https.any.js.ini

This file was deleted.

0 comments on commit 2cc714c

Please sign in to comment.