From b8ba7922af1cb07417b49d505e9ed4240c565b02 Mon Sep 17 00:00:00 2001 From: alterdekim Date: Sun, 7 Apr 2024 18:34:53 +0300 Subject: [PATCH] hash fixes --- .../javabot/components/BunkerBot.java | 4 ++-- .../com/alterdekim/javabot/util/BotUtils.java | 22 +++++++++---------- .../alterdekim/javabot/util/HashUtils.java | 8 +++++++ 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/alterdekim/javabot/components/BunkerBot.java b/src/main/java/com/alterdekim/javabot/components/BunkerBot.java index d51f83f..f09ad19 100644 --- a/src/main/java/com/alterdekim/javabot/components/BunkerBot.java +++ b/src/main/java/com/alterdekim/javabot/components/BunkerBot.java @@ -513,9 +513,9 @@ public class BunkerBot extends TelegramLongPollingBot { update.getCallbackQuery().getId() ) ); - if( update.getCallbackQuery().getData().equals(HashUtils.bytesToHex(Constants.JOIN_GAME_BTN.getBytes())) ) { + if( update.getCallbackQuery().getData().equals(HashUtils.getCRC32(Constants.JOIN_GAME_BTN.getBytes())) ) { joinGame(update.getCallbackQuery().getFrom(), 0); - } else if( update.getCallbackQuery().getData().equals(HashUtils.bytesToHex(Constants.START_GAME_BTN.getBytes())) ) { + } else if( update.getCallbackQuery().getData().equals(HashUtils.getCRC32(Constants.START_GAME_BTN.getBytes())) ) { if( isAdmin(update.getCallbackQuery().getFrom()) ) { startGame(); return; diff --git a/src/main/java/com/alterdekim/javabot/util/BotUtils.java b/src/main/java/com/alterdekim/javabot/util/BotUtils.java index fe51904..0780b9d 100644 --- a/src/main/java/com/alterdekim/javabot/util/BotUtils.java +++ b/src/main/java/com/alterdekim/javabot/util/BotUtils.java @@ -18,13 +18,13 @@ public class BotUtils { List> columns = new ArrayList<>(); InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); inlineKeyboardButton.setText(Constants.JOIN_GAME_BTN); - inlineKeyboardButton.setCallbackData(HashUtils.bytesToHex(Constants.JOIN_GAME_BTN.getBytes())); + inlineKeyboardButton.setCallbackData(HashUtils.getCRC32(Constants.JOIN_GAME_BTN.getBytes())); List keyboardButtonsRow1 = new ArrayList<>(); keyboardButtonsRow1.add(inlineKeyboardButton); columns.add(keyboardButtonsRow1); InlineKeyboardButton inlineKeyboardButton1 = new InlineKeyboardButton(); inlineKeyboardButton1.setText(Constants.START_GAME_BTN); - inlineKeyboardButton1.setCallbackData(HashUtils.bytesToHex(Constants.START_GAME_BTN.getBytes())); + inlineKeyboardButton1.setCallbackData(HashUtils.getCRC32(Constants.START_GAME_BTN.getBytes())); List keyboardButtonsRow2 = new ArrayList<>(); keyboardButtonsRow2.add(inlineKeyboardButton1); columns.add(keyboardButtonsRow2); @@ -38,37 +38,37 @@ public class BotUtils { if( !infoSections.getIsHobbyShowed() ) { InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); inlineKeyboardButton.setText(Constants.HOBBY_BTN); - inlineKeyboardButton.setCallbackData(HashUtils.bytesToHex(Constants.HOBBY_BTN.getBytes())); + inlineKeyboardButton.setCallbackData(HashUtils.getCRC32(Constants.HOBBY_BTN.getBytes())); columns.add(Collections.singletonList(inlineKeyboardButton)); } if( !infoSections.getIsAgeShowed() ) { InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); inlineKeyboardButton.setText(Constants.AGE_BTN); - inlineKeyboardButton.setCallbackData(HashUtils.bytesToHex(Constants.AGE_BTN.getBytes())); + inlineKeyboardButton.setCallbackData(HashUtils.getCRC32(Constants.AGE_BTN.getBytes())); columns.add(Collections.singletonList(inlineKeyboardButton)); } if( !infoSections.getIsGenderShowed() ) { InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); inlineKeyboardButton.setText(Constants.GENDER_BTN); - inlineKeyboardButton.setCallbackData(HashUtils.bytesToHex(Constants.GENDER_BTN.getBytes())); + inlineKeyboardButton.setCallbackData(HashUtils.getCRC32(Constants.GENDER_BTN.getBytes())); columns.add(Collections.singletonList(inlineKeyboardButton)); } if( !infoSections.getIsHealthShowed() ) { InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); inlineKeyboardButton.setText(Constants.HEALTH_BTN); - inlineKeyboardButton.setCallbackData(HashUtils.bytesToHex(Constants.HEALTH_BTN.getBytes())); + inlineKeyboardButton.setCallbackData(HashUtils.getCRC32(Constants.HEALTH_BTN.getBytes())); columns.add(Collections.singletonList(inlineKeyboardButton)); } if( !infoSections.getIsWorkShowed() ) { InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); inlineKeyboardButton.setText(Constants.WORK_BTN); - inlineKeyboardButton.setCallbackData(HashUtils.bytesToHex(Constants.WORK_BTN.getBytes())); + inlineKeyboardButton.setCallbackData(HashUtils.getCRC32(Constants.WORK_BTN.getBytes())); columns.add(Collections.singletonList(inlineKeyboardButton)); } if( !infoSections.getIsLuggageShowed() ) { InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); inlineKeyboardButton.setText(Constants.LUGG_BTN); - inlineKeyboardButton.setCallbackData(HashUtils.bytesToHex(Constants.LUGG_BTN.getBytes())); + inlineKeyboardButton.setCallbackData(HashUtils.getCRC32(Constants.LUGG_BTN.getBytes())); columns.add(Collections.singletonList(inlineKeyboardButton)); } inlineKeyboardMarkup.setKeyboard(columns); @@ -81,7 +81,7 @@ public class BotUtils { .map(s -> { InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); inlineKeyboardButton.setText(textDataValService.getTextDataValById(s.getTextNameId()).getText()); - inlineKeyboardButton.setCallbackData(HashUtils.bytesToHex(textDataValService.getTextDataValById(s.getTextNameId()).getText().getBytes())); + inlineKeyboardButton.setCallbackData(HashUtils.getCRC32(textDataValService.getTextDataValById(s.getTextNameId()).getText().getBytes())); return Collections.singletonList(inlineKeyboardButton); }).collect(Collectors.toList())); return inlineKeyboardMarkup; @@ -92,13 +92,13 @@ public class BotUtils { List> columns = new ArrayList<>(); InlineKeyboardButton inlineKeyboardButton = new InlineKeyboardButton(); inlineKeyboardButton.setText(Constants.PROBABILITY); - inlineKeyboardButton.setCallbackData(HashUtils.bytesToHex(Constants.PROBABILITY.getBytes())); + inlineKeyboardButton.setCallbackData(HashUtils.getCRC32(Constants.PROBABILITY.getBytes())); List keyboardButtonsRow1 = new ArrayList<>(); keyboardButtonsRow1.add(inlineKeyboardButton); columns.add(keyboardButtonsRow1); InlineKeyboardButton inlineKeyboardButton1 = new InlineKeyboardButton(); inlineKeyboardButton1.setText(Constants.DEATHMATCH); - inlineKeyboardButton1.setCallbackData(HashUtils.bytesToHex(Constants.DEATHMATCH.getBytes())); + inlineKeyboardButton1.setCallbackData(HashUtils.getCRC32(Constants.DEATHMATCH.getBytes())); List keyboardButtonsRow2 = new ArrayList<>(); keyboardButtonsRow2.add(inlineKeyboardButton1); columns.add(keyboardButtonsRow2); diff --git a/src/main/java/com/alterdekim/javabot/util/HashUtils.java b/src/main/java/com/alterdekim/javabot/util/HashUtils.java index a5878e2..a3faa82 100644 --- a/src/main/java/com/alterdekim/javabot/util/HashUtils.java +++ b/src/main/java/com/alterdekim/javabot/util/HashUtils.java @@ -4,6 +4,8 @@ import lombok.extern.slf4j.Slf4j; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; +import java.util.Locale; +import java.util.zip.CRC32; @Slf4j public class HashUtils { @@ -56,4 +58,10 @@ public class HashUtils { } return new String(hexChars, StandardCharsets.UTF_8); } + + public static String getCRC32(byte[] data){ + CRC32 fileCRC32 = new CRC32(); + fileCRC32.update(data); + return String.format(Locale.US,"%08X", fileCRC32.getValue()); + } }