Class CryptoPrimitives

  • All Implemented Interfaces:
    CryptoSuite


    public class CryptoPrimitives
    extends java.lang.Object
    implements CryptoSuite
    • Constructor Summary

      Constructors 
      Constructor Description
      CryptoPrimitives​()  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addCACertificatesToTrustStore​(java.io.BufferedInputStream bis)
      addCACertificatesToTrustStore adds a CA certs in a stream to the trust store used for signature validation
      void addCACertificateToTrustStore​(java.io.File caCertPem, java.lang.String alias)
      addCACertificateToTrustStore adds a CA cert to the set of certificates used for signature validation
      java.security.cert.Certificate bytesToCertificate​(byte[] certBytes)
      Convert bytes in PEM format to Certificate.
      java.security.PrivateKey bytesToPrivateKey​(byte[] pemKey)
      Return PrivateKey from pem bytes.
      byte[] certificateToDER​(java.lang.String certificatePEM)  
      java.lang.String generateCertificationRequest​(java.lang.String subject, java.security.KeyPair pair)
      generateCertificationRequest
      CryptoSuiteFactory getCryptoSuiteFactory​()
      Get Crypto Suite Factory for this implementation.
      java.util.Properties getProperties​()  
      java.security.KeyStore getTrustStore​()
      getTrustStore returns the KeyStore object where we keep trusted certificates.
      byte[] hash​(byte[] input)
      Hash the specified text byte data.
      void init​()  
      java.security.KeyPair keyGen​()
      Generate a key.
      void loadCACertificates​(java.util.Collection<java.security.cert.Certificate> certificates)
      Set the Certificate Authority certificates to be used when validating a certificate chain of trust
      void loadCACertificatesAsBytes​(java.util.Collection<byte[]> certificatesBytes)
      Set the Certificate Authority certificates to be used when validating a certificate chain of trust.
      byte[] sign​(java.security.PrivateKey key, byte[] data)
      Sign the specified byte string.
      boolean verify​(byte[] pemCertificate, java.lang.String signatureAlgorithm, byte[] signature, byte[] plainText)
      Verify the specified signature
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CryptoPrimitives

        public CryptoPrimitives​()
                         throws java.lang.ClassNotFoundException,
                                java.lang.IllegalAccessException,
                                java.lang.InstantiationException
        Throws:
        java.lang.ClassNotFoundException
        java.lang.IllegalAccessException
        java.lang.InstantiationException
    • Method Detail

      • bytesToPrivateKey

        public java.security.PrivateKey bytesToPrivateKey​(byte[] pemKey)
                                                   throws CryptoException
        Return PrivateKey from pem bytes.
        Parameters:
        pemKey - pem-encoded private key
        Returns:
        Throws:
        CryptoException
      • verify

        public boolean verify​(byte[] pemCertificate,
                              java.lang.String signatureAlgorithm,
                              byte[] signature,
                              byte[] plainText)
                       throws CryptoException
        Description copied from interface: CryptoSuite
        Verify the specified signature
        Specified by:
        verify in interface CryptoSuite
        Parameters:
        pemCertificate - the certificate of the signer as the contents of the PEM file
        signatureAlgorithm - the algorithm used to create the signature.
        signature - the signature to verify
        plainText - the original text that is to be verified
        Returns:
        true if the signature is successfully verified; otherwise false.
        Throws:
        CryptoException
      • getTrustStore

        public java.security.KeyStore getTrustStore​()
                                             throws CryptoException
        getTrustStore returns the KeyStore object where we keep trusted certificates. If no trust store has been set, this method will create one.
        Returns:
        the trust store as a java.security.KeyStore object
        Throws:
        CryptoException
        See Also:
        KeyStore
      • addCACertificateToTrustStore

        public void addCACertificateToTrustStore​(java.io.File caCertPem,
                                                 java.lang.String alias)
                                          throws CryptoException,
                                                 InvalidArgumentException
        addCACertificateToTrustStore adds a CA cert to the set of certificates used for signature validation
        Parameters:
        caCertPem - an X.509 certificate in PEM format
        alias - an alias associated with the certificate. Used as shorthand for the certificate during crypto operations
        Throws:
        CryptoException
        InvalidArgumentException
      • loadCACertificates

        public void loadCACertificates​(java.util.Collection<java.security.cert.Certificate> certificates)
                                throws CryptoException
        Description copied from interface: CryptoSuite
        Set the Certificate Authority certificates to be used when validating a certificate chain of trust
        Specified by:
        loadCACertificates in interface CryptoSuite
        Parameters:
        certificates - A collection of Certificates
        Throws:
        CryptoException
      • loadCACertificatesAsBytes

        public void loadCACertificatesAsBytes​(java.util.Collection<byte[]> certificatesBytes)
                                       throws CryptoException
        Description copied from interface: CryptoSuite
        Set the Certificate Authority certificates to be used when validating a certificate chain of trust.
        Specified by:
        loadCACertificatesAsBytes in interface CryptoSuite
        Parameters:
        certificatesBytes - a collection of certificates in PEM format
        Throws:
        CryptoException
      • sign

        public byte[] sign​(java.security.PrivateKey key,
                           byte[] data)
                    throws CryptoException
        Description copied from interface: CryptoSuite
        Sign the specified byte string.
        Specified by:
        sign in interface CryptoSuite
        Parameters:
        key - the PrivateKey to be used for signing
        data - the byte string to sign
        Returns:
        the signed data.
        Throws:
        java.lang.ClassCastException - if the supplied private key is not of type ECPrivateKey.
        CryptoException
      • generateCertificationRequest

        public java.lang.String generateCertificationRequest​(java.lang.String subject,
                                                             java.security.KeyPair pair)
                                                      throws InvalidArgumentException
        generateCertificationRequest
        Specified by:
        generateCertificationRequest in interface CryptoSuite
        Parameters:
        subject - The subject to be added to the certificate
        pair - Public private key pair
        Returns:
        PKCS10CertificationRequest Certificate Signing Request.
        Throws:
        org.bouncycastle.operator.OperatorCreationException
        InvalidArgumentException
      • hash

        public byte[] hash​(byte[] input)
        Description copied from interface: CryptoSuite
        Hash the specified text byte data.
        Specified by:
        hash in interface CryptoSuite
        Parameters:
        input - the text to hash
        Returns:
        the hashed data.
      • getCryptoSuiteFactory

        public CryptoSuiteFactory getCryptoSuiteFactory​()
        Description copied from interface: CryptoSuite
        Get Crypto Suite Factory for this implementation.
        Specified by:
        getCryptoSuiteFactory in interface CryptoSuite
        Returns:
        MUST return the one and only one instance of a factory that produced this crypto suite.
      • getProperties

        public java.util.Properties getProperties​()
        Specified by:
        getProperties in interface CryptoSuite
        Returns:
        the Properties object containing implementation specific key generation properties
      • certificateToDER

        public byte[] certificateToDER​(java.lang.String certificatePEM)