/** * Creates a CachedContentIndex which works on the index file in the given cacheDir. * * @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. * The key must be 16 bytes long. */ public CachedContentIndex(File cacheDir, byte[] secretKey) { if (secretKey != null) { Assertions.checkArgument(secretKey.length == 16); try { cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); secretKeySpec = new SecretKeySpec(secretKey, "AES"); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { throw new IllegalStateException(e); // Should never happen. } } else { cipher = null; secretKeySpec = null; } keyToContent = new HashMap<>(); idToKey = new SparseArray<>(); atomicFile = new AtomicFile(new File(cacheDir, FILE_NAME)); }
/** * 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 Whether the index will be encrypted when written. Must be false if {@code * secretKey} is null. */ public CachedContentIndex(File cacheDir, byte[] secretKey, boolean encrypt) { this.encrypt = encrypt; if (secretKey != null) { Assertions.checkArgument(secretKey.length == 16); try { cipher = getCipher(); secretKeySpec = new SecretKeySpec(secretKey, "AES"); } catch (NoSuchAlgorithmException | NoSuchPaddingException e) { throw new IllegalStateException(e); // Should never happen. } } else { Assertions.checkState(!encrypt); cipher = null; secretKeySpec = null; } keyToContent = new HashMap<>(); idToKey = new SparseArray<>(); atomicFile = new AtomicFile(new File(cacheDir, FILE_NAME)); }