-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
137 additions
and
0 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
24 changes: 24 additions & 0 deletions
24
src/main/java/com/mirkocaserta/bruce/digest/FileDigester.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,24 @@ | ||
package com.mirkocaserta.bruce.digest; | ||
|
||
import com.mirkocaserta.bruce.BruceException; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* Generic interface for getting file digests | ||
* in an encoded format such as hexadecimal or base64. | ||
* | ||
* @author Mirko Caserta ([email protected]) | ||
*/ | ||
public interface FileDigester { | ||
|
||
/** | ||
* Returns the file digest in encoded format. | ||
* | ||
* @param file the file to digest | ||
* @return the file digest in encoded format | ||
* @throws BruceException on digesting errors | ||
*/ | ||
String digest(File file); | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
src/test/java/com/mirkocaserta/bruce/digest/FileDigesterTest.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,44 @@ | ||
package com.mirkocaserta.bruce.digest; | ||
|
||
import com.mirkocaserta.bruce.Bruce; | ||
import com.mirkocaserta.bruce.BruceException; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.File; | ||
|
||
import static com.mirkocaserta.bruce.Bruce.digester; | ||
import static com.mirkocaserta.bruce.Bruce.fileDigester; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@DisplayName("File digester tests") | ||
class FileDigesterTest { | ||
|
||
@Test | ||
@DisplayName("Hexadecimal file digester for the SHA1 algorithm") | ||
void sha1Hex() { | ||
var digester = fileDigester("SHA1", Bruce.Encoding.HEX); | ||
assertEquals("4e1243bd22c66e76c2ba9eddc1f91394e57f9f83", digester.digest(new File("src/test/resources/test-file-1")), "1st sha1"); | ||
assertEquals("9054fbe0b622c638224d50d20824d2ff6782e308", digester.digest(new File("src/test/resources/test-file-2")), "2nd sha1"); | ||
} | ||
|
||
@Test | ||
@DisplayName("Base64 file digester for the SHA1 algorithm") | ||
void sha1Base64() { | ||
var digester = fileDigester("SHA1", Bruce.Encoding.BASE64); | ||
assertEquals("ThJDvSLGbnbCup7dwfkTlOV/n4M=", digester.digest(new File("src/test/resources/test-file-1")), "1st sha1"); | ||
assertEquals("kFT74LYixjgiTVDSCCTS/2eC4wg=", digester.digest(new File("src/test/resources/test-file-2")), "2nd sha1"); | ||
} | ||
|
||
@Test | ||
@DisplayName("Digester for an invalid algorithm should throw a DigesterException") | ||
void invalidAlgorithm1() { | ||
Assertions.assertThrows( | ||
BruceException.class, | ||
() -> fileDigester("foo", Bruce.Encoding.HEX), | ||
"No such algorithm: foo" | ||
); | ||
} | ||
|
||
} |
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 @@ | ||
test |
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 @@ | ||
test2 |