15
15
import java .nio .charset .StandardCharsets ;
16
16
import java .security .MessageDigest ;
17
17
import java .security .NoSuchAlgorithmException ;
18
- import java .security .SecureRandom ;
19
18
import java .util .List ;
20
19
import java .util .Optional ;
21
- import java .util .Random ;
22
20
import java .util .concurrent .ExecutionException ;
23
21
import java .util .concurrent .ScheduledFuture ;
24
22
import java .util .concurrent .TimeUnit ;
73
71
import org .openhab .core .thing .binding .ThingHandler ;
74
72
import org .openhab .core .types .Command ;
75
73
import org .openhab .core .util .HexUtils ;
74
+ import org .openhab .core .util .StringUtils ;
76
75
import org .osgi .framework .BundleContext ;
77
76
import org .slf4j .Logger ;
78
77
import org .slf4j .LoggerFactory ;
@@ -93,7 +92,6 @@ public class MillheatAccountHandler extends BaseBridgeHandler {
93
92
private static final int NUM_NONCE_CHARS = 16 ;
94
93
private static final String CONTENT_TYPE = "application/x-zc-object" ;
95
94
private static final String ALLOWED_NONCE_CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
96
- private static final int ALLOWED_NONCE_CHARACTERS_LENGTH = ALLOWED_NONCE_CHARACTERS .length ();
97
95
private static final String REQUEST_TIMEOUT = "300" ;
98
96
public static String authEndpoint = "https://eurouter.ablecloud.cn:9005/zc-account/v1/" ;
99
97
public static String serviceEndpoint = "https://eurouter.ablecloud.cn:9005/millService/v1/" ;
@@ -107,15 +105,6 @@ public class MillheatAccountHandler extends BaseBridgeHandler {
107
105
private @ Nullable ScheduledFuture <?> statusFuture ;
108
106
private @ NonNullByDefault ({}) MillheatAccountConfiguration config ;
109
107
110
- private static String getRandomString (final int sizeOfRandomString ) {
111
- final Random random = new SecureRandom ();
112
- final StringBuilder sb = new StringBuilder (sizeOfRandomString );
113
- for (int i = 0 ; i < sizeOfRandomString ; ++i ) {
114
- sb .append (ALLOWED_NONCE_CHARACTERS .charAt (random .nextInt (ALLOWED_NONCE_CHARACTERS_LENGTH )));
115
- }
116
- return sb .toString ();
117
- }
118
-
119
108
public MillheatAccountHandler (final Bridge bridge , final HttpClient httpClient , final BundleContext context ) {
120
109
super (bridge );
121
110
this .httpClient = httpClient ;
@@ -343,7 +332,7 @@ private void updateThingStatuses() {
343
332
}
344
333
345
334
private Request buildLoggedInRequest (final AbstractRequest req ) throws NoSuchAlgorithmException {
346
- final String nonce = getRandomString (NUM_NONCE_CHARS );
335
+ final String nonce = StringUtils . getRandomString (NUM_NONCE_CHARS , ALLOWED_NONCE_CHARACTERS );
347
336
final String timestamp = String .valueOf (System .currentTimeMillis () / 1000 );
348
337
final String signatureBasis = REQUEST_TIMEOUT + timestamp + nonce + token ;
349
338
MessageDigest md = MessageDigest .getInstance (SHA_1_ALGORITHM );
0 commit comments