Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[optimize] crypto.getRandomValues judgements
  • Loading branch information
Hiram committed Dec 22, 2024
commit ff2f72880f18e3c79b8e00ea27e9287c81a83a47
8 changes: 7 additions & 1 deletion apps/cookie-butler/static/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,16 @@ class QRCodeHandler {
static generateUUID() {
if (crypto && typeof crypto.randomUUID === 'function') {
return crypto.randomUUID();
} else {
} else if (crypto && typeof crypto.getRandomValues === 'function') {
return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c =>
(+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)
);
} else {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0,
v = c == 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
}
}

Expand Down