{"_id":"553ea0104eb8a00d00309259","project":"553e9f6a6ab7691900ee7209","__v":10,"version":{"_id":"553e9f6a6ab7691900ee720c","__v":5,"project":"553e9f6a6ab7691900ee7209","createdAt":"2015-04-27T20:43:22.948Z","releaseDate":"2015-04-27T20:43:22.948Z","categories":["553e9f6b6ab7691900ee720d","553e9fca6ab7691900ee7210","553ea5456ab7691900ee7223","56fed4c349408d0e0043c6fb","5807b2ed2ce233230086a6dc"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"category":{"_id":"553e9fca6ab7691900ee7210","__v":2,"pages":["553ea0104eb8a00d00309259","553fc0ea6a7d010d0057c8c2"],"project":"553e9f6a6ab7691900ee7209","version":"553e9f6a6ab7691900ee720c","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-04-27T20:44:58.478Z","from_sync":false,"order":0,"slug":"textsecure-api","title":"Signal Service API"},"user":"553e9f0c6ab7691900ee7207","updates":["579e3cb2b0baa40e00a5e9f2","57ddd4ba27455f2b00f80c4c"],"next":{"pages":[],"description":""},"createdAt":"2015-04-27T20:46:08.253Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":999,"body":"## Getting the library\n\nFor Java projects:\n\n`````\n<dependency>\n  <groupId>org.whispersystems</groupId>\n  <artifactId>libsignal-service-java</artifactId>\n  <version>(latest version here)</version>\n</dependency>\n`````\n\nFor Android projects:\n\n`````\ncompile 'org.whispersystems:libsignal-service-android:(latest version here)'\n`````\n\n## Implementing the Signal Protocol interfaces\n\nSignal Protocol is a stateful protocol, so libsignal-service users\nneed to implement the storage interface `SignalProtocolStore`, which handles load/store\nof your key and session information to durable media.\n\n## Creating keys\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"IdentityKeyPair    identityKey        = KeyHelper.generateIdentityKeyPair();\\nList<PreKeyRecord> oneTimePreKeys     = KeyHelper.generatePreKeys(100);\\nPreKeyRecord       lastResortKey      = KeyHelper.generateLastResortKey();\\nSignedPreKeyRecord signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKey, signedPreKeyId);\",\n      \"language\": \"java\",\n      \"name\": null\n    }\n  ]\n}\n[/block]\nThe above keys are then stored locally so that they're available for load via the `SignalProtocolStore`.\n\n## Registering\n\nAt install time, clients need to register with the Signal server.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"private final String     URL         = \\\"https://my.signal.server.com\\\";\\nprivate final TrustStore TRUST_STORE = new MyTrustStoreImpl();\\nprivate final String     USERNAME    = \\\"+14151231234\\\";\\nprivate final String     PASSWORD    = generateRandomPassword();\\n\\nSignalServiceAccountManager accountManager = new SignalServiceAccountManager(URL, TRUST_STORE,USERNAME, PASSWORD);\\n\\naccountManager.requestSmsVerificationCode();\\naccountManager.verifyAccount(receivedSmsVerificationCode, generateRandomSignalingKey(), false, generateRandomInstallId());\\naccountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));\\naccountManager.setPreKeys(identityKey.getPublic(), lastResortKey, signedPreKey, oneTimePreKeys);\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n## Sending text messages\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, USERNAME, PASSWORD,\\n                                                                    localRecipientId, new MySignalProtocolStore(),\\n                                                                    Optional.absent());\\n\\nmessageSender.sendMessage(new SignalServiceAddress(\\\"+14159998888\\\"),\\n                          SignalServiceDataMessage.newBuilder()\\n                                                  .withBody(\\\"Hello, world!\\\")\\n                                                  .build());\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n## Sending media messages\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, USERNAME, PASSWORD,\\n                                                                    localRecipientId, new MySignalProtocolStore(),\\n                                                                    Optional.absent());\\n\\nFile                 myAttachment     = new File(\\\"/path/to/my.attachment\\\");\\nFileInputStream      attachmentStream = new FileInputStream(myAttachment);\\nSignalServiceAttachment attachment    = SignalServiceAttachment.newStreamBuilder()\\n                                                            .withStream(attachmentStream)\\n                                                            .withContentType(\\\"image/png\\\")\\n                                                            .withLength(myAttachment.size())\\n                                                            .build();\\n\\nmessageSender.sendMessage(new SignalServiceAddress(\\\"+14159998888\\\"),\\n                          SignalServiceDataMessage.newBuilder()\\n                                                  .withBody(\\\"An attachment!\\\")\\n                                                  .withAttachment(attachment)\\n                                                  .build());\\n\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]\n## Receiving messages\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, USERNAME, PASSWORD, mySignalingKey);\\nSignalServiceMessagePipe     messagePipe;\\n\\ntry {\\n  messagePipe = messageReciever.createMessagePipe();\\n\\n  while (listeningForMessages) {\\n    SignalServiceEnvelope envelope     = messagePipe.read(timeout, timeoutTimeUnit);\\n    SignalServiceCipher   cipher       = new SignalServiceCipher(new MySignalProtocolStore());\\n    SignalServiceDataMessage message   = cipher.decrypt(envelope);\\n\\n    System.out.println(\\\"Received message: \\\" + message.getBody().get());\\n  }\\n\\n} finally {\\n  if (messagePipe != null)\\n    messagePipe.close();\\n}\",\n      \"language\": \"java\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"textsecure-java-library","type":"basic","title":"Using The Java Library"}

Using The Java Library


## Getting the library For Java projects: ````` <dependency> <groupId>org.whispersystems</groupId> <artifactId>libsignal-service-java</artifactId> <version>(latest version here)</version> </dependency> ````` For Android projects: ````` compile 'org.whispersystems:libsignal-service-android:(latest version here)' ````` ## Implementing the Signal Protocol interfaces Signal Protocol is a stateful protocol, so libsignal-service users need to implement the storage interface `SignalProtocolStore`, which handles load/store of your key and session information to durable media. ## Creating keys [block:code] { "codes": [ { "code": "IdentityKeyPair identityKey = KeyHelper.generateIdentityKeyPair();\nList<PreKeyRecord> oneTimePreKeys = KeyHelper.generatePreKeys(100);\nPreKeyRecord lastResortKey = KeyHelper.generateLastResortKey();\nSignedPreKeyRecord signedPreKeyRecord = KeyHelper.generateSignedPreKey(identityKey, signedPreKeyId);", "language": "java", "name": null } ] } [/block] The above keys are then stored locally so that they're available for load via the `SignalProtocolStore`. ## Registering At install time, clients need to register with the Signal server. [block:code] { "codes": [ { "code": "private final String URL = \"https://my.signal.server.com\";\nprivate final TrustStore TRUST_STORE = new MyTrustStoreImpl();\nprivate final String USERNAME = \"+14151231234\";\nprivate final String PASSWORD = generateRandomPassword();\n\nSignalServiceAccountManager accountManager = new SignalServiceAccountManager(URL, TRUST_STORE,USERNAME, PASSWORD);\n\naccountManager.requestSmsVerificationCode();\naccountManager.verifyAccount(receivedSmsVerificationCode, generateRandomSignalingKey(), false, generateRandomInstallId());\naccountManager.setGcmId(Optional.of(GoogleCloudMessaging.getInstance(this).register(REGISTRATION_ID)));\naccountManager.setPreKeys(identityKey.getPublic(), lastResortKey, signedPreKey, oneTimePreKeys);", "language": "java" } ] } [/block] ## Sending text messages [block:code] { "codes": [ { "code": "SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, USERNAME, PASSWORD,\n localRecipientId, new MySignalProtocolStore(),\n Optional.absent());\n\nmessageSender.sendMessage(new SignalServiceAddress(\"+14159998888\"),\n SignalServiceDataMessage.newBuilder()\n .withBody(\"Hello, world!\")\n .build());", "language": "java" } ] } [/block] ## Sending media messages [block:code] { "codes": [ { "code": "SignalServiceMessageSender messageSender = new SignalServiceMessageSender(URL, TRUST_STORE, USERNAME, PASSWORD,\n localRecipientId, new MySignalProtocolStore(),\n Optional.absent());\n\nFile myAttachment = new File(\"/path/to/my.attachment\");\nFileInputStream attachmentStream = new FileInputStream(myAttachment);\nSignalServiceAttachment attachment = SignalServiceAttachment.newStreamBuilder()\n .withStream(attachmentStream)\n .withContentType(\"image/png\")\n .withLength(myAttachment.size())\n .build();\n\nmessageSender.sendMessage(new SignalServiceAddress(\"+14159998888\"),\n SignalServiceDataMessage.newBuilder()\n .withBody(\"An attachment!\")\n .withAttachment(attachment)\n .build());\n", "language": "java" } ] } [/block] ## Receiving messages [block:code] { "codes": [ { "code": "SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, USERNAME, PASSWORD, mySignalingKey);\nSignalServiceMessagePipe messagePipe;\n\ntry {\n messagePipe = messageReciever.createMessagePipe();\n\n while (listeningForMessages) {\n SignalServiceEnvelope envelope = messagePipe.read(timeout, timeoutTimeUnit);\n SignalServiceCipher cipher = new SignalServiceCipher(new MySignalProtocolStore());\n SignalServiceDataMessage message = cipher.decrypt(envelope);\n\n System.out.println(\"Received message: \" + message.getBody().get());\n }\n\n} finally {\n if (messagePipe != null)\n messagePipe.close();\n}", "language": "java" } ] } [/block]