-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This abstraction, which includes ByteBuf-compatible methods, also fixes the invalid-hash bug from ChunkTile.computeDataHash(), since it's not hashing the whole underlying array, only the written bytes.
- Loading branch information
1 parent
3178d69
commit 10cfe6c
Showing
4 changed files
with
127 additions
and
26 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
116 changes: 116 additions & 0 deletions
116
mod/common/src/main/java/gjum/minecraft/mapsync/common/utils/Hasher.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,116 @@ | ||
package gjum.minecraft.mapsync.common.utils; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import java.nio.ByteBuffer; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Objects; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public final class Hasher { | ||
private final MessageDigest messageDigest; | ||
|
||
private Hasher( | ||
final @NotNull MessageDigest messageDigest | ||
) { | ||
this.messageDigest = Objects.requireNonNull(messageDigest); | ||
} | ||
|
||
/** | ||
* Updates the digest with a single byte. | ||
*/ | ||
public @NotNull Hasher update( | ||
final byte input | ||
) { | ||
this.messageDigest.update((byte) input); | ||
return this; | ||
} | ||
|
||
/** | ||
* Updates the digest with an entire byte array. | ||
*/ | ||
public @NotNull Hasher update( | ||
final byte @NotNull [] input | ||
) { | ||
this.messageDigest.update((byte[]) input); | ||
return this; | ||
} | ||
|
||
/** | ||
* Updates the digest with a byte-array slice, defined by the given offset and length. | ||
*/ | ||
public @NotNull Hasher update( | ||
final byte @NotNull [] input, | ||
final int offset, | ||
final int length | ||
) { | ||
this.messageDigest.update((byte[]) input, offset, length); | ||
return this; | ||
} | ||
|
||
/** | ||
* Updates the digest with a ByteBuffer slice, using {@link ByteBuffer#position()} as the offset and | ||
* {@link ByteBuffer#remaining()} as the length. If you have been writing to this ByteBuffer, you may wish to | ||
* {@link ByteBuffer#flip()} it first before passing it into this method. | ||
*/ | ||
public @NotNull Hasher update( | ||
final @NotNull ByteBuffer input | ||
) { | ||
this.messageDigest.update((ByteBuffer) input); | ||
return this; | ||
} | ||
|
||
/** | ||
* Updates the digest with a ByteBuffer slice, defined by the given offset and length. | ||
*/ | ||
public @NotNull Hasher update( | ||
final @NotNull ByteBuffer input, | ||
final int offset, | ||
final int length | ||
) { | ||
return update((ByteBuffer) input.slice(offset, length)); | ||
} | ||
|
||
/** | ||
* Updates the digest with a ByteBuf slice, using {@link ByteBuf#readerIndex()} as the offset and | ||
* {@link ByteBuf#readableBytes()} as the length. | ||
*/ | ||
public @NotNull Hasher update( | ||
final @NotNull ByteBuf input | ||
) { | ||
update((ByteBuffer) input.nioBuffer()); | ||
return this; | ||
} | ||
|
||
/** | ||
* Updates the digest with a ByteBuf slice, defined by the given offset and length. | ||
*/ | ||
public @NotNull Hasher update( | ||
final @NotNull ByteBuf input, | ||
final int offset, | ||
final int length | ||
) { | ||
update((ByteBuffer) input.nioBuffer(offset, length)); | ||
return this; | ||
} | ||
|
||
public byte @NotNull [] generateHash() { | ||
return this.messageDigest.digest(); | ||
} | ||
|
||
/** | ||
* Since every implementation of Java is required to support SHA-1 | ||
* (<a href="https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/security/MessageDigest.html">source</a>) | ||
* it's a safe bet that the algorithm exists. | ||
*/ | ||
public static @NotNull Hasher sha1() { | ||
final MessageDigest messageDigest; | ||
try { | ||
messageDigest = MessageDigest.getInstance("SHA-1"); | ||
} | ||
catch (final NoSuchAlgorithmException thrown) { | ||
throw new IllegalStateException("This should never happen!", thrown); | ||
} | ||
return new Hasher(messageDigest); | ||
} | ||
} |