From 708ffc20da9f0eb4088ca967563f28c7e4e16ed5 Mon Sep 17 00:00:00 2001 From: kske Date: Sun, 29 Dec 2019 11:47:35 +0200 Subject: [PATCH] Added LoginCredentials, moved SerializationUtils to envoy-common --- .../java/envoy/data/LoginCredentials.java | 59 ++++++++++++ .../java/envoy/util/SerializationUtils.java | 91 +++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 src/main/java/envoy/data/LoginCredentials.java create mode 100644 src/main/java/envoy/util/SerializationUtils.java diff --git a/src/main/java/envoy/data/LoginCredentials.java b/src/main/java/envoy/data/LoginCredentials.java new file mode 100644 index 0000000..c7899d6 --- /dev/null +++ b/src/main/java/envoy/data/LoginCredentials.java @@ -0,0 +1,59 @@ +package envoy.data; + +import java.io.Serializable; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * Project: envoy-common
+ * File: LoginCredentials.java
+ * Created: 29.12.2019
+ * + * @author Kai S. K. Engelbart + * @since Envoy Common v0.2-alpha + */ +public class LoginCredentials implements Serializable { + + private final String name; + private final byte[] passwordHash; + + private static final long serialVersionUID = -7395245059059523314L; + + /** + * Creates an in stance of {@link LoginCredentials}. + * + * @param name the name of the user + * @param password the password of the user (will be converted to a hash) + * @throws NoSuchAlgorithmException + */ + public LoginCredentials(String name, char[] password) throws NoSuchAlgorithmException { + this.name = name; + passwordHash = getSha256(toByteArray(password)); + } + + private byte[] toByteArray(char[] chars) { + byte[] bytes = new byte[chars.length * 2]; + for (int i = 0; i < chars.length; ++i) { + bytes[i * 2] = (byte) (chars[i] >> 8); + bytes[i * 2 + 1] = (byte) (chars[i]); + } + return bytes; + } + + private byte[] getSha256(byte[] input) throws NoSuchAlgorithmException { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + return md.digest(input); + } + + /** + * @return the name of the user performing the login + * @since Envoy Common v0.2-alpha + */ + public String getName() { return name; } + + /** + * @return the password hash of the user performing the login + * @since Envoy Common v0.2-alpha + */ + public byte[] getPasswordHash() { return passwordHash; } +} \ No newline at end of file diff --git a/src/main/java/envoy/util/SerializationUtils.java b/src/main/java/envoy/util/SerializationUtils.java new file mode 100644 index 0000000..3813a00 --- /dev/null +++ b/src/main/java/envoy/util/SerializationUtils.java @@ -0,0 +1,91 @@ +package envoy.util; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.OutputStream; +import java.io.Serializable; + +import envoy.exception.EnvoyException; + +/** + * Project: envoy-client
+ * File: SerializationUtils.java
+ * Created: 23.12.2019
+ * + * @author Kai S. K. Engelbart + * @since Envoy Common v0.2-alpha + */ +public class SerializationUtils { + + private SerializationUtils() {} + + /** + * Deserializes an arbitrary {@link Serializable} object from a file. + * + * @param the type of the object to deserialize + * @param file the file deserialize from + * @param serializedClass the class of the object to deserialize + * @return the deserialized object + * @throws EnvoyException if an error occurred during deserialization + * @since Envoy Common v0.2-alpha + */ + public static T read(File file, Class serializedClass) throws EnvoyException { + if (file == null) throw new NullPointerException("File is null"); + try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) { + return serializedClass.cast(in.readObject()); + } catch (ClassNotFoundException | IOException e) { + throw new EnvoyException("Could not load serialized object", e); + } + } + + /** + * Serializes an arbitrary object to a file. + * + * @param file the file to serialize to + * @param obj the object to serialize + * @throws IOException if an error occurred during serialization + * @since Envoy Common v0.2-alpha + */ + public static void write(File file, Object obj) throws IOException { + if (file == null) throw new NullPointerException("File is null"); + if (obj == null) throw new NullPointerException("Object to serialize is null"); + if (!file.exists()) { + file.getParentFile().mkdirs(); + file.createNewFile(); + } + try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) { + out.writeObject(obj); + } + } + + /** + * Serializes an object and writes it into an output stream preceded by 4 bytes + * containing the number of serialized bytes. + * + * @param obj the object to serialize + * @param out the output stream to serialize to + * @throws IOException if an error occurred during serialization + */ + public static void writeBytesWithLength(Object obj, OutputStream out) throws IOException { + // Serialize object to byte array + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (ObjectOutputStream oout = new ObjectOutputStream(baos)) { + oout.writeObject(obj); + } + byte[] objBytes = baos.toByteArray(); + + // Get length of byte array in bytes + byte[] objLen = getBytes(objBytes.length); + + // Write length and byte array + out.write(objLen); + out.write(objBytes); + } + + private static byte[] getBytes(int n) { return new byte[] { (byte) (n >>> 24), (byte) (n >>> 16), (byte) (n >>> 8) }; } +} \ No newline at end of file