Skip to content

Commit

Permalink
Add readme and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rctcwyvrn committed May 13, 2020
1 parent 3cc311c commit c8bed6a
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
BLAKE3 in Java
---
A completely unoptimized translation of the blake3 reference implementation from rust to java.
Useful for if you want a single file implementation of blake3 with no dependencies and don't care about speed.

If what you are java bindings for blake3 that are fully optimized try: https://github.com/sken77/BLAKE3jni
```java
// Hashing strings
Blake3 hasher = new Blake3();
hasher.update("This is a string");
String hexhash = hasher.hexdigest();
```
```java
// Hashing files
String filename = "src/com/github/rctcwyvrn/blake3java/Blake3.java";
Blake3 fileHasher = new Blake3();
fileHasher.updateFile(filename);
String filehash = fileHasher.hexdigest();
```
16 changes: 6 additions & 10 deletions src/com/github/rctcwyvrn/blake3java/Blake3.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,13 @@ public void update(String input){
updateRaw(converted);
}

public void updateFile(String filename){
try {
byte[] fileContent = Files.readAllBytes(Paths.get(filename));
short[] converted = new short[fileContent.length];
for(int i = 0; i<fileContent.length; i++){
converted[i] = (short) (0xff & fileContent[i]);
}
updateRaw(converted);
} catch (IOException e) {
System.err.println("File not found: " + filename);
public void updateFile(String filename) throws IOException {
byte[] fileContent = Files.readAllBytes(Paths.get(filename));
short[] converted = new short[fileContent.length];
for(int i = 0; i<fileContent.length; i++){
converted[i] = (short) (0xff & fileContent[i]);
}
updateRaw(converted);
}
private void updateRaw(short[] input){
while(input.length != 0) {
Expand Down
17 changes: 12 additions & 5 deletions src/com/github/rctcwyvrn/blake3java/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.rctcwyvrn.blake3java;

import java.io.IOException;

public class Main {
public static void main(String[] args){

Expand All @@ -13,10 +15,15 @@ public static void main(String[] args){
// Hashing files
// Warning: Very slow due to lack of optimizations :c
String filename = "src/com/github/rctcwyvrn/blake3java/Blake3.java";
Blake3 fileHasher = new Blake3();
fileHasher.updateFile(filename);
String filehash = fileHasher.hexdigest();
if(!filehash.equals("5110a18d7e9595629ea32c87337662f6b1247b970e38dac117c47778955200de")) throw new AssertionError();
System.out.println("Success: " + filehash);
try {
Blake3 fileHasher = new Blake3();
fileHasher.updateFile(filename);
String filehash = fileHasher.hexdigest();
if (!filehash.equals("86894a68f93452be698f7ec97bc932df093f9905a52df73226cee2d713bf5323"))
throw new AssertionError();
System.out.println("Success: " + filehash);
} catch (IOException e) {
System.err.println("File not found: " + filename);
}
}
}

0 comments on commit c8bed6a

Please sign in to comment.