use of com.google.nigori.common.NigoriMessages.GetResponse 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.NigoriMessages.GetResponse 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.NigoriMessages.GetResponse in project nigori by ucam-cl-dtg.
the class MessageLibrary method getResponseAsProtobuf.
public static GetResponse getResponseAsProtobuf(Collection<RevValue> revisions) {
// TODO(drt24) add index
List<RevisionValue> protoRevisions = new ArrayList<RevisionValue>(revisions.size());
for (RevValue rv : revisions) {
protoRevisions.add(RevisionValue.newBuilder().setRevision(ByteString.copyFrom(rv.getRevision().getBytes())).setValue(ByteString.copyFrom(rv.getValue())).build());
}
GetResponse resp = GetResponse.newBuilder().addAllRevisions(protoRevisions).build();
return resp;
}
Aggregations