{"action":"created","issue":{"url":"https://api.github.com/repos/beowulfe/HAP-Java/issues/8","labels_url":"https://api.github.com/repos/beowulfe/HAP-Java/issues/8/labels{/name}","comments_url":"https://api.github.com/repos/beowulfe/HAP-Java/issues/8/comments","events_url":"https://api.github.com/repos/beowulfe/HAP-Java/issues/8/events","html_url":"https://github.com/beowulfe/HAP-Java/issues/8","id":124381092,"number":8,"title":"Persisting fails","user":{"login":"comdata","id":4700879,"avatar_url":"https://avatars.githubusercontent.com/u/4700879?v=3","gravatar_id":"","url":"https://api.github.com/users/comdata","html_url":"https://github.com/comdata","followers_url":"https://api.github.com/users/comdata/followers","following_url":"https://api.github.com/users/comdata/following{/other_user}","gists_url":"https://api.github.com/users/comdata/gists{/gist_id}","starred_url":"https://api.github.com/users/comdata/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/comdata/subscriptions","organizations_url":"https://api.github.com/users/comdata/orgs","repos_url":"https://api.github.com/users/comdata/repos","events_url":"https://api.github.com/users/comdata/events{/privacy}","received_events_url":"https://api.github.com/users/comdata/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-12-30T18:06:13Z","updated_at":"2016-01-06T13:49:55Z","closed_at":null,"body":"Hi again,\r\n\r\nI tried persisting the settings but so far I had no luck getting this to work.\r\n\r\nThe following is my AuthInfo class.\r\n\r\nAs far as I can see there is no request coming from my phone when the service comes back alive.\r\n\r\nAny ideas?\r\n\r\n---------------------------------------------\r\npackage cm.homeautomation.hap;\r\n\r\nimport java.io.DataInputStream;\r\nimport java.io.DataOutputStream;\r\nimport java.io.File;\r\nimport java.io.FileInputStream;\r\nimport java.io.FileNotFoundException;\r\nimport java.io.FileOutputStream;\r\nimport java.io.IOException;\r\nimport java.io.InputStream;\r\nimport java.io.OutputStream;\r\nimport java.math.BigInteger;\r\nimport java.nio.charset.StandardCharsets;\r\nimport java.security.InvalidAlgorithmParameterException;\r\nimport java.util.Properties;\r\nimport java.util.Set;\r\nimport java.util.concurrent.ConcurrentHashMap;\r\nimport java.util.concurrent.ConcurrentMap;\r\n\r\nimport org.apache.commons.codec.binary.Base64;\r\n\r\nimport com.beowulfe.hap.HomekitAuthInfo;\r\nimport com.beowulfe.hap.HomekitServer;\r\n\r\n/**\r\n */\r\npublic class AuthInfoService implements HomekitAuthInfo {\r\n\r\n\tprivate static String PIN = \"032-45-154\";\r\n\r\n\tprivate static String mac;\r\n\tprivate static BigInteger salt;\r\n\tprivate static byte[] privateKey;\r\n\tprivate static ConcurrentMap<String, byte[]> userKeyMap = new ConcurrentHashMap<>();\r\n\r\n\tpublic AuthInfoService() throws InvalidAlgorithmParameterException {\r\n\r\n\t\tProperties props = new Properties();\r\n\t\ttry {\r\n\t\t\tInputStream resourceAsStream = new FileInputStream(new File(\"/home/hap/\" + \"server.properties\"));\r\n\r\n\t\t\tif (resourceAsStream != null) {\r\n\t\t\t\tprops.load(resourceAsStream);\r\n\t\t\t}\r\n\t\t} catch (IOException e) {\r\n\t\t\t// TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\r\n\t\tif (props.getProperty(\"mac\") != null) {\r\n\t\t\tSystem.out.println(\"using existing config\");\r\n\t\t\tmac = props.getProperty(\"mac\");\r\n\t\t\tsalt = new BigInteger(Base64.decodeBase64((String) props.get(\"salt\")));\r\n\r\n\t\t\tprivateKey = readKey(\"privateKey\");\r\n\r\n\t\t\tSystem.out.println(new String(privateKey));\r\n\t\t\tSystem.out.println(mac);\r\n\t\t\tSystem.out.println(salt);\r\n\r\n\t\t\tloadUsers();\r\n\r\n\t\t} else {\r\n\t\t\tSystem.out.println(\"generating new config\");\r\n\t\t\tmac = \"04:a1:51:69:94:55\";// HomekitServer.generateMac();\r\n\t\t\tsalt = HomekitServer.generateSalt();\r\n\t\t\tprivateKey = HomekitServer.generateKey();\r\n\t\t\tSystem.out.println(\"SALT: \" + salt);\r\n\t\t\tSystem.out.println(\"KEY: \" + new String(privateKey));\r\n\t\t\tsaveParamChanges(mac, salt, privateKey);\r\n\r\n\t\t\tsaveKey(\"privateKey\", privateKey);\r\n\t\t}\r\n\r\n\t\tSystem.out.println(\r\n\t\t\t\t\"Auth info is generated each time the sample application is started. Pairings are not persisted.\");\r\n\t\tSystem.out.println(\"The PIN for pairing is \" + PIN);\r\n\t}\r\n\r\n\tpublic byte[] readKey(String string) {\r\n\t\ttry {\r\n\t\t\tFile f = new File(\"/home/hap/\" + string);\r\n\t\t\tFileInputStream fis = new FileInputStream(f);\r\n\t\t\tDataInputStream dis = new DataInputStream(fis);\r\n\t\t\tbyte[] keyBytes = new byte[(int) f.length()];\r\n\t\t\tdis.readFully(keyBytes);\r\n\t\t\tdis.close();\r\n\r\n\t\t\treturn keyBytes;\r\n\t\t} catch (FileNotFoundException e) {\r\n\t\t\t// TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t} catch (IOException e) {\r\n\t\t\t// TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t\treturn null;\r\n\t}\r\n\r\n\tpublic void saveKey(String name, byte[] privateKey2) {\r\n\t\t// TODO Auto-generated method stub\r\n\t\tDataOutputStream keyfos;\r\n\t\ttry {\r\n\t\t\tkeyfos = new DataOutputStream(new FileOutputStream(\"/home/hap/\" + name, false));\r\n\r\n\t\t\tkeyfos.write(privateKey2);\r\n\t\t\tkeyfos.close();\r\n\t\t} catch (FileNotFoundException e) {\r\n\t\t\t// TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t} catch (IOException e) {\r\n\t\t\t// TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void loadUsers() {\r\n\r\n\t\ttry {\r\n\t\t\tInputStream resourceAsStream = new FileInputStream(new File(\"/home/hap/\" + \"usermap.xml\"));\r\n\t\t\tProperties props = new Properties();\r\n\r\n\t\t\tif (resourceAsStream != null) {\r\n\t\t\t\tSystem.out.println(\"Loading user map\");\r\n\t\t\t\tprops.loadFromXML(resourceAsStream);\r\n\r\n\t\t\t\tSet<Object> keySet = props.keySet();\r\n\r\n\t\t\t\tfor (Object keyObject : keySet) {\r\n\r\n\t\t\t\t\tString key = (String) keyObject;\r\n\r\n\t\t\t\t\tbyte[] publicKey = readKey(key);\r\n\t\t\t\t\tif (publicKey != null) {\r\n\t\t\t\t\t\tSystem.out.println(\"Loading \" + key + \" \" + new String(publicKey));\r\n\t\t\t\t\t\tuserKeyMap.putIfAbsent(key, publicKey);\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t} else {\r\n\t\t\t\tSystem.out.println(\"File empty\");\r\n\t\t\t}\r\n\t\t} catch (IOException e) {\r\n\t\t\t// TODO Auto-generated catch block\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void saveParamChanges(String mac, BigInteger salt, byte[] privateKey) {\r\n\t\ttry {\r\n\t\t\tProperties props = new Properties();\r\n\t\t\tprops.setProperty(\"mac\", mac);\r\n\t\t\tprops.setProperty(\"salt\", \"\" + new String(Base64.encodeBase64(salt.toByteArray())));\r\n\r\n\t\t\tFile f = new File(\"/home/hap/\" + \"server.properties\");\r\n\t\t\tOutputStream out = new FileOutputStream(f);\r\n\t\t\tprops.store(out, \"This is an optional header comment string\");\r\n\t\t\tSystem.out.println(\"SALT: \" + new BigInteger(Base64.decodeBase64((String) props.get(\"salt\"))));\r\n\t\t} catch (Exception e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\t}\r\n\r\n\tpublic void saveUserMap() {\r\n\t\ttry {\r\n\t\t\tProperties props = new Properties();\r\n\r\n\t\t\tSet<String> keySet = userKeyMap.keySet();\r\n\t\t\tfor (String key : keySet) {\r\n\t\t\t\tprops.setProperty(key, key);\r\n\r\n\t\t\t\tsaveKey(key, userKeyMap.get(key));\r\n\t\t\t}\r\n\r\n\t\t\tFile f = new File(\"/home/hap/\" + \"usermap.xml\");\r\n\t\t\tOutputStream out = new FileOutputStream(f);\r\n\t\t\tprops.storeToXML(out, \"This is an optional header comment string\");\r\n\t\t} catch (Exception e) {\r\n\t\t\te.printStackTrace();\r\n\t\t}\r\n\r\n\t}\r\n\r\n\t@Override\r\n\tpublic String getPin() {\r\n\t\treturn PIN;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic String getMac() {\r\n\t\treturn mac;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic BigInteger getSalt() {\r\n\t\treturn salt;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic byte[] getPrivateKey() {\r\n\t\treturn privateKey;\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void createUser(String username, byte[] publicKey) {\r\n\r\n\t\tif (userKeyMap.containsKey(username)) {\r\n\t\t\tSystem.out.println(\"Performing key update\");\r\n\t\t\tuserKeyMap.replace(username, publicKey);\r\n\t\t} else {\r\n\t\t\tSystem.out.println(\"Adding initial key\");\r\n\t\t\tuserKeyMap.putIfAbsent(username, publicKey);\r\n\t\t}\r\n\r\n\t\tsaveUserMap();\r\n\t\tSystem.out.println(\"Added pairing for \" + username + \" \" + new String(publicKey));\r\n\t}\r\n\r\n\t@Override\r\n\tpublic void removeUser(String username) {\r\n\t\tuserKeyMap.remove(username);\r\n\t\tsaveUserMap();\r\n\t\tSystem.out.println(\"Removed pairing for \" + username);\r\n\t}\r\n\r\n\t@Override\r\n\tpublic byte[] getUserPublicKey(String username) {\r\n\t\tSystem.out.println();\r\n\t\tbyte[] bs = userKeyMap.get(username);\r\n\t\tSystem.out.println(\"Requesting User:\" + username + \" \" + new String(bs));\r\n\r\n\t\treturn bs;\r\n\t}\r\n\r\n}"},"comment":{"url":"https://api.github.com/repos/beowulfe/HAP-Java/issues/comments/169327399","html_url":"https://github.com/beowulfe/HAP-Java/issues/8#issuecomment-169327399","issue_url":"https://api.github.com/repos/beowulfe/HAP-Java/issues/8","id":169327399,"user":{"login":"mickwheelz","id":5056807,"avatar_url":"https://avatars.githubusercontent.com/u/5056807?v=3","gravatar_id":"","url":"https://api.github.com/users/mickwheelz","html_url":"https://github.com/mickwheelz","followers_url":"https://api.github.com/users/mickwheelz/followers","following_url":"https://api.github.com/users/mickwheelz/following{/other_user}","gists_url":"https://api.github.com/users/mickwheelz/gists{/gist_id}","starred_url":"https://api.github.com/users/mickwheelz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mickwheelz/subscriptions","organizations_url":"https://api.github.com/users/mickwheelz/orgs","repos_url":"https://api.github.com/users/mickwheelz/repos","events_url":"https://api.github.com/users/mickwheelz/events{/privacy}","received_events_url":"https://api.github.com/users/mickwheelz/received_events","type":"User","site_admin":false},"created_at":"2016-01-06T13:49:55Z","updated_at":"2016-01-06T13:49:55Z","body":"I am having similar issues with persistance. I have attempted several ways to store the data on disk but each files. attached is my auth code\r\n\r\n\r\n\r\n[HomeKitAuth.txt](https://github.com/beowulfe/HAP-Java/files/79854/HomeKitAuth.txt)\r\n"}}