From 51de9cbc825ba41dc34c17d06250f583fdd905e7 Mon Sep 17 00:00:00 2001 From: Joshua Kissoon Date: Sat, 5 Apr 2014 22:36:46 +0530 Subject: [PATCH] Re-Arranged a few packages a bit Added a HashCalculator class --- src/kademlia/Kademlia.java | 6 +-- src/kademlia/dht/DHT.java | 4 +- .../message/ContentLookupMessage.java | 2 +- src/kademlia/message/ContentMessage.java | 2 +- src/kademlia/message/StoreContentMessage.java | 2 +- src/kademlia/util/JsonSerializer.java | 47 ------------------- src/kademlia/util/hashing/HashCalculator.java | 26 ++++++++++ .../serializer/JsonDHTSerializer.java | 2 +- .../JsonRoutingTableSerializer.java | 2 +- .../{ => util}/serializer/JsonSerializer.java | 2 +- .../{ => util}/serializer/KadSerializer.java | 2 +- 11 files changed, 38 insertions(+), 59 deletions(-) delete mode 100644 src/kademlia/util/JsonSerializer.java create mode 100644 src/kademlia/util/hashing/HashCalculator.java rename src/kademlia/{ => util}/serializer/JsonDHTSerializer.java (98%) rename src/kademlia/{ => util}/serializer/JsonRoutingTableSerializer.java (98%) rename src/kademlia/{ => util}/serializer/JsonSerializer.java (97%) rename src/kademlia/{ => util}/serializer/KadSerializer.java (96%) diff --git a/src/kademlia/Kademlia.java b/src/kademlia/Kademlia.java index 76b1bb9..b0a0f04 100644 --- a/src/kademlia/Kademlia.java +++ b/src/kademlia/Kademlia.java @@ -30,9 +30,9 @@ import kademlia.operation.Operation; import kademlia.operation.KadRefreshOperation; import kademlia.operation.StoreOperation; import kademlia.routing.RoutingTable; -import kademlia.serializer.JsonDHTSerializer; -import kademlia.serializer.JsonRoutingTableSerializer; -import kademlia.serializer.JsonSerializer; +import kademlia.util.serializer.JsonDHTSerializer; +import kademlia.util.serializer.JsonRoutingTableSerializer; +import kademlia.util.serializer.JsonSerializer; /** * The main Kademlia network management class diff --git a/src/kademlia/dht/DHT.java b/src/kademlia/dht/DHT.java index 735c578..c6883d0 100644 --- a/src/kademlia/dht/DHT.java +++ b/src/kademlia/dht/DHT.java @@ -13,8 +13,8 @@ import kademlia.core.KadConfiguration; import kademlia.exceptions.ContentExistException; import kademlia.exceptions.ContentNotFoundException; import kademlia.node.NodeId; -import kademlia.serializer.JsonSerializer; -import kademlia.serializer.KadSerializer; +import kademlia.util.serializer.JsonSerializer; +import kademlia.util.serializer.KadSerializer; /** * The main Distributed Hash Table class that manages the entire DHT diff --git a/src/kademlia/message/ContentLookupMessage.java b/src/kademlia/message/ContentLookupMessage.java index 1b8b9bb..9d8a85f 100644 --- a/src/kademlia/message/ContentLookupMessage.java +++ b/src/kademlia/message/ContentLookupMessage.java @@ -5,7 +5,7 @@ import java.io.DataOutputStream; import java.io.IOException; import kademlia.dht.GetParameter; import kademlia.node.Node; -import kademlia.serializer.JsonSerializer; +import kademlia.util.serializer.JsonSerializer; /** * Messages used to send to another node requesting content. diff --git a/src/kademlia/message/ContentMessage.java b/src/kademlia/message/ContentMessage.java index 8da6a20..1a35304 100644 --- a/src/kademlia/message/ContentMessage.java +++ b/src/kademlia/message/ContentMessage.java @@ -5,7 +5,7 @@ import java.io.DataOutputStream; import java.io.IOException; import kademlia.dht.StorageEntry; import kademlia.node.Node; -import kademlia.serializer.JsonSerializer; +import kademlia.util.serializer.JsonSerializer; /** * A Message used to send content between nodes diff --git a/src/kademlia/message/StoreContentMessage.java b/src/kademlia/message/StoreContentMessage.java index 6ffba1b..9a7ce5d 100644 --- a/src/kademlia/message/StoreContentMessage.java +++ b/src/kademlia/message/StoreContentMessage.java @@ -6,7 +6,7 @@ import java.io.IOException; import kademlia.dht.KadContent; import kademlia.dht.StorageEntry; import kademlia.node.Node; -import kademlia.serializer.JsonSerializer; +import kademlia.util.serializer.JsonSerializer; /** * A StoreContentMessage used to send a store message to a node diff --git a/src/kademlia/util/JsonSerializer.java b/src/kademlia/util/JsonSerializer.java deleted file mode 100644 index 1075803..0000000 --- a/src/kademlia/util/JsonSerializer.java +++ /dev/null @@ -1,47 +0,0 @@ -/** - * @author Joshua Kissoon - * @created 20140218 - * @desc Serializes a message into a json message - */ -package kademlia.util; - -import com.google.gson.Gson; -import com.google.gson.stream.JsonWriter; -import java.io.IOException; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import kademlia.message.Message; - -public class JsonSerializer -{ - - private final Gson gson; - - public JsonSerializer() - { - this.gson = new Gson(); - } - - /** - * Writes a message to an output stream - * - * @param msg The message to write - * @param out The output stream to write the message to - */ - public void write(Message msg, OutputStream out) - { - try (JsonWriter writer = new JsonWriter(new OutputStreamWriter(out))) - { - writer.beginArray(); - - this.gson.toJson(msg, msg.getClass(), writer); - - writer.endArray(); - } - catch (IOException e) - { - - } - } - -} diff --git a/src/kademlia/util/hashing/HashCalculator.java b/src/kademlia/util/hashing/HashCalculator.java new file mode 100644 index 0000000..6901d5a --- /dev/null +++ b/src/kademlia/util/hashing/HashCalculator.java @@ -0,0 +1,26 @@ +package kademlia.util.hashing; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * A class that is used to calculate the hash of strings. + * + * @author Joshua Kissoon + * @since 20140405 + */ +public class HashCalculator +{ + + public static byte[] sha1Hash(String toHash) throws NoSuchAlgorithmException + { + /* Create a MessageDigest */ + MessageDigest md = MessageDigest.getInstance("SHA-1"); + + /* Add password bytes to digest */ + md.update(toHash.getBytes()); + + /* Get the hashed bytes */ + return md.digest(); + } +} diff --git a/src/kademlia/serializer/JsonDHTSerializer.java b/src/kademlia/util/serializer/JsonDHTSerializer.java similarity index 98% rename from src/kademlia/serializer/JsonDHTSerializer.java rename to src/kademlia/util/serializer/JsonDHTSerializer.java index 6f88215..a9b2307 100644 --- a/src/kademlia/serializer/JsonDHTSerializer.java +++ b/src/kademlia/util/serializer/JsonDHTSerializer.java @@ -1,4 +1,4 @@ -package kademlia.serializer; +package kademlia.util.serializer; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; diff --git a/src/kademlia/serializer/JsonRoutingTableSerializer.java b/src/kademlia/util/serializer/JsonRoutingTableSerializer.java similarity index 98% rename from src/kademlia/serializer/JsonRoutingTableSerializer.java rename to src/kademlia/util/serializer/JsonRoutingTableSerializer.java index f0ee30a..614883f 100644 --- a/src/kademlia/serializer/JsonRoutingTableSerializer.java +++ b/src/kademlia/util/serializer/JsonRoutingTableSerializer.java @@ -1,4 +1,4 @@ -package kademlia.serializer; +package kademlia.util.serializer; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; diff --git a/src/kademlia/serializer/JsonSerializer.java b/src/kademlia/util/serializer/JsonSerializer.java similarity index 97% rename from src/kademlia/serializer/JsonSerializer.java rename to src/kademlia/util/serializer/JsonSerializer.java index a43170e..b59e605 100644 --- a/src/kademlia/serializer/JsonSerializer.java +++ b/src/kademlia/util/serializer/JsonSerializer.java @@ -1,4 +1,4 @@ -package kademlia.serializer; +package kademlia.util.serializer; import com.google.gson.Gson; import com.google.gson.stream.JsonReader; diff --git a/src/kademlia/serializer/KadSerializer.java b/src/kademlia/util/serializer/KadSerializer.java similarity index 96% rename from src/kademlia/serializer/KadSerializer.java rename to src/kademlia/util/serializer/KadSerializer.java index ddd33f4..824a1d1 100644 --- a/src/kademlia/serializer/KadSerializer.java +++ b/src/kademlia/util/serializer/KadSerializer.java @@ -1,4 +1,4 @@ -package kademlia.serializer; +package kademlia.util.serializer; import java.io.DataInputStream; import java.io.DataOutputStream;