Skip to content

Commit

Permalink
Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
shulng committed Aug 29, 2024
1 parent dade733 commit b46da90
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 105 deletions.
76 changes: 76 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<kotlin.version>2.0.20</kotlin.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -102,6 +103,17 @@
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<!-- Spigot -->
Expand Down Expand Up @@ -200,6 +212,70 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/main/java</source>
<source>target/generated-sources/annotations</source>
</sourceDirs>
</configuration>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
<configuration>
<sourceDirs>
<source>src/test/java</source>
<source>target/generated-test-sources/test-annotations</source>
</sourceDirs>
</configuration>
</execution>
</executions>
<configuration>
<jvmTarget>${maven.compiler.target}</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
<execution>
<id>default-testCompile</id>
<phase>none</phase>
</execution>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/cc/baka9/catseedlogin/util/CommunicationAuth.kt
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()
}
}
65 changes: 34 additions & 31 deletions src/main/java/cc/baka9/catseedlogin/util/Crypt.kt
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)
}
}

}
52 changes: 23 additions & 29 deletions src/main/java/cc/baka9/catseedlogin/util/Mail.kt
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()
}

}
58 changes: 30 additions & 28 deletions src/main/java/cc/baka9/catseedlogin/util/Util.kt
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")
}
}

0 comments on commit b46da90

Please sign in to comment.