У меня есть PasswordEncryptionService, где я хэширую свои пароли и сохраняю их в своей базе данных MySQL вместе с солью, используемой для их хеширования.Я сохраняю и свои пароли, и свой Salt в виде байтового массива, и я слышал, что могу просто сохранить их как тип varbinary в базе данных SQL.
Вот мой код:
//this method is used to retrieve the salt when a user is logging in
public static byte[] getSaltMethod(String username, String password) throws SQLException, LoginSampleException {
try {
Connection con = Connector.connection();
String SQL = "SELECT * from Users.Salt WHERE email = ?, password = ?";
PreparedStatement statement = con.prepareStatement(SQL);
statement.setString(1, username);
statement.setString(2, password);
ResultSet set = statement.executeQuery();
while (set.next()) {
// vi skal ikke have en blolb her alligevel
byte[] salt = set.getBytes("salt");
//release the blob and free up memory. (since JDBC 4.0)
/* jeg er i tivil om denne skal være her*/
return salt;
}
} catch (SQLException ex) {
Conf.MYLOGGER.log(Level.SEVERE, null, ex);
throw new LoginSampleException(ex.getSQLState());
}
return null;
}
//this method is used to save the user along with the salt when a user is signing up
public static void createUser(User user) throws LoginSampleException, NoSuchAlgorithmException
{
try {
PasswordEncryptionService PE = new PasswordEncryptionService();
byte[] salt = PE.generateSalt();
Connection con = Connector.connection();
String SQL = "INSERT INTO Users (email, password, phone, post, adress, role, salt) VALUES (?, ?, ?, ?, ?, ?, ?)";
PreparedStatement ps = con.prepareStatement(SQL, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, user.getEmail());
ps.setString(2, user.getPassword());
ps.setString(3, user.getPhonenumber());
ps.setString(4, user.getPostalCode());
ps.setString(5, user.getAddress());
ps.setString(6, user.getRole());
ps.setBytes(7, salt);
ps.executeUpdate();
ResultSet ids = ps.getGeneratedKeys();
ids.next();
int id = ids.getInt(1);
user.setId(id);
} catch (SQLException ex) {
throw new LoginSampleException(ex.getMessage());
}
}
Прямо сейчас у меня есть отдельный метод для получения соли и регистрации в ней человека. Должен ли я сохранить соль в другой таблице или ее можно сохранить в пользовательском объекте?Должен ли я сохранить его как varbinary или как обычный varchar?