Search in sources :

Example 76 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project nhin-d by DirectProject.

the class RESTSmtpAgentConfigFunctional_Test method addCertificatesToLdap.

protected void addCertificatesToLdap(String[] filename, String email) throws Exception {
    Entry entry = new Entry();
    entry.addAttribute("objectClass", "organizationalUnit");
    entry.addAttribute("objectClass", "top");
    entry.addAttribute("objectClass", "userPrivKey");
    entry.addAttribute("email", email);
    for (int i = 0; i < filename.length; i++) {
        byte[] buffer = loadCertificateData(filename[i]);
        Base64 base64 = new Base64();
        String certificateValue = new String(base64.encode(buffer));
        entry.addAttribute("privKeyStore", certificateValue);
    }
    String ou;
    int index = email.indexOf("@");
    if (index > -1)
        ou = email.substring(0, email.indexOf("@"));
    else
        ou = email;
    entry.addAttribute("ou", ou);
    rootDSE.createSubcontext("ou=" + ou + ", ou=privKeys, ou=cerner, ou=com, cn=lookupTest", entry.getAttributes());
}
Also used : Entry(org.apache.directory.shared.ldap.ldif.Entry) Base64(org.apache.commons.codec.binary.Base64)

Example 77 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project nhin-d by DirectProject.

the class WSSmtpAgentConfigFunctional_Test method addCertificatesToLdap.

protected void addCertificatesToLdap(String[] filename, String email) throws Exception {
    Entry entry = new Entry();
    entry.addAttribute("objectClass", "organizationalUnit");
    entry.addAttribute("objectClass", "top");
    entry.addAttribute("objectClass", "userPrivKey");
    entry.addAttribute("email", email);
    for (int i = 0; i < filename.length; i++) {
        byte[] buffer = loadCertificateData(filename[i]);
        Base64 base64 = new Base64();
        String certificateValue = new String(base64.encode(buffer));
        entry.addAttribute("privKeyStore", certificateValue);
    }
    String ou;
    int index = email.indexOf("@");
    if (index > -1)
        ou = email.substring(0, email.indexOf("@"));
    else
        ou = email;
    entry.addAttribute("ou", ou);
    rootDSE.createSubcontext("ou=" + ou + ", ou=privKeys, ou=cerner, ou=com, cn=lookupTest", entry.getAttributes());
}
Also used : Entry(org.apache.directory.shared.ldap.ldif.Entry) Base64(org.apache.commons.codec.binary.Base64)

Example 78 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project nhin-d by DirectProject.

the class XMLSmtpAgentConfigFunctional_Test method addCertificatesToLdap.

