From e12f99ab493e905f7bf8e09b257c6be007b98fb1 Mon Sep 17 00:00:00 2001 From: Ivan Hetman Date: Wed, 7 Dec 2022 13:33:27 +0200 Subject: [PATCH] Crypto/UUID (#639) JS: if possible, use crypto API to generate UUID --- .../org/teavm/classlib/java/util/TUUID.java | 12 +++-- .../java/org/teavm/jso/crypto/Crypto.java | 47 +++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) create mode 100644 jso/apis/src/main/java/org/teavm/jso/crypto/Crypto.java diff --git a/classlib/src/main/java/org/teavm/classlib/java/util/TUUID.java b/classlib/src/main/java/org/teavm/classlib/java/util/TUUID.java index 1ed20453b..ac80daf0b 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/util/TUUID.java +++ b/classlib/src/main/java/org/teavm/classlib/java/util/TUUID.java @@ -15,8 +15,10 @@ */ package org.teavm.classlib.java.util; -public class TUUID { +import org.teavm.classlib.PlatformDetector; +import org.teavm.jso.crypto.Crypto; +public class TUUID { private String value; private TUUID(String value) { @@ -44,8 +46,12 @@ public class TUUID { } public static TUUID randomUUID() { - String value = s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4(); - return new TUUID(value); + if (PlatformDetector.isJavaScript() && Crypto.isSupported()) { + return new TUUID(Crypto.current().randomUUID()); + } else { + String value = s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4(); + return new TUUID(value); + } } private static String s4() { diff --git a/jso/apis/src/main/java/org/teavm/jso/crypto/Crypto.java b/jso/apis/src/main/java/org/teavm/jso/crypto/Crypto.java new file mode 100644 index 000000000..d32cadfbd --- /dev/null +++ b/jso/apis/src/main/java/org/teavm/jso/crypto/Crypto.java @@ -0,0 +1,47 @@ +/* + * Copyright 2022 ihromant. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.jso.crypto; + +import org.teavm.jso.JSBody; +import org.teavm.jso.JSObject; +import org.teavm.jso.typedarrays.Int16Array; +import org.teavm.jso.typedarrays.Int32Array; +import org.teavm.jso.typedarrays.Int8Array; +import org.teavm.jso.typedarrays.Uint16Array; +import org.teavm.jso.typedarrays.Uint8Array; +import org.teavm.jso.typedarrays.Uint8ClampedArray; + +public abstract class Crypto implements JSObject { + @JSBody(script = "return window.crypto != null;") + public static native boolean isSupported(); + + @JSBody(script = "return window.crypto;") + public static native Crypto current(); + + public abstract String randomUUID(); + + public abstract void getRandomValues(Int8Array arr); + + public abstract void getRandomValues(Uint8Array arr); + + public abstract void getRandomValues(Uint8ClampedArray arr); + + public abstract void getRandomValues(Int16Array arr); + + public abstract void getRandomValues(Uint16Array arr); + + public abstract void getRandomValues(Int32Array arr); +}