feat: extracted ECDSA key pair loader

This commit is contained in:
zihluwang
2025-01-26 18:47:46 +08:00
parent e28559a5f9
commit 5f9ea34af0
3 changed files with 137 additions and 77 deletions
@@ -18,105 +18,38 @@
package com.onixbyte.security;
import com.onixbyte.security.exception.KeyLoadingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
* The {@code KeyLoader} class provides utility methods for loading ECDSA keys from PEM-formatted
* The {@code KeyLoader} class provides utility methods for loading keys pairs from PEM-formatted
* key text. This class supports loading both private and public keys.
* <p>
* The utility methods in this class are useful for scenarios where ECDSA keys need to be loaded
* from PEM-formatted strings for cryptographic operations.
* </p>
*
* <p><b>Example usage:</b></p>
* <pre>{@code
* String pemPrivateKey = """
* -----BEGIN PRIVATE KEY-----
* ...
* -----END PRIVATE KEY-----""";
* ECPrivateKey privateKey = KeyLoader.loadEcdsaPrivateKey(pemPrivateKey);
*
* String pemPublicKey = """
* -----BEGIN PUBLIC KEY-----
* ...
* -----END PUBLIC KEY-----""";
* ECPublicKey publicKey = KeyLoader.loadEcdsaPublicKey(pemPublicKey);
* }</pre>
*
* @author zihluwang
* @version 1.6.0
* @since 1.6.0
*/
public class KeyLoader {
private final static Logger log = LoggerFactory.getLogger(KeyLoader.class);
public interface KeyLoader {
/**
* Private constructor prevents from being initialised.
*/
private KeyLoader() {
}
/**
* Load ECDSA private key from pem-formatted key text.
* Load private key from pem-formatted key text.
*
* @param pemKeyText pem-formatted key text
* @return loaded private key
* @throws KeyLoadingException if the generated key is not a {@link ECPrivateKey} instance,
* or EC Key Factory is not loaded, or key spec is invalid
*/
public static ECPrivateKey loadEcdsaPrivateKey(String pemKeyText) {
try {
var decodedKeyString = Base64.getDecoder().decode(pemKeyText);
var keySpec = new PKCS8EncodedKeySpec(decodedKeyString);
var keyFactory = KeyFactory.getInstance("EC");
var _key = keyFactory.generatePrivate(keySpec);
if (_key instanceof ECPrivateKey privateKey) {
return privateKey;
} else {
throw new KeyLoadingException("Unable to load private key from pem-formatted key text.");
}
} catch (NoSuchAlgorithmException e) {
throw new KeyLoadingException("Cannot get EC Key Factory.", e);
} catch (InvalidKeySpecException e) {
throw new KeyLoadingException("Key spec is invalid.", e);
}
}
PrivateKey loadPrivateKey(String pemKeyText);
/**
* Load ECDSA public key from pem-formatted key text.
* Load public key from pem-formatted key text.
*
* @param pemKeyText pem-formatted key text
* @return loaded private key
* @throws KeyLoadingException if the generated key is not a {@link ECPrivateKey} instance,
* or EC Key Factory is not loaded, or key spec is invalid
*/
public static ECPublicKey loadEcdsaPublicKey(String pemKeyText) {
try {
var keyBytes = Base64.getDecoder().decode(pemKeyText);
var spec = new X509EncodedKeySpec(keyBytes);
var keyFactory = KeyFactory.getInstance("EC");
var key = keyFactory.generatePublic(spec);
if (key instanceof ECPublicKey publicKey) {
return publicKey;
} else {
throw new KeyLoadingException("Unable to load private key from pem-formatted key text.");
}
} catch (NoSuchAlgorithmException e) {
throw new KeyLoadingException("Cannot get EC Key Factory.", e);
} catch (InvalidKeySpecException e) {
throw new KeyLoadingException("Key spec is invalid.", e);
}
}
PublicKey loadPublicKey(String pemKeyText);
}