Created
September 8, 2015 21:45
-
-
Save garcia-jj/7d759a4f2adc8e55c328 to your computer and use it in GitHub Desktop.
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
package ot.security; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
/** | |
* Initial draft for a fluent API to use hashes like MD5, SHA and so on. | |
* | |
* @author Otávio S Garcia | |
*/ | |
public final class Sha3Hasher { | |
private static final int BUFFER_SIZE = 4096; | |
public static String checksum512(Path path) { | |
MessageDigest md = getSha3Algorithm(); | |
try (InputStream stream = Files.newInputStream(path)) { | |
byte[] dataBytes = new byte[BUFFER_SIZE]; | |
int nread = 0; | |
while ((nread = stream.read(dataBytes)) != -1) { | |
md.update(dataBytes, 0, nread); | |
} | |
} catch (IOException e) { | |
throw new ChecksumException(e); | |
} | |
return toHex(md.digest()); | |
} | |
private static MessageDigest getSha3Algorithm() { | |
try { | |
return MessageDigest.getInstance("SHA3-512"); | |
} catch (NoSuchAlgorithmException e) { | |
throw new ChecksumException("SHA3-512 algorithm not found. Please install Bouncy Castle API.", e); | |
} | |
} | |
private static String toHex(byte[] data) { | |
StringBuilder hexStr = new StringBuilder(data.length * 2); | |
for (int i = 0; i < data.length; i++) { | |
hexStr.append(Character.forDigit((data[i] >> 4) & 0xF, 16)); | |
hexStr.append(Character.forDigit((data[i] & 0xF), 16)); | |
} | |
return hexStr.toString(); | |
} | |
// TODO externalize | |
public static class ChecksumException extends IllegalStateException { | |
private static final long serialVersionUID = 1L; | |
public ChecksumException(Throwable cause) { | |
super(cause); | |
} | |
public ChecksumException(String message, Throwable cause) { | |
super(message, cause); | |
} | |
} | |
} |
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
package ot.security; | |
import static org.hamcrest.Matchers.is; | |
import static org.junit.Assert.assertThat; | |
import static ot.security.Sha3Hasher.checksum512; | |
import java.nio.charset.StandardCharsets; | |
import java.nio.file.Files; | |
import java.nio.file.Path; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.rules.TemporaryFolder; | |
public class Sha3HasherTest { | |
@Rule | |
public TemporaryFolder folder = new TemporaryFolder(); | |
private static final String expected224 = "55d39d141deb65eb9f9363c348ed69d4b5c8e56afe9e1e75d7fd5f99"; | |
private static final String expected256 = "865bf05cca7ba26fb8051e8366c6d19e21cadeebe3ee6bfa462b5c72275414ec"; | |
private static final String expected384 = "71ced3eca3769907fc417bed3fdf82d9d7d36b99d89ead4caaa77d412ada4d37ec1ea26f857f0d964c2f2edb9cf03cf0"; | |
private static final String expected512 = "b87fac887f393280be76c803f147a4938103bfc9d829281699a88e48ee54c9d3a7233b018a943a6b85ff5719e58fd0e34442bae8d2cafad54e7d18b7e60a3457"; | |
@Test | |
public void test512() throws Exception { | |
Path file = folder.newFile().toPath(); | |
Files.write(file, "the quick brown fox jumps over the lazy dog".getBytes(StandardCharsets.UTF_8)); | |
assertThat(checksum512(file), is(expected512)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment