public NorvosPreKeyStore() { lastResortKey = KeyHelper.generateLastResortPreKey(); oneTimePreKeys = new CircularBuffer<PreKeyRecord>(Medium.MAX_VALUE); Random r = new Random(); List<PreKeyRecord> list = KeyHelper.generatePreKeys(r.nextInt(Medium.MAX_VALUE), 100); for (PreKeyRecord key : list) { oneTimePreKeys.add(key.getId(), key); } }
public synchronized byte[] serialize() { Builder builder = PreKeyStoreStructure.newBuilder(); for (PreKeyRecord entry : oneTimePreKeys.getAll()) { PreKeyStructure struct = PreKeyStructure.newBuilder() .setKeyId(entry.getId()) .setPreKeyRecord(ByteString.copyFrom(entry.serialize())) .build(); builder.addOneTimePreKey(struct); } return builder.build().toByteArray(); }
@Override public void storePreKey(int preKeyId, PreKeyRecord record) { synchronized (FILE_LOCK) { try { storeSerializedRecord(getPreKeyFile(preKeyId), record.serialize()); } catch (IOException e) { throw new AssertionError(e); } } }
public void storeKey(final int id, final PreKeyRecord preKey) throws SQLException { final String query = "MERGE INTO prekeystore VALUES (?, ?)"; try (PreparedStatement stmt = Database.ensureTableExists(this).prepareStatement(query)) { stmt.setInt(1, id); stmt.setBytes(2, preKey.serialize()); stmt.execute(); } }