Added LoginCredentials, moved SerializationUtils to envoy-common

This commit is contained in:
Kai S. K. Engelbart 2019-12-29 11:47:35 +02:00
parent 3c7032ceca
commit 708ffc20da
2 changed files with 150 additions and 0 deletions

View File

@ -0,0 +1,59 @@
package envoy.data;
import java.io.Serializable;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* Project: <strong>envoy-common</strong><br>
* File: <strong>LoginCredentials.java</strong><br>
* Created: <strong>29.12.2019</strong><br>
*
* @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; }
}

View File

@ -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: <strong>envoy-client</strong><br>
* File: <strong>SerializationUtils.java</strong><br>
* Created: <strong>23.12.2019</strong><br>
*
* @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 <T> 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 extends Serializable> T read(File file, Class<T> 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) }; }
}