-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
180 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 17 additions & 17 deletions
34
src/main/java/cc/baka9/catseedlogin/util/CommunicationAuth.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,27 @@ | ||
package cc.baka9.catseedlogin.util; | ||
package cc.baka9.catseedlogin.util | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.MessageDigest | ||
import java.security.NoSuchAlgorithmException | ||
|
||
public class CommunicationAuth { | ||
object CommunicationAuth { | ||
private var messageDigest: MessageDigest? = null | ||
|
||
private static final MessageDigest messageDigest; | ||
|
||
static { | ||
init { | ||
try { | ||
messageDigest = MessageDigest.getInstance("SHA-256"); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new RuntimeException(e); | ||
messageDigest = MessageDigest.getInstance("SHA-256") | ||
} catch (e: NoSuchAlgorithmException) { | ||
throw RuntimeException(e) | ||
} | ||
} | ||
|
||
public static String encryption(String... args) { | ||
String paramString = String.join("", args); | ||
byte[] arrayOfByte = messageDigest.digest(paramString.getBytes()); | ||
StringBuilder stringBuilder = new StringBuilder(arrayOfByte.length * 2); | ||
for (byte value : arrayOfByte) { | ||
stringBuilder.append(String.format("%02x", value & 0xff)); | ||
@JvmStatic | ||
fun encryption(vararg args: String?): String { | ||
val paramString = java.lang.String.join("", *args) | ||
val arrayOfByte = messageDigest!!.digest(paramString.toByteArray()) | ||
val stringBuilder = StringBuilder(arrayOfByte.size * 2) | ||
for (value in arrayOfByte) { | ||
stringBuilder.append(String.format("%02x", value.toInt() and 0xff)) | ||
} | ||
return stringBuilder.toString(); | ||
return stringBuilder.toString() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,44 @@ | ||
package cc.baka9.catseedlogin.util; | ||
package cc.baka9.catseedlogin.util | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.nio.charset.StandardCharsets | ||
import java.security.MessageDigest | ||
import java.security.NoSuchAlgorithmException | ||
|
||
public class Crypt { | ||
|
||
private static final char[] CRYPTCHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; | ||
|
||
public static String encrypt(final String name, final String password){ | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("ÜÄaeut//&/=I ").append(password).append("7421€547").append(name).append("__+IÄIH§%NK ").append(password); | ||
String text = sb.toString(); | ||
try { | ||
final MessageDigest md = MessageDigest.getInstance("SHA-512"); | ||
md.update(text.getBytes(StandardCharsets.UTF_8), 0, text.length()); | ||
return byteArrayToHexString(md.digest()); | ||
} catch (final NoSuchAlgorithmException e) { | ||
return null; | ||
class Crypt { | ||
fun match(name: String?, password: String?, encrypted: String): Boolean { | ||
return try { | ||
encrypted == encrypt(name, password) | ||
} catch (e: Exception) { | ||
false | ||
} | ||
} | ||
|
||
public static String byteArrayToHexString(byte[] args){ | ||
char[] chars = new char[args.length * 2]; | ||
for (int i = 0; i < args.length; i++) { | ||
chars[(i * 2)] = CRYPTCHARS[(args[i] >> 4 & 0xF)]; | ||
chars[(i * 2 + 1)] = CRYPTCHARS[(args[i] & 0xF)]; | ||
companion object { | ||
private val CRYPTCHARS = | ||
charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f') | ||
|
||
@JvmStatic | ||
fun encrypt(name: String?, password: String?): String? { | ||
val sb = StringBuilder() | ||
sb.append("ÜÄaeut//&/=I ").append(password).append("7421€547").append(name).append("__+IÄIH§%NK ") | ||
.append(password) | ||
val text = sb.toString() | ||
try { | ||
val md = MessageDigest.getInstance("SHA-512") | ||
md.update(text.toByteArray(StandardCharsets.UTF_8), 0, text.length) | ||
return byteArrayToHexString(md.digest()) | ||
} catch (e: NoSuchAlgorithmException) { | ||
return null | ||
} | ||
} | ||
return new String(chars); | ||
} | ||
|
||
public boolean match(final String name, final String password, final String encrypted){ | ||
try { | ||
return encrypted.equals(encrypt(name, password)); | ||
} catch (final Exception e) { | ||
return false; | ||
fun byteArrayToHexString(args: ByteArray): String { | ||
val chars = CharArray(args.size * 2) | ||
for (i in args.indices) { | ||
chars[i * 2] = CRYPTCHARS[args[i].toInt() shr 4 and 0xF] | ||
chars[i * 2 + 1] = CRYPTCHARS[args[i].toInt() and 0xF] | ||
} | ||
return String(chars) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,28 @@ | ||
package cc.baka9.catseedlogin.util; | ||
|
||
import org.apache.commons.mail.DefaultAuthenticator; | ||
import org.apache.commons.mail.Email; | ||
import org.apache.commons.mail.HtmlEmail; | ||
|
||
import cc.baka9.catseedlogin.bukkit.Config; | ||
|
||
public class Mail { | ||
|
||
private Mail() { | ||
} | ||
|
||
public static void sendMail(String receiveMailAccount, String subject, String content) throws Exception { | ||
|
||
Email email = new HtmlEmail(); | ||
email.setHostName(Config.EmailVerify.EmailSmtpHost); | ||
email.setSmtpPort(Integer.parseInt(Config.EmailVerify.EmailSmtpPort)); | ||
email.setAuthenticator(new DefaultAuthenticator(Config.EmailVerify.EmailAccount, Config.EmailVerify.EmailPassword)); | ||
package cc.baka9.catseedlogin.util | ||
|
||
import cc.baka9.catseedlogin.bukkit.Config | ||
import org.apache.commons.mail.DefaultAuthenticator | ||
import org.apache.commons.mail.Email | ||
import org.apache.commons.mail.HtmlEmail | ||
|
||
object Mail { | ||
@JvmStatic | ||
@Throws(Exception::class) | ||
fun sendMail(receiveMailAccount: String?, subject: String?, content: String?) { | ||
val email: Email = HtmlEmail() | ||
email.hostName = Config.EmailVerify.EmailSmtpHost | ||
email.setSmtpPort(Config.EmailVerify.EmailSmtpPort.toInt()) | ||
email.setAuthenticator(DefaultAuthenticator(Config.EmailVerify.EmailAccount, Config.EmailVerify.EmailPassword)) | ||
if (Config.EmailVerify.SSLAuthVerify) { | ||
email.setSSLOnConnect(true); | ||
email.setSSLOnConnect(true) | ||
} else { | ||
email.setStartTLSEnabled(true); | ||
email.setStartTLSEnabled(true) | ||
} | ||
email.setFrom(Config.EmailVerify.EmailAccount, Config.EmailVerify.FromPersonal); | ||
email.setSubject(subject); | ||
email.setMsg(content); | ||
email.addTo(receiveMailAccount); | ||
email.setCharset("UTF-8"); | ||
email.send(); | ||
|
||
email.setFrom(Config.EmailVerify.EmailAccount, Config.EmailVerify.FromPersonal) | ||
email.setSubject(subject) | ||
email.setMsg(content) | ||
email.addTo(receiveMailAccount) | ||
email.setCharset("UTF-8") | ||
email.send() | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,40 @@ | ||
package cc.baka9.catseedlogin.util; | ||
|
||
import java.security.SecureRandom; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.Random; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.commons.lang3.RandomStringUtils; | ||
|
||
public class Util { | ||
private static final Pattern passwordDifficultyRegex = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$"); | ||
private static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); | ||
private static final Random random = new SecureRandom(); | ||
|
||
public static boolean passwordIsDifficulty(String pwd) { | ||
return passwordDifficultyRegex.matcher(pwd).find(); | ||
package cc.baka9.catseedlogin.util | ||
|
||
import org.apache.commons.lang3.RandomStringUtils | ||
import java.security.SecureRandom | ||
import java.text.SimpleDateFormat | ||
import java.util.* | ||
import java.util.regex.Pattern | ||
|
||
object Util { | ||
private val passwordDifficultyRegex: Pattern = Pattern.compile("^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$") | ||
private val sdf = SimpleDateFormat("yyyy-MM-dd HH:mm:ss") | ||
private val random: Random = SecureRandom() | ||
|
||
@JvmStatic | ||
fun passwordIsDifficulty(pwd: String?): Boolean { | ||
return passwordDifficultyRegex.matcher(pwd).find() | ||
} | ||
|
||
public static String time2Str(long time) { | ||
synchronized (sdf) { | ||
return sdf.format(new Date(time)); | ||
fun time2Str(time: Long): String { | ||
synchronized(sdf) { | ||
return sdf.format(Date(time)) | ||
} | ||
} | ||
|
||
public static boolean checkMail(String e_mail) { | ||
return e_mail.matches("[a-zA-Z0-9_]+@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)+"); | ||
@JvmStatic | ||
fun checkMail(e_mail: String): Boolean { | ||
return e_mail.matches("[a-zA-Z0-9_]+@[a-zA-Z0-9_]+(\\.[a-zA-Z0-9_]+)+".toRegex()) | ||
} | ||
|
||
public static String randomStr() { | ||
return RandomStringUtils.randomAlphanumeric(10); | ||
@JvmStatic | ||
fun randomStr(): String { | ||
return RandomStringUtils.randomAlphanumeric(10) | ||
} | ||
|
||
public static boolean isOSLinux() { | ||
String os = System.getProperty("os.name"); | ||
return os != null && os.toLowerCase().contains("linux"); | ||
} | ||
val isOSLinux: Boolean | ||
get() { | ||
val os = System.getProperty("os.name") | ||
return os != null && os.lowercase(Locale.getDefault()).contains("linux") | ||
} | ||
} |