From 9938d4649b51d2be7a8b1f13862a1c3cfb8de4ff Mon Sep 17 00:00:00 2001 From: Rodrigo Verdiani Date: Tue, 14 Jul 2026 14:11:44 -0300 Subject: [PATCH] refactor(image): extract shared SHA-256 hash utility Move computeHash from ImageFetchService into reusable ImageHashUtil to avoid duplication when other services need image hashing. --- .../image/service/ImageFetchService.java | 22 ++-------------- .../magamochi/image/util/ImageHashUtil.java | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/magamochi/image/util/ImageHashUtil.java diff --git a/src/main/java/com/magamochi/image/service/ImageFetchService.java b/src/main/java/com/magamochi/image/service/ImageFetchService.java index 4849a0c..68c9037 100644 --- a/src/main/java/com/magamochi/image/service/ImageFetchService.java +++ b/src/main/java/com/magamochi/image/service/ImageFetchService.java @@ -4,12 +4,12 @@ import static java.util.Objects.nonNull; import com.google.common.util.concurrent.RateLimiter; import com.magamochi.common.model.enumeration.ContentType; +import com.magamochi.image.util.ImageHashUtil; import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; -import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.UUID; import lombok.RequiredArgsConstructor; @@ -54,7 +54,7 @@ public class ImageFetchService { throws NoSuchAlgorithmException { var fileContentType = resolveContentType(httpResponse, imageBytes); - var fileHash = computeHash(imageBytes); + var fileHash = ImageHashUtil.computeSha256(imageBytes); return imageManagerService.upload( imageBytes, fileContentType, contentType.name().toLowerCase(), fileHash); @@ -76,22 +76,4 @@ public class ImageFetchService { return tika.detect(fileBytes); } - - private String computeHash(byte[] content) throws NoSuchAlgorithmException { - var digest = MessageDigest.getInstance("SHA-256"); - var hashBytes = digest.digest(content); - var hexString = new StringBuilder(2 * hashBytes.length); - - for (byte b : hashBytes) { - var hex = Integer.toHexString(0xff & b); - - if (hex.length() == 1) { - hexString.append('0'); - } - - hexString.append(hex); - } - - return hexString.toString(); - } } diff --git a/src/main/java/com/magamochi/image/util/ImageHashUtil.java b/src/main/java/com/magamochi/image/util/ImageHashUtil.java new file mode 100644 index 0000000..48ad9fc --- /dev/null +++ b/src/main/java/com/magamochi/image/util/ImageHashUtil.java @@ -0,0 +1,26 @@ +package com.magamochi.image.util; + +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = lombok.AccessLevel.PRIVATE) +public class ImageHashUtil { + public static String computeSha256(byte[] content) throws NoSuchAlgorithmException { + var digest = MessageDigest.getInstance("SHA-256"); + var hashBytes = digest.digest(content); + var hexString = new StringBuilder(2 * hashBytes.length); + + for (byte b : hashBytes) { + var hex = Integer.toHexString(0xff & b); + + if (hex.length() == 1) { + hexString.append('0'); + } + + hexString.append(hex); + } + + return hexString.toString(); + } +}