/** Return a base64-encoded string value as a byte array. */
 public byte[] getByteArrayBase64(String key) {
   String s = (String) this.object.get(key);
   if (s == null) {
     return null;
   }
   return Base64.decodeBase64(s);
 }
  @Test
  public void testSetKeysFromWBO()
      throws IOException, ParseException, NonObjectJSONException, CryptoException,
          NoCollectionKeysSetException {
    String json =
        "{\"default\":[\"3fI6k1exImMgAKjilmMaAWxGqEIzFX/9K5EjEgH99vc=\",\"/AMaoCX4hzic28WY94XtokNi7N4T0nv+moS1y5wlbug=\"],\"collections\":{},\"collection\":\"crypto\",\"id\":\"keys\"}";
    CryptoRecord rec = new CryptoRecord(json);

    KeyBundle syncKeyBundle =
        new KeyBundle("slyjcrjednxd6rf4cr63vqilmkus6zbe", "6m8mv8ex2brqnrmsb9fjuvfg7y");
    rec.keyBundle = syncKeyBundle;

    rec.encrypt();
    CollectionKeys ck = new CollectionKeys();
    ck.setKeyPairsFromWBO(rec, syncKeyBundle);
    byte[] input = "3fI6k1exImMgAKjilmMaAWxGqEIzFX/9K5EjEgH99vc=".getBytes("UTF-8");
    byte[] expected = Base64.decodeBase64(input);
    assertSame(expected, ck.defaultKeyBundle().getEncryptionKey());
  }
  @Test
  public void testCryptoRecordFromCollectionKeys()
      throws CryptoException, NoCollectionKeysSetException, IOException, ParseException,
          NonObjectJSONException {
    CollectionKeys ck1 = CollectionKeys.generateCollectionKeys();
    assertNotNull(ck1.defaultKeyBundle());
    assertEquals(ck1.keyBundleForCollection("foobar"), ck1.defaultKeyBundle());
    CryptoRecord rec = ck1.asCryptoRecord();
    assertEquals(rec.collection, "crypto");
    assertEquals(rec.guid, "keys");
    JSONArray defaultKey = (JSONArray) rec.payload.get("default");

    assertSame(
        Base64.decodeBase64((String) (defaultKey.get(0))),
        ck1.defaultKeyBundle().getEncryptionKey());
    CollectionKeys ck2 = new CollectionKeys();
    ck2.setKeyPairsFromWBO(rec, null);
    assertSame(
        ck1.defaultKeyBundle().getEncryptionKey(), ck2.defaultKeyBundle().getEncryptionKey());
  }
Example #4
0
 public static String generateGuid() {
   byte[] encodedBytes = Base64.encodeBase64(generateRandomBytes(9), false);
   return new String(encodedBytes).replace("+", "-").replace("/", "_");
 }
Example #5
0
 /**
  * Utility for Base64 decoding. Should ensure that the correct Apache Commons version is used.
  *
  * @param base64 An input string. Will be decoded as UTF-8.
  * @return A byte array of decoded values.
  * @throws UnsupportedEncodingException Should not occur.
  */
 public static byte[] decodeBase64(String base64) throws UnsupportedEncodingException {
   return Base64.decodeBase64(base64.getBytes("UTF-8"));
 }