protected void addCertificatesToLdap(String[] filename, String email) throws NamingException {
    Entry entry = new Entry();
    entry.addAttribute("objectClass", "organizationalUnit");
    entry.addAttribute("objectClass", "top");
    entry.addAttribute("objectClass", "userPrivKey");
    entry.addAttribute("email", email);
    File fl = new File("testfile");
    int idx = fl.getAbsolutePath().lastIndexOf("testfile");
    String path = fl.getAbsolutePath().substring(0, idx);
    for (int i = 0; i < filename.length; i++) {
        byte[] buffer = new byte[(int) new File(path + "src/test/resources/" + filename[i]).length() + 100];
        try {
            InputStream stream = TestUtils.class.getResourceAsStream(filename[i]);
            stream.read(buffer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Base64 base64 = new Base64();
        String certificateValue = new String(base64.encode(buffer));
        entry.addAttribute("privKeyStore", certificateValue);
    }
    String ou = email.substring(0, email.indexOf("@"));
    entry.addAttribute("ou", ou);
    rootDSE.createSubcontext("ou=" + ou + ", ou=privKeys, ou=cerner, ou=com, cn=lookupTest", entry.getAttributes());
}
Also used : Entry(org.apache.directory.shared.ldap.ldif.Entry) Base64(org.apache.commons.codec.binary.Base64) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 79 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project nhin-d by DirectProject.

the class LDAPResearchTest method createLdapEntries.

protected void createLdapEntries() throws NamingException {
    /*Attributes attrs = new BasicAttributes( "objectClass", "top", true);
		attrs.put("objectClass", "organizationalUnit");
		attrs.put("objectClass", "userPrivKey");
		attrs.put("email", "gm2552@cerner.com");
		attrs.put("privKeyStore", "1234567");
		rootDSE.createSubcontext("ou=gm2552, ou=privKeys, ou=cerner, ou=com, cn=lookupTest", attrs);
		*/
    Entry entry = new Entry();
    entry.addAttribute("objectClass", "organizationalUnit");
    entry.addAttribute("objectClass", "top");
    entry.addAttribute("objectClass", "userPrivKey");
    entry.addAttribute("email", "gm2552@cerner.com");
    File fl = new File("testfile");
    int idx = fl.getAbsolutePath().lastIndexOf("testfile");
    String path = fl.getAbsolutePath().substring(0, idx);
    byte[] buffer = new byte[(int) new File(path + "src/test/resources/certs/bob.der").length() + 100];
    try {
        InputStream stream = LDAPResearchTest.class.getClassLoader().getResourceAsStream("certs/bob.der");
        stream.read(buffer);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Base64 base64 = new Base64();
    String certificateValue = new String(base64.encode(buffer));
    entry.addAttribute("privKeyStore", certificateValue);
    entry.addAttribute("ou", "gm2552");
    rootDSE.createSubcontext("ou=gm2552, ou=privKeys, ou=cerner, ou=com, cn=lookupTest", entry.getAttributes());
}
Also used : Entry(org.apache.directory.shared.ldap.ldif.Entry) Base64(org.apache.commons.codec.binary.Base64) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 80 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project OpenAttestation by OpenAttestation.

the class TAHelper method getPemPublicKey.

public static PublicKey getPemPublicKey(String filename) {
    File f = new File(filename);
    FileInputStream filestr = null;
    PublicKey pubkey = null;
    try {
        filestr = new FileInputStream(f);
        DataInputStream datastr = new DataInputStream(filestr);
        byte[] keyBytes = new byte[(int) f.length()];
        datastr.readFully(keyBytes);
        datastr.close();
        String temp = new String(keyBytes);
        String publicKeyPEM = temp.replace("-----BEGIN PUBLIC KEY-----\n", "");
        publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", "");
        Base64 b64 = new Base64();
        byte[] decoded = b64.decode(publicKeyPEM);
        X509EncodedKeySpec spec = new X509EncodedKeySpec(decoded);
        KeyFactory kf = KeyFactory.getInstance("RSA");
        kf.generatePublic(spec);
        pubkey = kf.generatePublic(spec);
    } catch (FileNotFoundException e) {
        log.error("Unable to find rsapubkey file, " + e.toString());
    } catch (IOException e) {
        log.error("Unable to read nonce file");
    } catch (NoSuchAlgorithmException e) {
        log.error("no such algorithm " + e.toString());
    } catch (InvalidKeySpecException e) {
        log.error("invalid key is found " + e.toString());
    }
    return pubkey;
}
Also used : Base64(org.apache.commons.codec.binary.Base64) PublicKey(java.security.PublicKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory)

Aggregations

Base64 (org.apache.commons.codec.binary.Base64)135 IOException (java.io.IOException)30 Test (org.junit.Test)29 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)12 InputStream (java.io.InputStream)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 HashMap (java.util.HashMap)10 SecretKeySpec (javax.crypto.spec.SecretKeySpec)9 MessageDigest (java.security.MessageDigest)8 File (java.io.File)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 URL (java.net.URL)7 Mac (javax.crypto.Mac)7 ServletException (javax.servlet.ServletException)7 X509Certificate (java.security.cert.X509Certificate)6 FileNotFoundException (java.io.FileNotFoundException)5 Signature (java.security.Signature)5