org.cryptacular.bean.PemBasedPublicKeyFactoryBean Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cryptacular Show documentation
Show all versions of cryptacular Show documentation
The spectacular complement to the Bouncy Castle crypto API for Java.
/* See LICENSE for licensing and NOTICE for copyright. */
package org.cryptacular.bean;
import java.security.PublicKey;
import org.cryptacular.EncodingException;
import org.cryptacular.util.ByteUtil;
import org.cryptacular.util.KeyPairUtil;
import org.cryptacular.util.PemUtil;
/**
* Factory for creating a public key from a PEM-encoded string:
*
* -----BEGIN PUBLIC KEY-----
MIIBtzCCASsGByqGSM44BAEwggEeAoGBAOulifG+AGGBVGWEjunG4661rydB7eFy
RfHzbwVAVaPU0H3zFcOY35z1l6Pk4ZANVHq7hCbViJBR7XyrkYKaUcaB0nSPLgg3
vWWOmvGqhuR6tWRGbz4fyHl1urCRk9mrJum4mAJd3OlLugCyuIqozsYUtvJ5mlGe
vir1zmxinKd7AhUA7fBEySYP53g7FLOlcEyuhIjvQAECgYBJ9baoGzn0zKpeteC4
jfbGVuKrFksr2eeY0AFJOeTtyFkCnVqrNnF674eN1RAOwA2tzzhWZ96G0AGux8ah
mGsNRbj/qaUTDNRWr7BPBIvDd+8LpMin4Cb5j4c/A7uOY+5WxhUm3TNifueBRohw
h1NnexYQqpclcuTRA/ougLX48gOBhQACgYEA6Tw2khtb1g0vcHu6JRgggWPZVTuj
/HOH3FyjufsfHogWKrlKebZ6hnQ73qAcEgLLYKctPdCX6wnpXN+BsQGYdTkc0FsU
NZD4VW5L5kaWRiLVfE8x55wXdMZtXKWqg1vL6aXYZw7RFe9U9Ck+/AG90knThDC+
xrX2FTDm6uC25rk=
-----END PUBLIC KEY-----
*
* @author Middleware Services
* @see KeyPairUtil#decodePublicKey(byte[])
*/
public class PemBasedPublicKeyFactoryBean implements FactoryBean
{
/** PEM-encoded public key data. */
private String encodedKey;
/** Creates a new instance. */
public PemBasedPublicKeyFactoryBean() {}
/**
* Creates a new instance by specifying all properties.
*
* @param pemEncodedKey PEM-encoded public key data.
*/
public PemBasedPublicKeyFactoryBean(final String pemEncodedKey)
{
setEncodedKey(pemEncodedKey);
}
/** @return PEM-encoded public key data. */
public String getEncodedKey()
{
return encodedKey;
}
/**
* Sets the PEM-encoded public key data.
*
* @param pemEncodedKey PEM-encoded public key data.
*/
public void setEncodedKey(final String pemEncodedKey)
{
if (!PemUtil.isPem(ByteUtil.toBytes(pemEncodedKey))) {
throw new IllegalArgumentException("Data is not PEM encoded.");
}
this.encodedKey = pemEncodedKey;
}
@Override
public PublicKey newInstance() throws EncodingException
{
return KeyPairUtil.decodePublicKey(PemUtil.decode(encodedKey));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy