Example #1
0
  /**
   * If the device has not previously generated and persisted its device ID (aka public/private
   * keypair for PSI), generates and stores said ID.
   *
   * <p>If the ID is already stored, this harmlessly does nothing.
   */
  private void generateAndStoreDeviceID(Context context, int encryption) {
    if (store == null) store = new StorageBase(context, encryption);
    String privateDeviceID = store.get(DEVICE_PRIVATE_ID_KEY);
    String publicDeviceID = store.get(DEVICE_PUBLIC_ID_KEY);
    if (privateDeviceID == null || publicDeviceID == null) {
      // This would be very strange, if only half the ID was stored.
      if (privateDeviceID != publicDeviceID) {
        if (privateDeviceID == null) {
          log.error(
              "Only one of private and public ID are stored! Public is stored, private is null.");
        } else {
          log.error(
              "Only one of private and public ID are stored! Private is stored, public is null.");
        }
      }

      AsymmetricCipherKeyPair keypair = Crypto.generateUserID();
      privateDeviceID = bytesToBase64(Crypto.generatePrivateID(keypair));
      publicDeviceID = bytesToBase64(Crypto.generatePublicID(keypair));

      store.put(DEVICE_PRIVATE_ID_KEY, privateDeviceID);
      store.put(DEVICE_PUBLIC_ID_KEY, publicDeviceID);
    }
  }
Example #2
0
 /**
  * Return the device's public device ID as a base64 encoded string, ready to be shared with
  * another device (e.g. as part of a QR code).
  *
  * @return A base64 encoded string representing the local device's public ID, or null if something
  *     went wrong.
  */
 public String getPublicDeviceIDString(Context context, int encryption) {
   generateAndStoreDeviceID(context, encryption);
   return store.get(DEVICE_PUBLIC_ID_KEY);
 }