Search in sources :

Example 1 with SecureDocument

use of com.bluenimble.platform.crypto.SecureDocument in project serverless by bluenimble.

the class SignVerifyDocument method main.

public static void main(String[] args) throws StoreLoaderException, UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, CertificateException, SignerException, IOException {
    String password = "beesphere";
    String alias = "beesphere";
    String p12 = "beesphere.p12";
    final String cer = "beesphere.cer";
    CertificatesManager cm = new DefaultCertificatesManager();
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put(CertificatesManager.KEY_PASSWORD, password);
    KeyStore ks = cm.load(new FileInputStream(p12), properties);
    PrivateKey key = (PrivateKey) ks.getKey(alias, password.toCharArray());
    Signer signer = new DefaultSigner();
    SecureDocument doc = new StringSecureDocument("a document to sign");
    signer.sign(doc, key, new X509Certificate[] { ReadX509.read(new FileInputStream(cer)) });
    System.out.println(new String(doc.getBytes()));
    signer.verify(doc, new CertificateAcceptor() {

        private static final long serialVersionUID = 8524753501741582177L;

        @Override
        public boolean accept(X509Certificate cert) throws SignerException {
            try {
                return cert.equals(ReadX509.read(new FileInputStream(cer)));
            } catch (Throwable th) {
                throw new SignerException(th, th.getMessage());
            }
        }
    });
    System.out.println(new String(doc.getBytes()));
}
Also used : StringSecureDocument(com.bluenimble.platform.crypto.signer.impl.StringSecureDocument) DefaultSigner(com.bluenimble.platform.crypto.signer.impl.DefaultSigner) PrivateKey(java.security.PrivateKey) DefaultCertificatesManager(com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager) HashMap(java.util.HashMap) CertificateAcceptor(com.bluenimble.platform.crypto.signer.CertificateAcceptor) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) X509Certificate(java.security.cert.X509Certificate) SecureDocument(com.bluenimble.platform.crypto.SecureDocument) StringSecureDocument(com.bluenimble.platform.crypto.signer.impl.StringSecureDocument) DefaultSigner(com.bluenimble.platform.crypto.signer.impl.DefaultSigner) Signer(com.bluenimble.platform.crypto.signer.Signer) CertificatesManager(com.bluenimble.platform.crypto.generator.CertificatesManager) DefaultCertificatesManager(com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager) SignerException(com.bluenimble.platform.crypto.signer.SignerException)

Aggregations

SecureDocument (com.bluenimble.platform.crypto.SecureDocument)1 CertificatesManager (com.bluenimble.platform.crypto.generator.CertificatesManager)1 DefaultCertificatesManager (com.bluenimble.platform.crypto.generator.impl.DefaultCertificatesManager)1 CertificateAcceptor (com.bluenimble.platform.crypto.signer.CertificateAcceptor)1 Signer (com.bluenimble.platform.crypto.signer.Signer)1 SignerException (com.bluenimble.platform.crypto.signer.SignerException)1 DefaultSigner (com.bluenimble.platform.crypto.signer.impl.DefaultSigner)1 StringSecureDocument (com.bluenimble.platform.crypto.signer.impl.StringSecureDocument)1 FileInputStream (java.io.FileInputStream)1 KeyStore (java.security.KeyStore)1 PrivateKey (java.security.PrivateKey)1 X509Certificate (java.security.cert.X509Certificate)1 HashMap (java.util.HashMap)1