From dc6c159db3b7184c4180957be4f1f7ce7bf3fe81 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sun, 27 Aug 2023 11:02:50 +0700 Subject: [PATCH] section signs validation for the funny reason --- .../chomens_bot/commands/ValidateCommand.java | 11 ++++++-- .../plugins/CommandHandlerPlugin.java | 10 +++++--- .../chomens_bot/plugins/HashingPlugin.java | 25 ++++++++++++++++--- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ValidateCommand.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ValidateCommand.java index 5c1284c..c992bc9 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ValidateCommand.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/commands/ValidateCommand.java @@ -25,8 +25,15 @@ public class ValidateCommand extends Command { final String hash = fullArgs[0]; - if (hash.equals(bot.hashing.getHash(context.splitInput[0], context.sender))) return Component.text("Valid hash").color(NamedTextColor.GREEN); - else if (hash.equals(bot.hashing.getOwnerHash(context.splitInput[0], context.sender))) return Component.text("Valid OwnerHash").color(NamedTextColor.GREEN); + if ( + hash.equals(bot.hashing.getHash(context.splitInput[0], context.sender, true)) || + hash.equals(bot.hashing.getHash(context.splitInput[0], context.sender, false)) + ) return Component.text("Valid hash").color(NamedTextColor.GREEN); + + else if ( + hash.equals(bot.hashing.getOwnerHash(context.splitInput[0], context.sender, true)) || + hash.equals(bot.hashing.getOwnerHash(context.splitInput[0], context.sender, false)) + ) return Component.text("Valid OwnerHash").color(NamedTextColor.GREEN); return null; } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java index f5de2e8..46d1feb 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/CommandHandlerPlugin.java @@ -143,13 +143,17 @@ public class CommandHandlerPlugin { } else { if ( command.trustLevel == TrustLevel.TRUSTED && - !userHash.equals(bot.hashing.getHash(splitInput[0], context.sender)) && - !userHash.equals(bot.hashing.getOwnerHash(splitInput[0], context.sender)) + // mess? + !userHash.equals(bot.hashing.getHash(splitInput[0], context.sender, true)) && + !userHash.equals(bot.hashing.getOwnerHash(splitInput[0], context.sender, true)) && + !userHash.equals(bot.hashing.getHash(splitInput[0], context.sender, false)) && + !userHash.equals(bot.hashing.getOwnerHash(splitInput[0], context.sender, false)) ) return Component.text("Invalid hash").color(NamedTextColor.RED); if ( command.trustLevel == TrustLevel.OWNER && - !userHash.equals(bot.hashing.getOwnerHash(splitInput[0], context.sender)) + !userHash.equals(bot.hashing.getOwnerHash(splitInput[0], context.sender, false)) && + !userHash.equals(bot.hashing.getOwnerHash(splitInput[0], context.sender, true)) ) return Component.text("Invalid OwnerHash").color(NamedTextColor.RED); } } diff --git a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/HashingPlugin.java b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/HashingPlugin.java index 9bfd6ca..498e8ff 100644 --- a/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/HashingPlugin.java +++ b/src/main/java/land/chipmunk/chayapak/chomens_bot/plugins/HashingPlugin.java @@ -5,6 +5,7 @@ import land.chipmunk.chayapak.chomens_bot.Bot; import land.chipmunk.chayapak.chomens_bot.data.chat.PlayerEntry; import java.nio.charset.StandardCharsets; +import java.util.Arrays; public class HashingPlugin { private final Bot bot; @@ -13,29 +14,45 @@ public class HashingPlugin { this.bot = bot; } - public String getHash (String prefix, PlayerEntry sender) { + public String getHash (String prefix, PlayerEntry sender, boolean sectionSigns) { final long time = System.currentTimeMillis() / 5_000; final String key = bot.config.keys.normalKey; final String hashValue = sender.profile.getIdAsString() + prefix + time + key; - return Hashing.sha256() + final String hash = Hashing.sha256() .hashString(hashValue, StandardCharsets.UTF_8) .toString() .substring(0, 16); + + return sectionSigns ? + String.join("", + Arrays.stream(hash.split("")) + .map((letter) -> "§" + letter) + .toArray(String[]::new) + ) : + hash; } - public String getOwnerHash (String prefix, PlayerEntry sender) { + public String getOwnerHash (String prefix, PlayerEntry sender, boolean sectionSigns) { final long time = System.currentTimeMillis() / 5_000; final String key = bot.config.keys.ownerKey; final String value = sender.profile.getIdAsString() + prefix + time + key; - return Hashing.sha256() + final String hash = Hashing.sha256() .hashString(value, StandardCharsets.UTF_8) .toString() .substring(0, 16); + + return sectionSigns ? + String.join("", + Arrays.stream(hash.split("")) + .map((letter) -> "§" + letter) + .toArray(String[]::new) + ) : + hash; } }