Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.cedarsoft.utils.crypt;
  
  import  org.jetbrains.annotations.NonNls;
  import  org.jetbrains.annotations.NotNull;
  import  org.jetbrains.annotations.Nullable;
  
 import java.net.URL;
X509 Support
 
 public class X509Support {
   @NonNls
   @NotNull
   private static final String RSA = "RSA";
   @NonNls
   @NotNull
   private static final String SHA_256_WITH_RSA = "SHA256withRSA";
   @NonNls
   @NotNull
   private static final String X_509_CERTIFICATE_TYPE = "X.509";
 
   @NotNull
   private final X509Certificate certificate;
   @Nullable
   private final RSAPrivateKey privateKey;

  
Creates a new X509 support without any signing capabilities

Parameters:
certificate the certificate
Throws:
IOException
SecurityException
 
   public X509Support( @NotNull URL certificate ) throws IOExceptionSecurityException {
     thiscertificatenull );
   }

  
Creates a new X509 support

Parameters:
certificate the certificate
privateKey the private key (if available)
Throws:
IOException
SecurityException
 
   @Inject
   public X509Support( @CertificateUrl @NotNull URL certificate, @PrivateKeyUrl @Nullable URL privateKey ) throws IOExceptionSecurityException {
     thisreadCertificatecertificate ), readPrivateKeyprivateKey ) );
   }

  
Creates a new x509 support

Parameters:
certificate the certificate
 
   public X509Support( @NotNull X509Certificate certificate ) {
     thiscertificatenull );
   }

  
Creates a new x509 support

Parameters:
certificate the certificate
privateKey the (optional) private key
 
   public X509Support( @NotNull X509Certificate certificate, @Nullable RSAPrivateKey privateKey ) {
     this. = certificate;
     this. = privateKey;
   }
 
   @NotNull
   public byte[] cipher( @NotNull byte[] plainText ) throws SecurityException {
     try {
       Cipher cipher = Cipher.getInstance );
       cipher.init.getPrivateKey() );
       return cipher.doFinalplainText );
     } catch ( GeneralSecurityException e ) {
       throw new SecurityExceptione );
     }
   }
 
   @NotNull
   public byte[] decipher( @NotNull byte[] bytes ) throws SecurityException {
    try {
      Cipher cipher = Cipher.getInstance );
      cipher.init. );
      return cipher.doFinalbytes );
    } catch ( GeneralSecurityException e ) {
      throw new SecurityExceptione );
    }
  }
  @NotNull
  public com.cedarsoft.utils.crypt.Signature sign( @NotNull byte[] plainText ) throws SecurityException {
    try {
      Signature signature = Signature.getInstance );
      signature.initSigngetPrivateKey() );
      signature.updateplainText );
      return new com.cedarsoft.utils.crypt.Signaturesignature.sign() );
    } catch ( GeneralSecurityException e ) {
      throw new SecurityExceptione );
    }
  }
  public boolean verifySignature( @NotNull byte[] plainText, @NotNull com.cedarsoft.utils.crypt.Signature signature ) throws SecurityException {
    try {
      Signature sign = Signature.getInstance );
      sign.initVerify );
      sign.updateplainText );
      return sign.verifysignature.getBytes() );
    } catch ( GeneralSecurityException e ) {
      throw new SecurityExceptione );
    }
  }

  
Returns the certificate

Returns:
the certificate
  @NotNull
    return ;
  }

  
Returns the private key (if there is one)

Returns:
the private key
  @NotNull
    if (  == null ) {
      throw new IllegalStateException"Private key not avaible" );
    }
    return ;
  }

  
Reads a private key form a url

Parameters:
privateKeyUrl the url containing the private key
Returns:
the read private key
Throws:
IOException
SecurityException
  @Nullable
  public static RSAPrivateKey readPrivateKey( @Nullable URL privateKeyUrl ) throws IOExceptionSecurityException {
    //If a null url is given - just return null
    if ( privateKeyUrl == null ) {
      return null;
    }
    //We have an url --> return it
    DataInputStream in = new DataInputStreamprivateKeyUrl.openStream() );
    try {
      byte[] keyBytes = IOUtils.toByteArrayin );
      KeyFactory keyFactory = KeyFactory.getInstance );
      PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpeckeyBytes );
      return ( RSAPrivateKey ) keyFactory.generatePrivateprivSpec );
    } catch ( GeneralSecurityException e ) {
      throw new SecurityExceptione );
    } finally {
      in.close();
    }
  }

  
Reads the x509 certificate from the given url

Parameters:
certificateUrl the certificate url
Returns:
the certificate
Throws:
IOException
SecurityException
  @NotNull
  public static X509Certificate readCertificate( @NotNull URL certificateUrl ) throws IOExceptionSecurityException {
    //Read the cert
    DataInputStream in = new DataInputStreamcertificateUrl.openStream() );
    try {
      CertificateFactory cf = CertificateFactory.getInstance );
      X509Certificate certificate = ( X509Certificate ) cf.generateCertificatein );
      certificate.checkValidity();
      return certificate;
    } catch ( GeneralSecurityException e ) {
      throw new SecurityExceptione );
    } finally {
      in.close();
    }
  }
New to GrepCode? Check out our FAQ X