Skip to content

Commit 548bfc5

Browse files
author
Eugen Freiter
committed
add default implementation for setup Id
Signed-off-by: Eugen Freiter <freiter@gmx.de>
1 parent c345a4b commit 548bfc5

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

src/main/java/io/github/hapjava/server/HomekitAuthInfo.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.hapjava.server;
22

33
import io.github.hapjava.server.impl.HomekitServer;
4+
import io.github.hapjava.server.impl.crypto.HAPSetupCodeUtils;
45
import java.math.BigInteger;
56

67
/**
@@ -27,7 +28,9 @@ public interface HomekitAuthInfo {
2728
*
2829
* @return setup id
2930
*/
30-
String getSetupId();
31+
default String getSetupId() {
32+
return HAPSetupCodeUtils.generateSetupId();
33+
}
3134

3235
/**
3336
* A unique MAC address to be advertised with the HomeKit information. This does not have to be

src/main/java/io/github/hapjava/server/impl/crypto/HAPSetupCodeUtils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@
55
import org.bouncycastle.crypto.digests.SHA512Digest;
66

77
public class HAPSetupCodeUtils {
8+
private static final String ALPHA_NUMERIC_STRING = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
9+
10+
public static String randomAlphaNumeric(int count) {
11+
StringBuilder builder = new StringBuilder();
12+
while (count-- != 0) {
13+
int character = (int) (Math.random() * ALPHA_NUMERIC_STRING.length());
14+
builder.append(ALPHA_NUMERIC_STRING.charAt(character));
15+
}
16+
return builder.toString();
17+
}
18+
19+
public static String generateSetupId() {
20+
String setupId =
21+
""
22+
+ ALPHA_NUMERIC_STRING.charAt((int) (Math.random() * ALPHA_NUMERIC_STRING.length()))
23+
+ ALPHA_NUMERIC_STRING.charAt((int) (Math.random() * ALPHA_NUMERIC_STRING.length()))
24+
+ ALPHA_NUMERIC_STRING.charAt((int) (Math.random() * ALPHA_NUMERIC_STRING.length()))
25+
+ ALPHA_NUMERIC_STRING.charAt((int) (Math.random() * ALPHA_NUMERIC_STRING.length()));
26+
return setupId;
27+
}
828

929
private static byte[] calculateHash(final String input, final Digest digest) {
1030
byte[] inputAsBytes = input.getBytes();

src/main/java/io/github/hapjava/server/impl/jmdns/JmdnsHomekitAdvertiser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ public synchronized void setConfigurationIndex(int revision) throws IOException
8484

8585
private void registerService() throws IOException {
8686
logger.info("Registering " + SERVICE_TYPE + " on port " + port);
87+
logger.trace("MAC:" + mac + " Setup Id:" + setupId);
8788
Map<String, String> props = new HashMap<>();
8889
props.put("sf", discoverable ? "1" : "0");
8990
props.put("id", mac);

0 commit comments

Comments
 (0)