2019-12-29 10:47:35 +01:00
|
|
|
package envoy.data;
|
|
|
|
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.security.MessageDigest;
|
|
|
|
import java.security.NoSuchAlgorithmException;
|
2019-12-29 11:53:16 +01:00
|
|
|
import java.util.Formatter;
|
2019-12-29 10:47:35 +01:00
|
|
|
|
|
|
|
/**
|
2019-12-31 10:16:52 +01:00
|
|
|
* Contains a {@link User}'s login information.<br>
|
|
|
|
* <br>
|
2019-12-29 10:47:35 +01:00
|
|
|
* Project: <strong>envoy-common</strong><br>
|
|
|
|
* File: <strong>LoginCredentials.java</strong><br>
|
|
|
|
* Created: <strong>29.12.2019</strong><br>
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
2019-12-29 10:47:35 +01:00
|
|
|
* @author Kai S. K. Engelbart
|
|
|
|
* @since Envoy Common v0.2-alpha
|
|
|
|
*/
|
|
|
|
public class LoginCredentials implements Serializable {
|
|
|
|
|
2019-12-30 18:49:48 +01:00
|
|
|
private final String name;
|
|
|
|
private final byte[] passwordHash;
|
2019-12-29 10:47:35 +01:00
|
|
|
|
2019-12-31 10:16:52 +01:00
|
|
|
private static final long serialVersionUID = -7395245059059523314L;
|
|
|
|
|
2019-12-29 10:47:35 +01:00
|
|
|
/**
|
|
|
|
* Creates an in stance of {@link LoginCredentials}.
|
2019-12-30 18:49:48 +01:00
|
|
|
*
|
2019-12-29 10:47:35 +01:00
|
|
|
* @param name the name of the user
|
|
|
|
* @param password the password of the user (will be converted to a hash)
|
2019-12-30 18:49:48 +01:00
|
|
|
* @throws NoSuchAlgorithmException if the algorithm used is unknown
|
|
|
|
* @since Envoy Common v0.2-alpha
|
2019-12-29 10:47:35 +01:00
|
|
|
*/
|
|
|
|
public LoginCredentials(String name, char[] password) throws NoSuchAlgorithmException {
|
|
|
|
this.name = name;
|
|
|
|
passwordHash = getSha256(toByteArray(password));
|
|
|
|
}
|
|
|
|
|
2019-12-30 18:49:48 +01:00
|
|
|
/**
|
|
|
|
* @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; }
|
|
|
|
|
2019-12-31 10:16:52 +01:00
|
|
|
private byte[] getSha256(byte[] input) throws NoSuchAlgorithmException { return MessageDigest.getInstance("SHA-256").digest(input); }
|
2019-12-29 11:53:16 +01:00
|
|
|
|
2019-12-29 10:47:35 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-12-30 18:49:48 +01:00
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
try (Formatter form = new Formatter()) {
|
|
|
|
form.format("LoginCredentials[name=%s,passwordHash=", name);
|
2019-12-31 10:16:52 +01:00
|
|
|
for (byte element : passwordHash)
|
2019-12-30 18:49:48 +01:00
|
|
|
form.format("%02x", element);
|
2019-12-31 10:16:52 +01:00
|
|
|
return form.format("]").toString();
|
2019-12-30 18:49:48 +01:00
|
|
|
}
|
2019-12-29 10:47:35 +01:00
|
|
|
}
|
|
|
|
}
|