Move hasing operations to own class; javadoc improvements

This commit is contained in:
Stuart Stock
2017-01-28 10:59:43 -06:00
parent 4da87917c9
commit 68493b696b
4 changed files with 61 additions and 20 deletions

View File

@ -12,7 +12,16 @@ import java.util.Map;
public final class RtEncoding {
/**
* Encode the given message using the provided allocator.
* Encode the given message using the system default ByteBuf allocator.
*
* @return A {@link ByteBuf} containing this message encoded for transmission.
*/
public static ByteBuf toWire(RtMessage msg) {
return toWire(msg, ByteBufAllocator.DEFAULT);
}
/**
* Encode the given message using the provided ByteBuf allocator.
*
* @return A {@link ByteBuf} containing this message encoded for transmission.
*/

View File

@ -0,0 +1,40 @@
package nearenough.protocol;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public final class RtHashing {
/**
* @return A new SHA-512 instance
*/
public static MessageDigest newSha512() {
try {
return MessageDigest.getInstance("SHA-512");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
private final MessageDigest sha512;
public RtHashing() {
this.sha512 = newSha512();
}
public byte[] hashLeaf(byte[] leaf) {
sha512.reset();
sha512.update(RtConstants.TREE_LEAF_TWEAK);
sha512.update(leaf);
return sha512.digest();
}
public byte[] hashNode(byte[] left, byte[] right) {
sha512.reset();
sha512.update(RtConstants.TREE_NODE_TWEAK);
sha512.update(left);
sha512.update(right);
return sha512.digest();
}
}

View File

@ -54,6 +54,7 @@ public enum RtTag {
if (tag != null) {
return tag;
} else {
//noinspection NumericCastThatLosesPrecision
String exMsg = String.format(
"'%c%c%c%c' (0x%08x)",
(char) (tagValue >> 24 & 0xff),

View File

@ -2,7 +2,6 @@ package nearenough.util;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
@ -10,15 +9,14 @@ import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.DatagramPacket;
import io.netty.channel.socket.nio.NioDatagramChannel;
import nearenough.protocol.RtConstants;
import nearenough.protocol.RtEncoding;
import nearenough.protocol.RtMessage;
import nearenough.protocol.RtTag;
import java.net.InetSocketAddress;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import nearenough.protocol.RtEncoding;
import nearenough.protocol.RtHashing;
import nearenough.protocol.RtMessage;
import nearenough.protocol.RtTag;
/**
* Send a one-off request to the given Roughtime server and dump the response (if any)
@ -35,21 +33,12 @@ public final class ResponseDumper {
public RequestHandler(InetSocketAddress addr) {
Random rand = new Random();
MessageDigest sha512;
try {
sha512 = MessageDigest.getInstance("SHA-512");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
RtHashing hasher = new RtHashing();
this.addr = addr;
this.nonce = new byte[64];
rand.nextBytes(nonce);
sha512.update(RtConstants.TREE_LEAF_TWEAK);
sha512.update(nonce);
this.expectedLeafHash = sha512.digest();
this.expectedLeafHash = hasher.hashLeaf(nonce);
System.out.println("NONC = " + ByteBufUtil.hexDump(nonce));
System.out.println("LEAF(NONC) = " + ByteBufUtil.hexDump(expectedLeafHash));
@ -62,7 +51,7 @@ public final class ResponseDumper {
.add(RtTag.NONC, nonce)
.build();
ByteBuf buf = RtEncoding.toWire(msg, ByteBufAllocator.DEFAULT);
ByteBuf buf = RtEncoding.toWire(msg);
ctx.writeAndFlush(new DatagramPacket(buf, addr))
.addListener(fut -> {
@ -74,7 +63,9 @@ public final class ResponseDumper {
@Override
protected void channelRead0(ChannelHandlerContext ctx, DatagramPacket msg) throws Exception {
System.out.printf("Read message (%s):\n", msg.sender());
System.out.printf(
"Read message of %d bytes from %s:\n", msg.content().readableBytes(), msg.sender()
);
System.out.println(ByteBufUtil.prettyHexDump(msg.content()));
RtMessage response = new RtMessage(msg.content());
System.out.println(response);