From 0965fc50072495776c5dd0aea89d89dd60b7e83b Mon Sep 17 00:00:00 2001 From: Kavin <20838718+FireMasterK@users.noreply.github.com> Date: Mon, 30 Jan 2023 16:57:48 +0000 Subject: [PATCH] Cleanup unnecessary hash function. --- .../kavin/piped/utils/SponsorBlockUtils.java | 22 ++----------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java b/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java index bb86d73..6a57b94 100644 --- a/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java +++ b/src/main/java/me/kavin/piped/utils/SponsorBlockUtils.java @@ -7,11 +7,10 @@ import com.grack.nanojson.JsonWriter; import me.kavin.piped.consts.Constants; import me.kavin.piped.utils.resp.InvalidRequestResponse; import me.kavin.piped.utils.resp.SimpleErrorMessage; +import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class SponsorBlockUtils { @@ -22,7 +21,7 @@ public class SponsorBlockUtils { if (StringUtils.isEmpty(categories)) return Constants.mapper.writeValueAsString(new InvalidRequestResponse()); - String hash = toSha256(id); + String hash = DigestUtils.sha256Hex(id); for (String url : Constants.SPONSORBLOCK_SERVERS) { try { @@ -46,21 +45,4 @@ public class SponsorBlockUtils { return null; } - private static String toSha256(final String videoId) throws NoSuchAlgorithmException { - final MessageDigest digest = MessageDigest.getInstance("SHA-256"); - final byte[] bytes = digest.digest(videoId.getBytes(StandardCharsets.UTF_8)); - final StringBuilder sb = new StringBuilder(); - - for (final byte b : bytes) { - final String hex = Integer.toHexString(0xff & b); - - if (hex.length() == 1) { - sb.append('0'); - } - - sb.append(hex); - } - - return sb.toString(); - } }