Метод получение md5-hash перенесён в класс Util
This commit is contained in:
parent
e696923f05
commit
fa9aa7d2f9
@ -6,6 +6,8 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Scanner;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
@ -68,6 +70,24 @@ public class Util {
|
||||
if (DEBUG) ex.printStackTrace();
|
||||
}
|
||||
|
||||
public static String md5(String s) {
|
||||
try {
|
||||
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
|
||||
digest.update(s.getBytes());
|
||||
byte[] messageDigest = digest.digest();
|
||||
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
for (int i = 0; i < messageDigest.length; i++) {
|
||||
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
|
||||
}
|
||||
return hexString.toString();
|
||||
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private static InputStream getInputStream(String resource) {
|
||||
return Runtime.getRuntime().getClass().getResourceAsStream(resource);
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import javax.swing.JTextField;
|
||||
import javax.swing.SwingConstants;
|
||||
import org.netbeans.lib.awtextra.AbsoluteConstraints;
|
||||
import org.netbeans.lib.awtextra.AbsoluteLayout;
|
||||
import tse.Util;
|
||||
|
||||
/**
|
||||
* Панель авторизации.
|
||||
@ -147,7 +148,7 @@ public class LoginPanel extends JPanel {
|
||||
}
|
||||
|
||||
private void registerNewAccount(String user, String pass) {
|
||||
String md5hash = Utils.md5(pass);
|
||||
String md5hash = Util.md5(pass);
|
||||
AccountManager.getInstance().createNewAccount(user, md5hash);
|
||||
}
|
||||
|
||||
@ -163,7 +164,7 @@ public class LoginPanel extends JPanel {
|
||||
return;
|
||||
}
|
||||
|
||||
String md5hash = Utils.md5(pass);
|
||||
String md5hash = Util.md5(pass);
|
||||
switch(AccountManager.getInstance().checkAuth(login, md5hash)) {
|
||||
case AccountManager.STATE_ACCOUNT_NOT_EXISTS:
|
||||
JOptionPane.showMessageDialog(this, "Пользователя с таким именем не существует", "Ошибка", JOptionPane.ERROR_MESSAGE);
|
||||
|
@ -1,29 +0,0 @@
|
||||
package tse.lr4;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author aNNiMON
|
||||
*/
|
||||
public class Utils {
|
||||
|
||||
public static String md5(String s) {
|
||||
try {
|
||||
MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
|
||||
digest.update(s.getBytes());
|
||||
byte[] messageDigest = digest.digest();
|
||||
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
for (int i = 0; i < messageDigest.length; i++) {
|
||||
hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
|
||||
}
|
||||
return hexString.toString();
|
||||
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user