use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.
the class CryptoNigoriDatastore method get.
/**
* Retrieve the value associated with {@code index} on the server.
*
* @param index
* @param revision
* @return a list of RevValues containing the data associated with {@code index} or {@code null}
* if no data exists. WARNING: there is no assurance that the value for the revision is a
* pair once specified by a valid client - the server can pair any value with any
* revision.
*/
private List<RevValue> get(byte[] encKey, Index index, Revision revision) throws IOException, NigoriCryptographyException, UnauthorisedException {
byte[] encIndex;
byte[] encRevision = null;
if (encKey == null) {
encIndex = keyManager.encryptDeterministically(index.getBytes());
if (revision != null) {
encRevision = keyManager.encryptDeterministically(revision.getBytes());
}
} else {
encIndex = keyManager.encryptDeterministically(encKey, index.getBytes());
if (revision != null) {
encRevision = keyManager.encryptDeterministically(encKey, revision.getBytes());
}
}
try {
GetResponse getResponse = protocol.get(MessageLibrary.getRequestAsProtobuf(keyManager.getServerName(), keyManager.signer(), encIndex, encRevision));
if (getResponse == null) {
return null;
}
List<RevisionValue> revisions = getResponse.getRevisionsList();
List<RevValue> answer = new ArrayList<RevValue>(revisions.size());
for (RevisionValue revisionValue : revisions) {
byte[] revisionciphertext = revisionValue.getRevision().toByteArray();
byte[] valueciphertext = revisionValue.getValue().toByteArray();
if (encKey == null) {
answer.add(new RevValue(keyManager.decrypt(revisionciphertext), keyManager.decrypt(valueciphertext)));
} else {
answer.add(new RevValue(keyManager.decrypt(encKey, revisionciphertext), keyManager.decrypt(encKey, valueciphertext)));
}
}
return answer;
} catch (NotFoundException e) {
return null;
}
}
use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.
the class JEDatabase method getRevision.
@Override
public RevValue getRevision(User user, byte[] key, byte[] revision) throws IOException {
try {
DatabaseEntry value = new DatabaseEntry();
OperationStatus status = db.get(null, makeValueKey(makeLookupBytes(user, key), revision), value, null);
if (OperationStatus.SUCCESS == status) {
return new RevValue(revision, value.getData());
}
return null;
} catch (DatabaseException e) {
throw new IOException(e);
}
}
use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.
the class SQLDatabase method getRecord.
@Override
public Collection<RevValue> getRecord(User user, byte[] key) throws IOException {
try {
PreparedStatement queryStatement = con.prepareStatement("SELECT rev, val FROM rev_values, lookups, stores WHERE rev_values.lid = lookups.lid AND lookups.lookup = ? AND lookups.sid = stores.sid AND stores.ph =?");
try {
queryStatement.setBytes(1, key);
queryStatement.setBytes(2, user.getPublicHash());
ResultSet set = queryStatement.executeQuery();
List<RevValue> revValues = new ArrayList<RevValue>();
while (set.next()) {
byte[] rev = set.getBytes("rev");
byte[] value = set.getBytes("val");
revValues.add(new RevValue(rev, value));
}
return revValues;
} finally {
queryStatement.close();
}
} catch (SQLException e) {
throw new IOException(e);
}
}
use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.
the class NigoriServletTest method testGetRequestKeyExists.
@Test
public void testGetRequestKeyExists() throws Exception {
final byte[] key = toBytes("a key");
final byte[] revision = toBytes("a revision");
final byte[] value = toBytes("a value");
final byte[] publicHash = keyManager.signer().getPublicHash();
final String jsonGet = MessageLibrary.getRequestAsJson(serverName, keyManager.signer(), key, null);
expectedCallsForJsonRequest(jsonGet, MessageLibrary.REQUEST_GET);
expectedCallsToAuthenticateUser(publicHash);
expect(database.getRecord(eq(user), aryEq(key))).andReturn(Arrays.asList(new RevValue[] { new RevValue(revision, value) }));
ServletOutputStream out = expectedCallsForJsonResponse();
Capture<byte[]> result = new Capture<byte[]>();
Capture<Integer> size = new Capture<Integer>();
out.write(capture(result), eq(0), capture(size));
out.flush();
runReplayVerifyWithDoPost(out);
String jsonResponse = new String(result.getValue(), 0, size.getValue(), MessageLibrary.CHARSET);
GetResponse getResponse = MessageLibrary.getResponseFromJson(jsonResponse);
List<RevisionValue> revs = getResponse.getRevisionsList();
assertEquals(1, revs.size());
for (RevisionValue rev : revs) {
assertArrayEquals(revision, rev.getRevision().toByteArray());
assertArrayEquals(value, rev.getValue().toByteArray());
}
}
use of com.google.nigori.common.RevValue in project nigori by ucam-cl-dtg.
the class MConcurrencyTest method deterministicEqual.
@Test
public void deterministicEqual() throws NigoriCryptographyException, IOException, UnauthorisedException {
failed = false;
Thread[] threads = new Thread[THREADS];
final MigoriDatastore migori = getStore();
final Index index = new Index("Concurrency");
assertTrue("Not registered", migori.register());
try {
final List<Throwable> exceptionList = Collections.synchronizedList(new LinkedList<Throwable>());
for (int j = 0; j < THREADS; ++j) {
threads[j] = new Thread() {
@Override
public void run() {
boolean succeeded = false;
try {
RevValue last = migori.put(index, Util.int2bin(0));
for (int i = 0; i < REPEATS; ++i) {
last = migori.put(index, Util.int2bin(i), last);
}
succeeded = true;
} catch (Throwable e) {
exceptionList.add(e);
} finally {
if (!succeeded && !failed) {
failed = true;
}
}
}
};
}
startThenJoinThreads(threads);
ifFailedPrintFailures(failed, exceptionList);
Collection<RevValue> heads = migori.get(index);
assertEquals(1, heads.size());
RevValue head = heads.toArray(new RevValue[1])[0];
int total = Util.bin2int(head.getValue(), 0);
assertEquals(REPEATS - 1, total);
assertTrue(migori.removeIndex(index, head.getRevision()));
} finally {
assertTrue("Not unregistered", migori.unregister());
}
}
Aggregations