Add flag to CachedContentIndex to disable encryption. This allows the encryption feature
to be disabled gracefully: encrypted index files may be read, but plaintext will be written. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=165196508
This commit is contained in:
parent
c9393db878
commit
d9cd4641f2
@ -64,6 +64,7 @@ import javax.crypto.spec.SecretKeySpec;
|
|||||||
private final AtomicFile atomicFile;
|
private final AtomicFile atomicFile;
|
||||||
private final Cipher cipher;
|
private final Cipher cipher;
|
||||||
private final SecretKeySpec secretKeySpec;
|
private final SecretKeySpec secretKeySpec;
|
||||||
|
private final boolean encrypt;
|
||||||
private boolean changed;
|
private boolean changed;
|
||||||
private ReusableBufferedOutputStream bufferedOutputStream;
|
private ReusableBufferedOutputStream bufferedOutputStream;
|
||||||
|
|
||||||
@ -80,10 +81,21 @@ import javax.crypto.spec.SecretKeySpec;
|
|||||||
* Creates a CachedContentIndex which works on the index file in the given cacheDir.
|
* Creates a CachedContentIndex which works on the index file in the given cacheDir.
|
||||||
*
|
*
|
||||||
* @param cacheDir Directory where the index file is kept.
|
* @param cacheDir Directory where the index file is kept.
|
||||||
* @param secretKey If not null, cache keys will be stored encrypted on filesystem using AES/CBC.
|
* @param secretKey 16 byte AES key for reading and writing the cache index.
|
||||||
* The key must be 16 bytes long.
|
|
||||||
*/
|
*/
|
||||||
public CachedContentIndex(File cacheDir, byte[] secretKey) {
|
public CachedContentIndex(File cacheDir, byte[] secretKey) {
|
||||||
|
this(cacheDir, secretKey, secretKey != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a CachedContentIndex which works on the index file in the given cacheDir.
|
||||||
|
*
|
||||||
|
* @param cacheDir Directory where the index file is kept.
|
||||||
|
* @param secretKey 16 byte AES key for reading, and optionally writing, the cache index.
|
||||||
|
* @param encrypt When false, a plaintext index will be written.
|
||||||
|
*/
|
||||||
|
public CachedContentIndex(File cacheDir, byte[] secretKey, boolean encrypt) {
|
||||||
|
this.encrypt = encrypt;
|
||||||
if (secretKey != null) {
|
if (secretKey != null) {
|
||||||
Assertions.checkArgument(secretKey.length == 16);
|
Assertions.checkArgument(secretKey.length == 16);
|
||||||
try {
|
try {
|
||||||
@ -288,10 +300,11 @@ import javax.crypto.spec.SecretKeySpec;
|
|||||||
output = new DataOutputStream(bufferedOutputStream);
|
output = new DataOutputStream(bufferedOutputStream);
|
||||||
output.writeInt(VERSION);
|
output.writeInt(VERSION);
|
||||||
|
|
||||||
int flags = cipher != null ? FLAG_ENCRYPTED_INDEX : 0;
|
boolean writeEncrypted = encrypt && cipher != null;
|
||||||
|
int flags = writeEncrypted ? FLAG_ENCRYPTED_INDEX : 0;
|
||||||
output.writeInt(flags);
|
output.writeInt(flags);
|
||||||
|
|
||||||
if (cipher != null) {
|
if (writeEncrypted) {
|
||||||
byte[] initializationVector = new byte[16];
|
byte[] initializationVector = new byte[16];
|
||||||
new Random().nextBytes(initializationVector);
|
new Random().nextBytes(initializationVector);
|
||||||
output.write(initializationVector);
|
output.write(initializationVector);
|
||||||
|
@ -60,10 +60,24 @@ public final class SimpleCache implements Cache {
|
|||||||
* The key must be 16 bytes long.
|
* The key must be 16 bytes long.
|
||||||
*/
|
*/
|
||||||
public SimpleCache(File cacheDir, CacheEvictor evictor, byte[] secretKey) {
|
public SimpleCache(File cacheDir, CacheEvictor evictor, byte[] secretKey) {
|
||||||
|
this(cacheDir, evictor, secretKey, secretKey != null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs the cache. The cache will delete any unrecognized files from the directory. Hence
|
||||||
|
* the directory cannot be used to store other files.
|
||||||
|
*
|
||||||
|
* @param cacheDir A dedicated cache directory.
|
||||||
|
* @param evictor The evictor to be used.
|
||||||
|
* @param secretKey If not null, cache keys will be stored encrypted on filesystem using AES/CBC.
|
||||||
|
* The key must be 16 bytes long.
|
||||||
|
* @param encrypt When false, a plaintext index will be written.
|
||||||
|
*/
|
||||||
|
public SimpleCache(File cacheDir, CacheEvictor evictor, byte[] secretKey, boolean encrypt) {
|
||||||
this.cacheDir = cacheDir;
|
this.cacheDir = cacheDir;
|
||||||
this.evictor = evictor;
|
this.evictor = evictor;
|
||||||
this.lockedSpans = new HashMap<>();
|
this.lockedSpans = new HashMap<>();
|
||||||
this.index = new CachedContentIndex(cacheDir, secretKey);
|
this.index = new CachedContentIndex(cacheDir, secretKey, encrypt);
|
||||||
this.listeners = new HashMap<>();
|
this.listeners = new HashMap<>();
|
||||||
// Start cache initialization.
|
// Start cache initialization.
|
||||||
final ConditionVariable conditionVariable = new ConditionVariable();
|
final ConditionVariable conditionVariable = new ConditionVariable();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user