mirror of
https://github.com/ChronosX88/KademliaDHT.git
synced 2024-11-25 03:32:19 +00:00
Re-Arranged a few packages a bit
Added a HashCalculator class
This commit is contained in:
parent
2dde2a75e0
commit
51de9cbc82
@ -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
|
||||
|
@ -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
|
||||
|
@ -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.
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
26
src/kademlia/util/hashing/HashCalculator.java
Normal file
26
src/kademlia/util/hashing/HashCalculator.java
Normal file
@ -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();
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package kademlia.serializer;
|
||||
package kademlia.util.serializer;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
@ -1,4 +1,4 @@
|
||||
package kademlia.serializer;
|
||||
package kademlia.util.serializer;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
@ -1,4 +1,4 @@
|
||||
package kademlia.serializer;
|
||||
package kademlia.util.serializer;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.stream.JsonReader;
|
@ -1,4 +1,4 @@
|
||||
package kademlia.serializer;
|
||||
package kademlia.util.serializer;
|
||||
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
Loading…
Reference in New Issue
Block a user