Метод получение md5-hash перенесён в класс Util
This commit is contained in:
parent
e696923f05
commit
fa9aa7d2f9
@ -1,74 +1,94 @@
|
||||
package tse;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Scanner;
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author aNNiMON
|
||||
*/
|
||||
public class Util {
|
||||
|
||||
private static final boolean DEBUG = true;
|
||||
|
||||
public static String readDescription(int labNumber, int index) {
|
||||
final StringBuilder text = new StringBuilder();
|
||||
final String resource = "/res/desc" + labNumber + "_" + index + ".txt";
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
InputStream stream = getInputStream(resource);
|
||||
InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
|
||||
in = new BufferedReader(reader);
|
||||
|
||||
String line = in.readLine();
|
||||
while (line != null) {
|
||||
text.append(line);
|
||||
line = in.readLine();
|
||||
}
|
||||
} catch (IOException | NullPointerException ex) {
|
||||
text.append("Error");
|
||||
handleException(ex);
|
||||
} finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException ex) {
|
||||
handleException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
return text.toString();
|
||||
}
|
||||
|
||||
public static double readDouble(Scanner sc) {
|
||||
while (!sc.hasNextDouble()) {
|
||||
System.out.println("Ошибка! Неверный формат!");
|
||||
sc.next();
|
||||
}
|
||||
return sc.nextDouble();
|
||||
}
|
||||
|
||||
public static Image readImageRes(String name) {
|
||||
try {
|
||||
InputStream is = getInputStream("/res/images/" + name);
|
||||
return ImageIO.read(is);
|
||||
} catch (IOException ex) {
|
||||
handleException(ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void handleException(Exception ex) {
|
||||
if (DEBUG) ex.printStackTrace();
|
||||
}
|
||||
|
||||
private static InputStream getInputStream(String resource) {
|
||||
return Runtime.getRuntime().getClass().getResourceAsStream(resource);
|
||||
}
|
||||
}
|
||||
package tse;
|
||||
|
||||
import java.awt.Image;
|
||||
import java.io.BufferedReader;
|
||||
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;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author aNNiMON
|
||||
*/
|
||||
public class Util {
|
||||
|
||||
private static final boolean DEBUG = true;
|
||||
|
||||
public static String readDescription(int labNumber, int index) {
|
||||
final StringBuilder text = new StringBuilder();
|
||||
final String resource = "/res/desc" + labNumber + "_" + index + ".txt";
|
||||
BufferedReader in = null;
|
||||
try {
|
||||
InputStream stream = getInputStream(resource);
|
||||
InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
|
||||
in = new BufferedReader(reader);
|
||||
|
||||
String line = in.readLine();
|
||||
while (line != null) {
|
||||
text.append(line);
|
||||
line = in.readLine();
|
||||
}
|
||||
} catch (IOException | NullPointerException ex) {
|
||||
text.append("Error");
|
||||
handleException(ex);
|
||||
} finally {
|
||||
if (in != null) {
|
||||
try {
|
||||
in.close();
|
||||
} catch (IOException ex) {
|
||||
handleException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
return text.toString();
|
||||
}
|
||||
|
||||
public static double readDouble(Scanner sc) {
|
||||
while (!sc.hasNextDouble()) {
|
||||
System.out.println("Ошибка! Неверный формат!");
|
||||
sc.next();
|
||||
}
|
||||
return sc.nextDouble();
|
||||
}
|
||||
|
||||
public static Image readImageRes(String name) {
|
||||
try {
|
||||
InputStream is = getInputStream("/res/images/" + name);
|
||||
return ImageIO.read(is);
|
||||
} catch (IOException ex) {
|
||||
handleException(ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void handleException(Exception ex) {
|
||||
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