feat: extracted Base64.Decoder

This commit is contained in:
zihluwang
2025-02-15 11:00:57 +08:00
parent 952a329047
commit fb8d6e3476
@@ -57,12 +57,15 @@ public class EcKeyLoader implements KeyLoader {
private final KeyFactory keyFactory; private final KeyFactory keyFactory;
private final Base64.Decoder decoder;
/** /**
* Initialise a key loader for EC-based algorithms. * Initialise a key loader for EC-based algorithms.
*/ */
public EcKeyLoader() { public EcKeyLoader() {
try { try {
this.keyFactory = KeyFactory.getInstance("EC"); this.keyFactory = KeyFactory.getInstance("EC");
this.decoder = Base64.getDecoder();
} catch (NoSuchAlgorithmException e) { } catch (NoSuchAlgorithmException e) {
throw new KeyLoadingException(e); throw new KeyLoadingException(e);
} }
@@ -84,7 +87,7 @@ public class EcKeyLoader implements KeyLoader {
.replaceAll("-----BEGIN (EC )?PRIVATE KEY-----", "") .replaceAll("-----BEGIN (EC )?PRIVATE KEY-----", "")
.replaceAll("-----END (EC )?PRIVATE KEY-----", "") .replaceAll("-----END (EC )?PRIVATE KEY-----", "")
.replaceAll("\n", ""); .replaceAll("\n", "");
var decodedKeyString = Base64.getDecoder().decode(pemKeyText); var decodedKeyString = decoder.decode(pemKeyText);
var keySpec = new PKCS8EncodedKeySpec(decodedKeyString); var keySpec = new PKCS8EncodedKeySpec(decodedKeyString);
var _key = keyFactory.generatePrivate(keySpec); var _key = keyFactory.generatePrivate(keySpec);
@@ -114,7 +117,7 @@ public class EcKeyLoader implements KeyLoader {
.replaceAll("-----BEGIN (EC )?PUBLIC KEY-----", "") .replaceAll("-----BEGIN (EC )?PUBLIC KEY-----", "")
.replaceAll("-----END (EC )?PUBLIC KEY-----", "") .replaceAll("-----END (EC )?PUBLIC KEY-----", "")
.replaceAll("\n", ""); .replaceAll("\n", "");
var keyBytes = Base64.getDecoder().decode(pemKeyText); var keyBytes = decoder.decode(pemKeyText);
var spec = new X509EncodedKeySpec(keyBytes); var spec = new X509EncodedKeySpec(keyBytes);
var key = keyFactory.generatePublic(spec); var key = keyFactory.generatePublic(spec);
if (key instanceof ECPublicKey publicKey) { if (key instanceof ECPublicKey publicKey) {