diff --git a/LauncherCore/src/main/java/pro/gravit/utils/helper/CryptoHelper.java b/LauncherCore/src/main/java/pro/gravit/utils/helper/CryptoHelper.java new file mode 100644 index 00000000..727243a5 --- /dev/null +++ b/LauncherCore/src/main/java/pro/gravit/utils/helper/CryptoHelper.java @@ -0,0 +1,54 @@ +package pro.gravit.utils.helper; + +import java.util.Objects; +import java.util.function.LongSupplier; + +public final class CryptoHelper { + public static byte[] encode(byte[] txt, String pKey) { + Objects.requireNonNull(txt); + Objects.requireNonNull(pKey); + byte[] key = SecurityHelper.fromHex(pKey); + byte[] res = new byte[txt.length]; + for (int i = 0; i < txt.length; i++) + res[i] = (byte) (txt[i] ^ key[i % key.length]); + return res; + } + + public static byte[] decode(byte[] pText, String pKey) { + Objects.requireNonNull(pText); + Objects.requireNonNull(pKey); + byte[] res = new byte[pText.length]; + byte[] key = SecurityHelper.fromHex(pKey); + for (int i = 0; i < pText.length; i++) + res[i] = (byte) (pText[i] ^ key[i % key.length]); + return res; + } + + public static String randomToken(int depth) { + VerifyHelper.verifyInt(depth, VerifyHelper.POSITIVE, "Depth must be positive"); + StringBuilder sb = new StringBuilder(SecurityHelper.TOKEN_STRING_LENGTH*depth); + for (int i = 0; i < depth; i++) + sb.append(SecurityHelper.randomStringToken()); + return sb.toString(); + } + + private CryptoHelper() { + } + + public static class StaticRandom implements LongSupplier { + private volatile long rnd; + + public StaticRandom(long rnd) { + this.rnd = rnd; + } + + @Override + public long getAsLong() { + this.rnd ^= (this.rnd << 21); + this.rnd ^= (this.rnd >>> 35); + this.rnd ^= (this.rnd << 4); + return this.rnd; + } + } + +} diff --git a/LauncherCore/src/main/java/pro/gravit/utils/helper/SecurityHelper.java b/LauncherCore/src/main/java/pro/gravit/utils/helper/SecurityHelper.java index 6bd343ae..29d4a1f2 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/helper/SecurityHelper.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/helper/SecurityHelper.java @@ -485,7 +485,7 @@ public static byte[] decrypt(String seed, byte[] encrypted) throws Exception { return decrypt(getRawKey(seed.getBytes()), encrypted); } - public static byte[] HexToByte(String hexString) { + public static byte[] fromHex(String hexString) { int len = hexString.length() / 2; byte[] result = new byte[len]; for (int i = 0; i < len; i++) {