public class RsaDigitalSignatureSerializer extends java.lang.Object implements ISerializer
String aDataToSerialize = "Hello";
// Public certificate
CertificateFactory aCertificateFactory = CertificateFactory.getInstance("X.509");
FileInputStream aCertificateStream = new FileInputStream("d:/MySigner.cer");
X509Certificate aCertificate = (X509Certificate) aCertificateFactory.generateCertificate(aCertificateStream);
// Private key
File aPrivateKeyFile = new File("d:/MySigner.pk8");
BufferedInputStream aBufferedPrivateKey = new BufferedInputStream(new FileInputStream(aPrivateKeyFile));
byte[] aPrivateKeyBytes = new byte[(int)aPrivateKeyFile.length()];
aBufferedPrivateKey.read(aPrivateKeyBytes);
KeySpec aKeySpec = new PKCS8EncodedKeySpec(aPrivateKeyBytes);
RSAPrivateKey aPrivateKey = (RSAPrivateKey)KeyFactory.getInstance("RSA").generatePrivate(aKeySpec);
// Create serializer
ISerializer aSerializer = new RsaDigitalSignatureSerializer(aCertificate, aPrivateKey);
// Serialize
Object aSerializedData = aSerializer.serialize(aDataToSerialize, String.class);
// Deserialize
String aDeserializedData = aSerializer.deserialize(aSerializedData, String.class);
Constructor and Description |
---|
RsaDigitalSignatureSerializer(java.security.cert.X509Certificate signerCertificate,
java.security.interfaces.RSAPrivateKey signerPrivateKey)
Constructs serializer with default parameters.
|
RsaDigitalSignatureSerializer(java.security.cert.X509Certificate signerPublicCertificate,
java.security.interfaces.RSAPrivateKey signerPrivateKey,
IFunction1<java.lang.Boolean,java.security.cert.X509Certificate> verifySignerCertificate,
ISerializer underlyingSerializer)
Constructs serializer with custom parameters.
|
Modifier and Type | Method and Description |
---|---|
<T> T |
deserialize(java.lang.Object serializedData,
java.lang.Class<T> clazz)
Deserializes data.
|
<T> java.lang.Object |
serialize(T dataToSerialize,
java.lang.Class<T> clazz)
Serializes data.
|
public RsaDigitalSignatureSerializer(java.security.cert.X509Certificate signerCertificate, java.security.interfaces.RSAPrivateKey signerPrivateKey)
signerCertificate
- public certificate of the signer. This certificate will be attached
to serialized data so that the deserializer can verify the signer identity and
can check if signed data are not changed.signerPrivateKey
- private key that will be used to sign data.public RsaDigitalSignatureSerializer(java.security.cert.X509Certificate signerPublicCertificate, java.security.interfaces.RSAPrivateKey signerPrivateKey, IFunction1<java.lang.Boolean,java.security.cert.X509Certificate> verifySignerCertificate, ISerializer underlyingSerializer)
signerPublicCertificate
- public certificate of the signer. This certificate will be attached
to serialized data so that the deserializer can verify the signer identity and
can check if signed data are not changed.signerPrivateKey
- private key that will be used to sign data.verifySignerCertificate
- Method that will check the signer public certificate before deserializing.
If null then default X509Certificate.checkValidity() is used.underlyingSerializer
- underlying serializer used to serialize data. It can
be any serializer from this namespace.public <T> java.lang.Object serialize(T dataToSerialize, java.lang.Class<T> clazz) throws java.lang.Exception
serialize
in interface ISerializer
dataToSerialize
- Data to be serialized.clazz
- represents the serialized type.java.lang.Exception
- If the serialization fails.public <T> T deserialize(java.lang.Object serializedData, java.lang.Class<T> clazz) throws java.lang.Exception
deserialize
in interface ISerializer
serializedData
- Data to be deserialized.java.lang.Exception
- If the deserialization fails.