diff options
Diffstat (limited to 'src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier')
5 files changed, 0 insertions, 194 deletions
diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierProvider.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierProvider.java deleted file mode 100644 index 3fbe284..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/FileIdentifierProvider.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.incremental.identifier; - -import java.io.File; -import java.io.IOException; - -public interface FileIdentifierProvider<T extends ObjectIdentifier> { - /** - * Generate file identifier from a random file. The file is not necessarily in the object base. - * - * @param file the file. - * @return the file identifier. - * @throws IOException when an I/O error occurs. - */ - T fromFile(File file) throws IOException; -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java deleted file mode 100644 index aece07d..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/ObjectIdentifier.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.incremental.identifier; - -import java.io.Serializable; - -/** - * The identifier distinguishing files in the object collection. - * It should be based on cryptographic hash function in order to prevent possible attacks to the backup system. - * All identifiers should be immutable and implement their own equals method. - * Immutable. - */ -public interface ObjectIdentifier extends Serializable { - String getIdentification(); -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java deleted file mode 100644 index c1c87e1..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/Sha256Identifier.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.incremental.identifier; - -import com.keuin.kbackupfabric.util.BytesUtil; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.Objects; - -/** - * Identifier based on sha256. - * Immutable. - */ -public class Sha256Identifier extends SingleHashIdentifier { - - private static final int SHA256_LENGTH = 32; - private static final Sha256Identifier DUMMY = new Sha256Identifier(new byte[SHA256_LENGTH]); // only for using its hash method - private static final FileIdentifierProvider<Sha256Identifier> factory = Sha256Identifier::fromFile; - private static final String marker = "S2"; - - public static Sha256Identifier fromFile(File file) throws IOException { - if (!file.isFile()) { - throw new IllegalArgumentException("file is not a file"); - } - return new Sha256Identifier(DUMMY.hash(file)); - } - - /** - * Load sha-256 from a named file. Only used in StorageObjectLoader. - * - * @param fileName the file name. - * @return identifier. - */ - static Sha256Identifier fromFileName(String fileName) { - if (!fileName.matches(marker + "-[0-9A-Fa-f]{32}")) - return null; - String hexString = fileName.substring(marker.length() + 1); - return new Sha256Identifier(BytesUtil.hexToBytes(hexString)); - } - - public static FileIdentifierProvider<Sha256Identifier> getFactory() { - return factory; - } - - protected Sha256Identifier(byte[] hash) { - super(hash, marker); - Objects.requireNonNull(hash); - if (hash.length != SHA256_LENGTH) { - throw new IllegalStateException(String.format("SHA256 must be %d bytes", SHA256_LENGTH)); - } - } - - @Override - protected byte[] hash(File file) throws IOException { - try { - MessageDigest digest = MessageDigest.getInstance("SHA-256"); - - try (FileInputStream inputStream = new FileInputStream(file)) { - // This does not work. I don't know why -// FileChannel channel = inputStream.getChannel(); -// ByteBuffer buffer = ByteBuffer.allocate(128); -// int readLength; -// while ((readLength = channel.read(buffer)) > 0) -// digest.update(buffer); - - // This also works, without warnings - byte[] readBuffer = new byte[1024 * 1024]; - int readLength; - while ((readLength = inputStream.read(readBuffer)) > 0) - digest.update(readBuffer, 0, readLength); - - // The below lines also works, but the IDE will complain about the while loop -// DigestInputStream digestInputStream = new DigestInputStream(inputStream, digest); -// while(digestInputStream.read() > 0) -// ; - - return digest.digest(); - } - - } catch (NoSuchAlgorithmException ignored) { - // this shouldn't happen - return new byte[SHA256_LENGTH]; - } - } - -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java deleted file mode 100644 index 0f62f2b..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/SingleHashIdentifier.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.incremental.identifier; - -import com.keuin.kbackupfabric.util.BytesUtil; - -import java.io.File; -import java.io.IOException; -import java.util.Arrays; -import java.util.Objects; - -/** - * A simple identifier based on a single hash function. - * Immutable. - */ -public abstract class SingleHashIdentifier implements ObjectIdentifier { - - private final byte[] hash; - private final String type; - - protected SingleHashIdentifier(byte[] hash, String type) { - Objects.requireNonNull(hash); - Objects.requireNonNull(type); - this.hash = Arrays.copyOf(hash, hash.length); - this.type = type; - } - - /** - * The hash function. - * - * @param file the file to be hashed. - * @return the hash bytes. - */ - protected abstract byte[] hash(File file) throws IOException; - - @Override - public String getIdentification() { - return type + "-" + BytesUtil.bytesToHex(hash); - } - - @Override - public boolean equals(Object obj) { - if (!(obj instanceof SingleHashIdentifier)) { - return false; - } - return Arrays.equals(hash, ((SingleHashIdentifier) obj).hash); - } - - @Override - public int hashCode() { - int result = Objects.hash(type); - result = 31 * result + Arrays.hashCode(hash); - return result; - } -} diff --git a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/StorageObjectLoader.java b/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/StorageObjectLoader.java deleted file mode 100644 index 96bc295..0000000 --- a/src/main/java/com/keuin/kbackupfabric/util/backup/incremental/identifier/StorageObjectLoader.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.keuin.kbackupfabric.util.backup.incremental.identifier; - -import java.io.File; -import java.util.Objects; - -public class StorageObjectLoader { - /** - * Get identifier from storage file. - * - * @param file storage file. - * @return identifier. If failed, return null. - */ - public static ObjectIdentifier asIdentifier(File file) { - Objects.requireNonNull(file); - String fileName = file.getName(); - ObjectIdentifier identifier; - - identifier = Sha256Identifier.fromFileName(fileName); - if (identifier != null) - return identifier; - - // Add more identifiers. - return null; - } -} |