mirror of
https://github.com/zhigang1992/AndroidVideoCache.git
synced 2026-03-06 17:04:24 +08:00
draft for encryption
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.danikula.videocache;
|
||||
|
||||
import com.danikula.videocache.encrypt.Cipher;
|
||||
import com.danikula.videocache.file.DiskUsage;
|
||||
import com.danikula.videocache.file.FileNameGenerator;
|
||||
import com.danikula.videocache.headers.HeaderInjector;
|
||||
@@ -19,13 +20,16 @@ class Config {
|
||||
public final DiskUsage diskUsage;
|
||||
public final SourceInfoStorage sourceInfoStorage;
|
||||
public final HeaderInjector headerInjector;
|
||||
public final Cipher cipher;
|
||||
|
||||
Config(File cacheRoot, FileNameGenerator fileNameGenerator, DiskUsage diskUsage, SourceInfoStorage sourceInfoStorage, HeaderInjector headerInjector) {
|
||||
Config(File cacheRoot, FileNameGenerator fileNameGenerator, DiskUsage diskUsage, SourceInfoStorage sourceInfoStorage,
|
||||
HeaderInjector headerInjector, Cipher cipher) {
|
||||
this.cacheRoot = cacheRoot;
|
||||
this.fileNameGenerator = fileNameGenerator;
|
||||
this.diskUsage = diskUsage;
|
||||
this.sourceInfoStorage = sourceInfoStorage;
|
||||
this.headerInjector = headerInjector;
|
||||
this.cipher = cipher;
|
||||
}
|
||||
|
||||
File generateCacheFile(String url) {
|
||||
|
||||
@@ -3,6 +3,8 @@ package com.danikula.videocache;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
|
||||
import com.danikula.videocache.encrypt.Cipher;
|
||||
import com.danikula.videocache.encrypt.NoCipher;
|
||||
import com.danikula.videocache.file.DiskUsage;
|
||||
import com.danikula.videocache.file.FileNameGenerator;
|
||||
import com.danikula.videocache.file.Md5FileNameGenerator;
|
||||
@@ -353,6 +355,7 @@ public class HttpProxyCacheServer {
|
||||
private DiskUsage diskUsage;
|
||||
private SourceInfoStorage sourceInfoStorage;
|
||||
private HeaderInjector headerInjector;
|
||||
private Cipher cipher;
|
||||
|
||||
public Builder(Context context) {
|
||||
this.sourceInfoStorage = SourceInfoStorageFactory.newSourceInfoStorage(context);
|
||||
@@ -360,6 +363,7 @@ public class HttpProxyCacheServer {
|
||||
this.diskUsage = new TotalSizeLruDiskUsage(DEFAULT_MAX_SIZE);
|
||||
this.fileNameGenerator = new Md5FileNameGenerator();
|
||||
this.headerInjector = new EmptyHeadersInjector();
|
||||
this.cipher = new NoCipher();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -390,6 +394,17 @@ public class HttpProxyCacheServer {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Overrides default cipher for cached files.
|
||||
*
|
||||
* @param cipher a new cipher, can't be {@code null}.
|
||||
* @return a builder.
|
||||
*/
|
||||
public Builder cipher(Cipher cipher) {
|
||||
this.cipher = checkNotNull(cipher);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets max cache size in bytes.
|
||||
* <p>
|
||||
@@ -452,8 +467,7 @@ public class HttpProxyCacheServer {
|
||||
}
|
||||
|
||||
private Config buildConfig() {
|
||||
return new Config(cacheRoot, fileNameGenerator, diskUsage, sourceInfoStorage, headerInjector);
|
||||
return new Config(cacheRoot, fileNameGenerator, diskUsage, sourceInfoStorage, headerInjector, cipher);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.danikula.videocache.encrypt;
|
||||
|
||||
/**
|
||||
* Defines a encryption/decryption for cached data.
|
||||
*
|
||||
* @author Alexey Danilov (danikula@gmail.com).
|
||||
*/
|
||||
public interface Cipher {
|
||||
|
||||
/**
|
||||
* Encrypts piece of data. Resulting byte[] array must have <b>same size<b/>.
|
||||
*
|
||||
* @param data an input data to be encrypted.
|
||||
* @param length an length of data to be encrypted.
|
||||
*/
|
||||
void encrypt(byte[] data, long length);
|
||||
|
||||
/**
|
||||
* Decrypts piece of data. Resulting byte[] array must have <b>same size<b/>.
|
||||
*
|
||||
* @param data an input data to be decrypted.
|
||||
* @param offset an offset of data to be decrypted.
|
||||
* @param length a length of data to be decrypted.
|
||||
*/
|
||||
void decrypt(byte[] data, long offset, int length);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package com.danikula.videocache.encrypt;
|
||||
|
||||
import com.danikula.videocache.Cache;
|
||||
import com.danikula.videocache.ProxyCacheException;
|
||||
|
||||
import static com.danikula.videocache.Preconditions.checkNotNull;
|
||||
|
||||
/**
|
||||
* {@link Cache} that take into account encryption/decryption.
|
||||
*
|
||||
* @author Alexey Danilov (danikula@gmail.com).
|
||||
*/
|
||||
public class CipherAwareCacheWrapper implements Cache {
|
||||
|
||||
private final Cache cache;
|
||||
private final Cipher cipher;
|
||||
|
||||
public CipherAwareCacheWrapper(Cache cache, Cipher cipher) {
|
||||
this.cache = checkNotNull(cache);
|
||||
this.cipher = checkNotNull(cipher);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer, long offset, int length) throws ProxyCacheException {
|
||||
cache.read(buffer, offset, length);
|
||||
cipher.decrypt(buffer, offset, length);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void append(byte[] data, int length) throws ProxyCacheException {
|
||||
cipher.encrypt(data, length);
|
||||
cache.append(data, length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long available() throws ProxyCacheException {
|
||||
return cache.available();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws ProxyCacheException {
|
||||
cache.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void complete() throws ProxyCacheException {
|
||||
cache.complete();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCompleted() {
|
||||
return cache.isCompleted();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.danikula.videocache.encrypt;
|
||||
|
||||
/**
|
||||
* {@link Cipher} that does not perform any encryption/decryption.
|
||||
*
|
||||
* @author Alexey Danilov (danikula@gmail.com).
|
||||
*/
|
||||
public class NoCipher implements Cipher {
|
||||
|
||||
@Override
|
||||
public void encrypt(byte[] data, long length) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void decrypt(byte[] data, long offset, int length) {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user