Skip to content

Commit

Permalink
Helper method for generating random, valid PINs
Browse files Browse the repository at this point in the history
Now that we have the spec, we know what PINs are invalid to easily weed them out.
  • Loading branch information
ccutrer committed Feb 12, 2019
1 parent 939c344 commit 8246b5c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/beowulfe/hap/HomekitServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,14 @@ public static byte[] generateKey() throws InvalidAlgorithmParameterException {
public static String generateMac() {
return HomekitUtils.generateMac();
}

/**
* Generates a value to supply in {@link HomekitAuthInfo#getPin() HomekitAuthInfo.getPin()}. This
* is used as the Pin a user enters into their Homekit device in order to confirm pairing.
*
* @return the generated Pin
*/
public static String generatePin() {
return HomekitUtils.generatePin();
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/beowulfe/hap/impl/HomekitUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@ public static String generateMac() {
.collect(Collectors.joining(":"));
}

public static String generatePin() {
String pin =
String.format(
"%03d-%02d-%03d",
getSecureRandom().nextInt(1000),
getSecureRandom().nextInt(100),
getSecureRandom().nextInt(1000));

if (pin == "000-00-000"
|| pin == "111-11-111"
|| pin == "222-22-222"
|| pin == "333-33-333"
|| pin == "444-44-444"
|| pin == "555-55-555"
|| pin == "666-66-666"
|| pin == "777-77-777"
|| pin == "888-88-888"
|| pin == "999-99-999"
|| pin == "123-45-678"
|| pin == "876-54-321") {
// disallowed Pin; just recurse and generate a new one
return generatePin();
}

return pin;
}

private static SecureRandom getSecureRandom() {
if (secureRandom == null) {
synchronized (HomekitUtils.class) {
Expand Down

0 comments on commit 8246b5c

Please sign in to comment.