-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from CodeDead/feature/upgrades
Feature/upgrades
- Loading branch information
Showing
52 changed files
with
400 additions
and
256 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
76 changes: 76 additions & 0 deletions
76
app/src/main/java/com/codedead/deadhash/domain/objects/hashgenerator/FileHashGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.codedead.deadhash.domain.objects.hashgenerator; | ||
|
||
import android.content.ContentResolver; | ||
import android.net.Uri; | ||
|
||
import com.codedead.deadhash.domain.utils.HashUtil; | ||
|
||
import java.util.List; | ||
|
||
public final class FileHashGenerator extends HashGenerator { | ||
|
||
private final Uri uri; | ||
private final ContentResolver contentResolver; | ||
|
||
/** | ||
* Initialize a new HashGenerator | ||
* | ||
* @param uri The Uri of the file that should be hashed | ||
* @param hashAlgorithms The List of HashingAlgorithm enums that should be used to calculate hashes | ||
* @param compare The compare String for the calculated hashes | ||
*/ | ||
public FileHashGenerator(final Uri uri, final ContentResolver contentResolver, final List<HashAlgorithm> hashAlgorithms, final String compare) { | ||
super(hashAlgorithms, compare); | ||
|
||
if (uri == null) | ||
throw new NullPointerException("File cannot be null!"); | ||
if (contentResolver == null) | ||
throw new NullPointerException("ContentResolver cannot be null!"); | ||
|
||
this.uri = uri; | ||
this.contentResolver = contentResolver; | ||
} | ||
|
||
/** | ||
* Generate the List of HashData for the given input data | ||
* | ||
* @return The List of HashData for the given input data | ||
*/ | ||
@Override | ||
public List<HashData> generateHashes() { | ||
for (final HashAlgorithm algorithm : super.getHashAlgorithms()) { | ||
switch (algorithm) { | ||
case md5 -> { | ||
final String md5 = HashUtil.calculateHash(uri, contentResolver, "MD5"); | ||
getHashData().add(new HashData("MD5", md5, getCompare())); | ||
} | ||
case sha1 -> { | ||
final String sha1 = HashUtil.calculateHash(uri, contentResolver, "SHA-1"); | ||
getHashData().add(new HashData("SHA-1", sha1, getCompare())); | ||
} | ||
case sha224 -> { | ||
final String sha224 = HashUtil.calculateHash(uri, contentResolver, "SHA-224"); | ||
getHashData().add(new HashData("SHA-224", sha224, getCompare())); | ||
} | ||
case sha256 -> { | ||
final String sha256 = HashUtil.calculateHash(uri, contentResolver, "SHA-256"); | ||
getHashData().add(new HashData("SHA-256", sha256, getCompare())); | ||
} | ||
case sha384 -> { | ||
final String sha384 = HashUtil.calculateHash(uri, contentResolver, "SHA-384"); | ||
getHashData().add(new HashData("SHA-384", sha384, getCompare())); | ||
} | ||
case sha512 -> { | ||
final String sha512 = HashUtil.calculateHash(uri, contentResolver, "SHA-512"); | ||
getHashData().add(new HashData("SHA-512", sha512, getCompare())); | ||
} | ||
case crc32 -> { | ||
final String crc32 = HashUtil.calculateCRC32(uri, contentResolver); | ||
getHashData().add(new HashData("CRC32", crc32, getCompare())); | ||
} | ||
} | ||
} | ||
|
||
return getHashData(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 15 additions & 83 deletions
98
app/src/main/java/com/codedead/deadhash/domain/objects/hashgenerator/HashGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,117 +1,49 @@ | ||
package com.codedead.deadhash.domain.objects.hashgenerator; | ||
|
||
import com.codedead.deadhash.domain.utils.HashUtil; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public final class HashGenerator { | ||
|
||
private final byte[] data; | ||
public abstract class HashGenerator implements IHashGenerator { | ||
private final List<HashAlgorithm> hashAlgorithms; | ||
private final List<HashData> hashData; | ||
private final String compare; | ||
|
||
/** | ||
* Initialize a new HashGenerator | ||
* | ||
* @param data The byte array that should be hashed | ||
* @param hashAlgorithms The List of HashingAlgorithm enums that should be used to calculate hashes | ||
* @param compare The compare String for the calculated hashes | ||
*/ | ||
public HashGenerator(final byte[] data, final List<HashAlgorithm> hashAlgorithms, final String compare) { | ||
public HashGenerator(final List<HashAlgorithm> hashAlgorithms, final String compare) { | ||
hashData = new ArrayList<>(); | ||
this.data = data; | ||
|
||
this.hashAlgorithms = hashAlgorithms; | ||
this.compare = compare; | ||
} | ||
|
||
/** | ||
* Initialize a new HashGenerator | ||
* Get the List of HashData for the given input data | ||
* | ||
* @param data The byte array that should be hashed | ||
* @param hashAlgorithms The List of HashingAlgorithm enums that should be used to calculate hashes | ||
* @param compare The compare String for the calculated hashes | ||
* @throws IOException When the File could not be read | ||
* @return The List of HashData for the given input data | ||
*/ | ||
public HashGenerator(final File data, final List<HashAlgorithm> hashAlgorithms, final String compare) throws IOException { | ||
hashData = new ArrayList<>(); | ||
this.data = readFileToBytes(data); | ||
this.hashAlgorithms = hashAlgorithms; | ||
this.compare = compare; | ||
public List<HashAlgorithm> getHashAlgorithms() { | ||
return hashAlgorithms; | ||
} | ||
|
||
/** | ||
* Read a file and return a byte array that represents the given File | ||
* Get the List of HashData for the given input data | ||
* | ||
* @param file The File that should be read | ||
* @return The byte array that represents the given File | ||
* @throws IOException When the File could not be read | ||
* @return The List of HashData for the given input data | ||
*/ | ||
private byte[] readFileToBytes(final File file) throws IOException { | ||
if (file == null) | ||
throw new NullPointerException("File cannot be null!"); | ||
|
||
final int size = (int) file.length(); | ||
final byte[] bytes = new byte[size]; | ||
final byte[] tmpBuff = new byte[size]; | ||
try (final FileInputStream fis = new FileInputStream(file)) { | ||
int read = fis.read(bytes, 0, size); | ||
if (read < size) { | ||
int remain = size - read; | ||
while (remain > 0) { | ||
read = fis.read(tmpBuff, 0, remain); | ||
System.arraycopy(tmpBuff, 0, bytes, size - remain, read); | ||
remain -= read; | ||
} | ||
} | ||
} | ||
|
||
return bytes; | ||
public List<HashData> getHashData() { | ||
return hashData; | ||
} | ||
|
||
/** | ||
* Generate the List of HashData for the given input data | ||
* @return The List of HashData for the given input data | ||
* Get the compare String for the calculated hashes | ||
* | ||
* @return The compare String for the calculated hashes | ||
*/ | ||
public final List<HashData> generateHashes() { | ||
for (final HashAlgorithm algorithm : hashAlgorithms) { | ||
switch (algorithm) { | ||
case md5: | ||
final String md5 = HashUtil.calculateHash(data, "MD5"); | ||
hashData.add(new HashData("MD5", md5, compare)); | ||
break; | ||
case sha1: | ||
final String sha1 = HashUtil.calculateHash(data, "SHA-1"); | ||
hashData.add(new HashData("SHA-1", sha1, compare)); | ||
break; | ||
case sha224: | ||
final String sha224 = HashUtil.calculateHash(data, "SHA-224"); | ||
hashData.add(new HashData("SHA-224", sha224, compare)); | ||
break; | ||
case sha256: | ||
final String sha256 = HashUtil.calculateHash(data, "SHA-256"); | ||
hashData.add(new HashData("SHA-256", sha256, compare)); | ||
break; | ||
case sha384: | ||
final String sha384 = HashUtil.calculateHash(data, "SHA-384"); | ||
hashData.add(new HashData("SHA-384", sha384, compare)); | ||
break; | ||
case sha512: | ||
final String sha512 = HashUtil.calculateHash(data, "SHA-512"); | ||
hashData.add(new HashData("SHA-512", sha512, compare)); | ||
break; | ||
case crc32: | ||
final String crc32 = HashUtil.calculateCRC32(data); | ||
hashData.add(new HashData("CRC32", crc32, compare)); | ||
break; | ||
} | ||
} | ||
|
||
return hashData; | ||
public String getCompare() { | ||
return compare; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
app/src/main/java/com/codedead/deadhash/domain/objects/hashgenerator/IHashGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.codedead.deadhash.domain.objects.hashgenerator; | ||
|
||
import java.util.List; | ||
|
||
public interface IHashGenerator { | ||
List<HashData> generateHashes(); | ||
} |
71 changes: 71 additions & 0 deletions
71
app/src/main/java/com/codedead/deadhash/domain/objects/hashgenerator/TextHashGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.codedead.deadhash.domain.objects.hashgenerator; | ||
|
||
import com.codedead.deadhash.domain.utils.HashUtil; | ||
|
||
import java.util.List; | ||
|
||
public final class TextHashGenerator extends HashGenerator { | ||
|
||
private final String data; | ||
|
||
/** | ||
* Initialize a new TextHashGenerator | ||
* | ||
* @param data The String that should be hashed | ||
* @param hashAlgorithms The List of HashingAlgorithm enums that should be used to calculate hashes | ||
* @param compare The compare String for the calculated hashes | ||
*/ | ||
public TextHashGenerator(final String data, List<HashAlgorithm> hashAlgorithms, String compare) { | ||
super(hashAlgorithms, compare); | ||
|
||
if (data == null) | ||
throw new NullPointerException("Data cannot be null!"); | ||
if (data.isEmpty()) | ||
throw new IllegalArgumentException("Data cannot be empty!"); | ||
|
||
this.data = data; | ||
} | ||
|
||
/** | ||
* Generate the List of HashData for the given input data | ||
* | ||
* @return The List of HashData for the given input data | ||
*/ | ||
@Override | ||
public List<HashData> generateHashes() { | ||
for (final HashAlgorithm algorithm : super.getHashAlgorithms()) { | ||
switch (algorithm) { | ||
case md5 -> { | ||
final String md5 = HashUtil.calculateHash(data.getBytes(), "MD5"); | ||
getHashData().add(new HashData("MD5", md5, getCompare())); | ||
} | ||
case sha1 -> { | ||
final String sha1 = HashUtil.calculateHash(data.getBytes(), "SHA-1"); | ||
getHashData().add(new HashData("SHA-1", sha1, getCompare())); | ||
} | ||
case sha224 -> { | ||
final String sha224 = HashUtil.calculateHash(data.getBytes(), "SHA-224"); | ||
getHashData().add(new HashData("SHA-224", sha224, getCompare())); | ||
} | ||
case sha256 -> { | ||
final String sha256 = HashUtil.calculateHash(data.getBytes(), "SHA-256"); | ||
getHashData().add(new HashData("SHA-256", sha256, getCompare())); | ||
} | ||
case sha384 -> { | ||
final String sha384 = HashUtil.calculateHash(data.getBytes(), "SHA-384"); | ||
getHashData().add(new HashData("SHA-384", sha384, getCompare())); | ||
} | ||
case sha512 -> { | ||
final String sha512 = HashUtil.calculateHash(data.getBytes(), "SHA-512"); | ||
getHashData().add(new HashData("SHA-512", sha512, getCompare())); | ||
} | ||
case crc32 -> { | ||
final String crc32 = HashUtil.calculateCRC32(data.getBytes()); | ||
getHashData().add(new HashData("CRC32", crc32, getCompare())); | ||
} | ||
} | ||
} | ||
|
||
return getHashData(); | ||
} | ||
} |
Oops, something went wrong.