From bf0cc2ba0f52a3e52e374954127c604827f7a6d6 Mon Sep 17 00:00:00 2001 From: lsiepel Date: Sat, 21 Oct 2023 11:56:27 +0200 Subject: [PATCH] adapt to core StringUtils (#15785) Signed-off-by: Leo Siepel --- .../internal/handler/MillheatAccountHandler.java | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/bundles/org.openhab.binding.millheat/src/main/java/org/openhab/binding/millheat/internal/handler/MillheatAccountHandler.java b/bundles/org.openhab.binding.millheat/src/main/java/org/openhab/binding/millheat/internal/handler/MillheatAccountHandler.java index 9ce18af1f8f..dfb912396fe 100644 --- a/bundles/org.openhab.binding.millheat/src/main/java/org/openhab/binding/millheat/internal/handler/MillheatAccountHandler.java +++ b/bundles/org.openhab.binding.millheat/src/main/java/org/openhab/binding/millheat/internal/handler/MillheatAccountHandler.java @@ -15,10 +15,8 @@ package org.openhab.binding.millheat.internal.handler; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; import java.util.List; import java.util.Optional; -import java.util.Random; import java.util.concurrent.ExecutionException; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.TimeUnit; @@ -73,6 +71,7 @@ import org.openhab.core.thing.binding.BaseBridgeHandler; import org.openhab.core.thing.binding.ThingHandler; import org.openhab.core.types.Command; import org.openhab.core.util.HexUtils; +import org.openhab.core.util.StringUtils; import org.osgi.framework.BundleContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -93,7 +92,6 @@ public class MillheatAccountHandler extends BaseBridgeHandler { private static final int NUM_NONCE_CHARS = 16; private static final String CONTENT_TYPE = "application/x-zc-object"; private static final String ALLOWED_NONCE_CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - private static final int ALLOWED_NONCE_CHARACTERS_LENGTH = ALLOWED_NONCE_CHARACTERS.length(); private static final String REQUEST_TIMEOUT = "300"; public static String authEndpoint = "https://eurouter.ablecloud.cn:9005/zc-account/v1/"; public static String serviceEndpoint = "https://eurouter.ablecloud.cn:9005/millService/v1/"; @@ -107,15 +105,6 @@ public class MillheatAccountHandler extends BaseBridgeHandler { private @Nullable ScheduledFuture statusFuture; private @NonNullByDefault({}) MillheatAccountConfiguration config; - private static String getRandomString(final int sizeOfRandomString) { - final Random random = new SecureRandom(); - final StringBuilder sb = new StringBuilder(sizeOfRandomString); - for (int i = 0; i < sizeOfRandomString; ++i) { - sb.append(ALLOWED_NONCE_CHARACTERS.charAt(random.nextInt(ALLOWED_NONCE_CHARACTERS_LENGTH))); - } - return sb.toString(); - } - public MillheatAccountHandler(final Bridge bridge, final HttpClient httpClient, final BundleContext context) { super(bridge); this.httpClient = httpClient; @@ -343,7 +332,7 @@ public class MillheatAccountHandler extends BaseBridgeHandler { } private Request buildLoggedInRequest(final AbstractRequest req) throws NoSuchAlgorithmException { - final String nonce = getRandomString(NUM_NONCE_CHARS); + final String nonce = StringUtils.getRandomString(NUM_NONCE_CHARS, ALLOWED_NONCE_CHARACTERS); final String timestamp = String.valueOf(System.currentTimeMillis() / 1000); final String signatureBasis = REQUEST_TIMEOUT + timestamp + nonce + token; MessageDigest md = MessageDigest.getInstance(SHA_1_ALGORITHM);