Search in sources :

Example 1 with Format

use of com.ingrian.security.nae.Format in project CipherTrust_Application_Protection by thalescpl-io.

the class CMSSignSample method main.

public static void main(String[] args) throws Exception {
    if (args.length != 4) {
        System.err.println("Usage: java CMSSignSample user password keyname caName");
        System.exit(-1);
    }
    String username = args[0];
    String password = args[1];
    String keyName = args[2];
    String caName = args[3];
    // data to sign
    byte[] data = "dataToSign".getBytes();
    // add Ingrian provider to the list of JCE providers
    Security.addProvider(new IngrianProvider());
    // get the list of all registered JCE providers
    Provider[] providers = Security.getProviders();
    for (int i = 0; i < providers.length; i++) System.out.println(providers[i].getInfo());
    NAESession session = null;
    try {
        // create NAE Session: pass in Key Manager user name and password
        session = NAESession.getSession(username, password.toCharArray());
        // Create Signature object
        Signature sig = Signature.getInstance("SHA1withRSA", "IngrianProvider");
        SignVerifySpec signVerifySpec = new SignVerifySpec(new Format("cms/detached/smime/enveloped"), new CertList(caName));
        sig.setParameter((AlgorithmParameterSpec) signVerifySpec);
        // Sign data
        // Get private key
        NAEPrivateKey privKey = NAEKey.getPrivateKey(keyName, session);
        // Initialize Signature object for signing
        sig.initSign(privKey);
        sig.update(data);
        byte[] signature = sig.sign();
        // Verify signature
        // Get public key
        NAEPublicKey pubKey = NAEKey.getPublicKey(keyName, session);
        // Initialize Signature object for signature verification
        sig.initVerify(pubKey);
        sig.update(data);
        if (sig.verify(signature))
            System.out.println("Signature verified.");
        else
            System.out.println("Signature verification failed.");
        // close NAE session
        session.closeSession();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (session != null)
            session.closeSession();
    }
}
Also used : NAEPrivateKey(com.ingrian.security.nae.NAEPrivateKey) NAEPublicKey(com.ingrian.security.nae.NAEPublicKey) IngrianProvider(com.ingrian.security.nae.IngrianProvider) IngrianProvider(com.ingrian.security.nae.IngrianProvider) Provider(java.security.Provider) CertList(com.ingrian.security.nae.CertList) SignVerifySpec(com.ingrian.security.nae.SignVerifySpec) Format(com.ingrian.security.nae.Format) Signature(java.security.Signature) NAESession(com.ingrian.security.nae.NAESession)

Aggregations

CertList (com.ingrian.security.nae.CertList)1 Format (com.ingrian.security.nae.Format)1 IngrianProvider (com.ingrian.security.nae.IngrianProvider)1 NAEPrivateKey (com.ingrian.security.nae.NAEPrivateKey)1 NAEPublicKey (com.ingrian.security.nae.NAEPublicKey)1 NAESession (com.ingrian.security.nae.NAESession)1 SignVerifySpec (com.ingrian.security.nae.SignVerifySpec)1 Provider (java.security.Provider)1 Signature (java.security.Signature)1