From 36b63958cc4612fbd632a482f1e780746ec6dabc Mon Sep 17 00:00:00 2001 From: 502647092 Date: Sun, 17 Jan 2016 22:45:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E9=83=A8=E5=88=86...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .../AuthMe/security/PasswordSecurity.java | 10 +++---- .../AuthMe/security/RandomString.java | 26 +++++++++++-------- 2 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/main/java/cn/citycraft/AuthMe/security/PasswordSecurity.java b/src/main/java/cn/citycraft/AuthMe/security/PasswordSecurity.java index b967363..3fa0c70 100644 --- a/src/main/java/cn/citycraft/AuthMe/security/PasswordSecurity.java +++ b/src/main/java/cn/citycraft/AuthMe/security/PasswordSecurity.java @@ -35,7 +35,7 @@ public class PasswordSecurity { method = event.getMethod(); if (method == null) { - throw new NoSuchAlgorithmException("Unknown hash algorithm"); + throw new NoSuchAlgorithmException("未知的加密算法..."); } if (method.comparePassword(hash, password, playerName)) { @@ -48,7 +48,7 @@ public class PasswordSecurity { } } } catch (InstantiationException | IllegalAccessException e) { - throw new NoSuchAlgorithmException("Problem with this hash algorithm"); + throw new NoSuchAlgorithmException("加密算法" + algo.name() + "运行时出现问题..."); } return false; } @@ -71,7 +71,7 @@ public class PasswordSecurity { method = null; } } catch (InstantiationException | IllegalAccessException e) { - throw new NoSuchAlgorithmException("Problem with this hash algorithm"); + throw new NoSuchAlgorithmException("加密算法" + alg.name() + "运行时出现问题..."); } String salt = ""; switch (alg) { @@ -150,13 +150,13 @@ public class PasswordSecurity { case CUSTOM: break; default: - throw new NoSuchAlgorithmException("Unknown hash algorithm"); + throw new NoSuchAlgorithmException("未知的加密算法..."); } final PasswordEncryptionEvent event = new PasswordEncryptionEvent(method, playerName); Bukkit.getPluginManager().callEvent(event); method = event.getMethod(); if (method == null) { - throw new NoSuchAlgorithmException("Unknown hash algorithm"); + throw new NoSuchAlgorithmException("未知的加密算法..."); } return method.getHash(password, salt, playerName); } diff --git a/src/main/java/cn/citycraft/AuthMe/security/RandomString.java b/src/main/java/cn/citycraft/AuthMe/security/RandomString.java index a316eda..75a0027 100644 --- a/src/main/java/cn/citycraft/AuthMe/security/RandomString.java +++ b/src/main/java/cn/citycraft/AuthMe/security/RandomString.java @@ -11,27 +11,31 @@ public class RandomString { private static final char[] chars = new char[36]; + static { + for (int idx = 0; idx < 10; ++idx) { + chars[idx] = (char) ('0' + idx); + } + for (int idx = 10; idx < 36; ++idx) { + chars[idx] = (char) ('a' + idx - 10); + } + } + private final char[] buf; private final Random random = new Random(); - public RandomString(int length) { - if (length < 1) - throw new IllegalArgumentException("length < 1: " + length); + public RandomString(final int length) { + if (length < 1) { + throw new IllegalArgumentException("随机字符串长度小于1: " + length); + } buf = new char[length]; random.setSeed(Calendar.getInstance().getTimeInMillis()); } - static { - for (int idx = 0; idx < 10; ++idx) - chars[idx] = (char) ('0' + idx); - for (int idx = 10; idx < 36; ++idx) - chars[idx] = (char) ('a' + idx - 10); - } - public String nextString() { - for (int idx = 0; idx < buf.length; ++idx) + for (int idx = 0; idx < buf.length; ++idx) { buf[idx] = chars[random.nextInt(chars.length)]; + } return new String(buf); }