Avoid UnsupportedEncodingException & use const from StandardCharsets (#11948)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp
2022-01-03 16:05:08 +01:00
committed by GitHub
parent 3f54327d5a
commit 167f8ebc49
52 changed files with 180 additions and 414 deletions

View File

@@ -12,7 +12,6 @@
*/
package org.openhab.binding.loxone.internal.security;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
@@ -238,11 +237,7 @@ class LxWsSecurityToken extends LxWsSecurity {
try {
String encrypted = Base64.getEncoder()
.encodeToString(aesEncryptCipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
try {
encrypted = URLEncoder.encode(encrypted, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.warn("[{}] Unsupported encoding for encrypted command conversion to URL.", debugId);
}
encrypted = URLEncoder.encode(encrypted, StandardCharsets.UTF_8);
return CMD_ENCRYPT_CMD + encrypted;
} catch (IllegalBlockSizeException | BadPaddingException e) {
logger.warn("[{}] Command encryption failed: {}", debugId, e.getMessage());
@@ -260,7 +255,7 @@ class LxWsSecurityToken extends LxWsSecurity {
try {
byte[] bytes = Base64.getDecoder().decode(string);
bytes = aesDecryptCipher.doFinal(bytes);
string = new String(bytes, "UTF-8");
string = new String(bytes, StandardCharsets.UTF_8);
string = string.replaceAll("\0+.*$", "");
string = string.replaceFirst("^salt/[^/]*/", "");
string = string.replaceFirst("^nextSalt/[^/]*/[^/]*/", "");
@@ -269,8 +264,6 @@ class LxWsSecurityToken extends LxWsSecurity {
logger.debug("[{}] Failed to decode base64 string: {}", debugId, string);
} catch (IllegalBlockSizeException | BadPaddingException e) {
logger.warn("[{}] Command decryption failed: {}", debugId, e.getMessage());
} catch (UnsupportedEncodingException e) {
logger.warn("[{}] Unsupported encoding for decrypted bytes to string conversion.", debugId);
}
return string;
}
@@ -544,11 +537,7 @@ class LxWsSecurityToken extends LxWsSecurity {
byte[] bytes = new byte[SALT_BYTES];
secureRandom.nextBytes(bytes);
String salt = HexUtils.bytesToHex(bytes);
try {
salt = URLEncoder.encode(salt, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.warn("[{}] Unsupported encoding for salt conversion to URL.", debugId);
}
salt = URLEncoder.encode(salt, StandardCharsets.UTF_8);
saltTimeStamp = timeElapsedInSeconds();
saltUseCount = 0;
logger.debug("[{}] Generated salt: {}", debugId, salt);