<div dir="ltr"><div class="markdown-here-wrapper" id="markdown-here-wrapper-898051" style><a href="#" name="aerogear-android-crypto"></a><h1 id="aerogear-android-crypto" style="margin:1.3em 0px 1em;padding:0px;font-weight:bold;font-size:1.6em">
<span style="font-size:small;font-weight:normal">The idea is implementing only save(add/update) and delete, using the stores we already have today by just adding crypt / decrypt and scheduling Query (readWithFilter) for the next release</span><br>
</h1>
<p style="margin:1.2em 0px!important"><b>DataManager</b></p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline;white-space:pre;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;padding:0.5em;color:rgb(51,51,51);background-color:rgb(248,248,255);background-repeat:initial initial">public Store encryptedStore(String storeName, String passphrase) {
// TODO Create a default passphrase-based KeyStore
KeyStore keyStore = null;
return encryptedStore(storeName, keyStore);
}
public Store encryptedStore(String storeName, KeyStore keyStore) {
StoreConfig storeConfig = new StoreConfig();
storeConfig.setType(StoreTypes.ENCRYPTED_MEMORY);
return encryptedStore(storeName, storeConfig, keyStore);
}
public Store encryptedStore(String storeName, StoreConfig config, String passphrase) {
// TODO Create a default passphrase-based KeyStore
KeyStore keyStore = null;
return encryptedStore(storeName, config, keyStore);
}
public Store encryptedStore(String storeName, StoreConfig config, KeyStore keyStore) {
config.setKeyStore(keyStore);
Store store = storeFactory.createStore(config);
stores.put(storeName, store);
return store;
}</code></pre>
<p style="margin:1.2em 0px!important"><b>EncryptedMemoryStore</b></p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline;white-space:pre;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;padding:0.5em;color:rgb(51,51,51);background-color:rgb(248,248,255);background-repeat:initial initial">public class EncryptedMemoryStore<T> implements Store<T> {
private final MemoryStorage<T> memoryStorage;
private final CryptoUtils<T> cryptoUtils;
public EncryptedMemoryStore(IdGenerator idGenerator, KeyStore keyStore) {
memoryStorage = new MemoryStorage(idGenerator);
cryptoUtils = new CryptoUtils<T>(keyStore);
}
@Override
public StoreType getType() {
return StoreTypes.ENCRYPTED_MEMORY;
}
@Override
public Collection<T> readAll() throws InvalidKeyException {
Collection<T> encryptedCollection = memoryStorage.readAll();
return cryptoUtils.decrypt(encryptedCollection);
}
@Override
public T read(Serializable id) throws InvalidKeyException {
T encryptedItem = memoryStorage.read(id);
return cryptoUtils.decrypt(encryptedItem);
}
@Override
public List<T> readWithFilter(ReadFilter filter) throws InvalidKeyException {
List<T> encryptedList = memoryStorage.readWithFilter(filter);
return cryptoUtils.decrypt(encryptedList);
}
@Override
public void save(T item) {
memoryStorage.save(cryptoUtils.encrypt(item));
}
@Override
public void reset() {
memoryStorage.reset();
}
@Override
public void remove(Serializable id) {
memoryStorage.remove(id);
}
@Override
public boolean isEmpty() {
return memoryStorage.isEmpty();
}
}</code></pre>
<p style="margin:1.2em 0px!important"><b>CryptoUtils</b> <br><br>Here is where the magic happens.</p>
<pre style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;font-size:1em;line-height:1.2em;margin:1.2em 0px"><code style="font-size:0.85em;font-family:Consolas,Inconsolata,Courier,monospace;margin:0px 0.15em;padding:0px 0.3em;white-space:pre-wrap;border:1px solid rgb(234,234,234);background-color:rgb(248,248,248);border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;display:inline;white-space:pre;overflow:auto;border-top-left-radius:3px;border-top-right-radius:3px;border-bottom-right-radius:3px;border-bottom-left-radius:3px;border:1px solid rgb(204,204,204);padding:0.5em 0.7em;display:block!important;display:block;padding:0.5em;color:rgb(51,51,51);background-color:rgb(248,248,255);background-repeat:initial initial">
public class CryptoUtils<T> {
private final KeyStore keyStore;
public CryptoUtils(KeyStore keyStore) {
this.keyStore = keyStore;
}
public Collection<T> decrypt(Collection<T> encryptedCollection) {
List<T> decryptedList = new ArrayList<T>();
for (T item : encryptedCollection) {
decryptedList.add(decrypt(item));
}
return decryptedList;
}
public List<T> decrypt(List<T> encryptedList) {
List<T> decryptedList = new ArrayList<T>();
for (T item : encryptedList) {
decryptedList.add(decrypt(item));
}
return decryptedList;
}
public T decrypt(T item) {
// TODO Read all fields and decrypt
return item;
}
public T encrypt(T item) {
// TODO Read all fields and encrypt
return item;
}
}</code></pre>
</div></div>