Skip to content

Commit e800cde

Browse files
committed
[feature/handle-similar-messages-as-scam] Added a method to hash strings and refactored existing code
1 parent 27f8563 commit e800cde

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

application/src/main/java/org/togetherjava/tjbot/features/moderation/scam/ScamHistoryStore.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.togetherjava.tjbot.db.generated.tables.records.ScamHistoryRecord;
88
import org.togetherjava.tjbot.features.utils.Hashing;
99

10-
import java.nio.charset.StandardCharsets;
1110
import java.time.Duration;
1211
import java.time.Instant;
1312
import java.util.Collection;
@@ -138,8 +137,7 @@ public void deleteHistoryOlderThan(Instant olderThan) {
138137
* @return a text representation of the hash
139138
*/
140139
public static String hashMessageContent(Message message) {
141-
return Hashing.bytesToHex(Hashing.hash(HASH_METHOD,
142-
message.getContentRaw().getBytes(StandardCharsets.UTF_8)));
140+
return Hashing.bytesToHex(Hashing.hashUTF8(HASH_METHOD, message.getContentRaw()));
143141
}
144142

145143
/**

application/src/main/java/org/togetherjava/tjbot/features/utils/Hashing.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,16 @@ public static byte[] hash(String method, byte[] data) {
5757
throw new IllegalStateException("Hash method must be supported", e);
5858
}
5959
}
60+
61+
/**
62+
* Hashes the given UTF 8 text using the given method, see {@link Hashing#hash(String, byte[])}.
63+
*
64+
* @param method the method to use for hashing, must be supported by {@link MessageDigest}, e.g.
65+
* {@code "SHA"}
66+
* @param text the UTF 8 text to hash
67+
* @return the computed hash
68+
*/
69+
public static byte[] hashUTF8(String method, String text) {
70+
return hash(method, text.getBytes(StandardCharsets.UTF_8));
71+
}
6072
}

0 commit comments

Comments
 (0)