From 198e2a989fc8aa11e7e2b52d7efe596d6193ade5 Mon Sep 17 00:00:00 2001 From: Andrei Arlou Date: Sat, 23 Nov 2019 22:31:46 +0300 Subject: [PATCH] KEYCLOAK-12217 Use StandartCharsets in module "server-spi-private" --- .../java/org/keycloak/crypto/HashProvider.java | 10 +++------- .../keycloak/models/utils/SHAPasswordEncoder.java | 14 +++----------- 2 files changed, 6 insertions(+), 18 deletions(-) diff --git a/server-spi-private/src/main/java/org/keycloak/crypto/HashProvider.java b/server-spi-private/src/main/java/org/keycloak/crypto/HashProvider.java index d3201952ac..a5616b69c8 100644 --- a/server-spi-private/src/main/java/org/keycloak/crypto/HashProvider.java +++ b/server-spi-private/src/main/java/org/keycloak/crypto/HashProvider.java @@ -17,7 +17,7 @@ package org.keycloak.crypto; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import org.keycloak.provider.Provider; @@ -28,12 +28,8 @@ public interface HashProvider extends Provider { default byte[] hash(String input) throws HashException { - try { - byte[] inputBytes = input.getBytes("UTF-8"); - return hash(inputBytes); - } catch (UnsupportedEncodingException e) { - throw new HashException("Unsupported encoding when trying to hash", e); - } + byte[] inputBytes = input.getBytes(StandardCharsets.UTF_8); + return hash(inputBytes); } diff --git a/server-spi-private/src/main/java/org/keycloak/models/utils/SHAPasswordEncoder.java b/server-spi-private/src/main/java/org/keycloak/models/utils/SHAPasswordEncoder.java index 8b4c25a535..156177c413 100755 --- a/server-spi-private/src/main/java/org/keycloak/models/utils/SHAPasswordEncoder.java +++ b/server-spi-private/src/main/java/org/keycloak/models/utils/SHAPasswordEncoder.java @@ -19,7 +19,7 @@ package org.keycloak.models.utils; import org.keycloak.common.util.Base64; -import java.io.UnsupportedEncodingException; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; @@ -45,16 +45,8 @@ public class SHAPasswordEncoder { public String encode(String rawPassword) { MessageDigest messageDigest = getMessageDigest(); - String encodedPassword = null; - - try { - byte[] digest = messageDigest.digest(rawPassword.getBytes("UTF-8")); - encodedPassword = Base64.encodeBytes(digest); - } catch (UnsupportedEncodingException e) { - throw new RuntimeException("Credential could not be encoded"); - } - - return encodedPassword; + byte[] digest = messageDigest.digest(rawPassword.getBytes(StandardCharsets.UTF_8)); + return Base64.encodeBytes(digest); } public boolean verify(String rawPassword, String encodedPassword) {