Skip to content

Commit 187989a

Browse files
author
Tim Harper
authored
Merge pull request #69 from ccutrer/generate_pin
Helper method for generating random, valid PINs
2 parents 661e6e0 + 8246b5c commit 187989a

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/main/java/com/beowulfe/hap/HomekitServer.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,14 @@ public static byte[] generateKey() throws InvalidAlgorithmParameterException {
149149
public static String generateMac() {
150150
return HomekitUtils.generateMac();
151151
}
152+
153+
/**
154+
* Generates a value to supply in {@link HomekitAuthInfo#getPin() HomekitAuthInfo.getPin()}. This
155+
* is used as the Pin a user enters into their Homekit device in order to confirm pairing.
156+
*
157+
* @return the generated Pin
158+
*/
159+
public static String generatePin() {
160+
return HomekitUtils.generatePin();
161+
}
152162
}

src/main/java/com/beowulfe/hap/impl/HomekitUtils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,33 @@ public static String generateMac() {
3535
.collect(Collectors.joining(":"));
3636
}
3737

38+
public static String generatePin() {
39+
String pin =
40+
String.format(
41+
"%03d-%02d-%03d",
42+
getSecureRandom().nextInt(1000),
43+
getSecureRandom().nextInt(100),
44+
getSecureRandom().nextInt(1000));
45+
46+
if (pin == "000-00-000"
47+
|| pin == "111-11-111"
48+
|| pin == "222-22-222"
49+
|| pin == "333-33-333"
50+
|| pin == "444-44-444"
51+
|| pin == "555-55-555"
52+
|| pin == "666-66-666"
53+
|| pin == "777-77-777"
54+
|| pin == "888-88-888"
55+
|| pin == "999-99-999"
56+
|| pin == "123-45-678"
57+
|| pin == "876-54-321") {
58+
// disallowed Pin; just recurse and generate a new one
59+
return generatePin();
60+
}
61+
62+
return pin;
63+
}
64+
3865
private static SecureRandom getSecureRandom() {
3966
if (secureRandom == null) {
4067
synchronized (HomekitUtils.class) {

0 commit comments

Comments
 (0)