Search in sources :

Example 86 with Base64.encodeBase64String

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

the class LdapCertificateStoreTest method addCertificatesToLdap.

protected void addCertificatesToLdap(String[] filename) throws Exception {
    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);
    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 = LDAPResearchTest.class.getClassLoader().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);
    }
    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) LDAPResearchTest(org.nhindirect.ldap.LDAPResearchTest) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 87 with Base64.encodeBase64String

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

the class LdapCertUtilImpl method ldapSearch.

public Collection<X509Certificate> ldapSearch(String subjectName) {
    DirContext ctx = null;
    try {
        ctx = getInitialDirContext(ldapEnvironment.getEnv());
        final SearchControls ctls = getDefaultSearchControls();
        NamingEnumeration<SearchResult> searchResult = ctx.search(ldapEnvironment.getLdapSearchBase(), ldapEnvironment.getLdapSearchAttribute() + "=" + subjectName, ctls);
        ArrayList<X509Certificate> certificates = new ArrayList<X509Certificate>();
        while (searchResult != null && searchResult.hasMoreElements()) {
            final SearchResult certEntry = searchResult.nextElement();
            if (certEntry != null) {
                final Attributes certAttributes = certEntry.getAttributes();
                if (certAttributes != null) {
                    // get only the returning cert attribute (for now, ignore all other attributes)
                    final Attribute certAttribute = certAttributes.get(ldapEnvironment.getReturningCertAttribute());
                    if (certAttribute != null) {
                        NamingEnumeration<? extends Object> allValues = certAttribute.getAll();
                        // LDAP may contain a collection of certificates.
                        while (allValues.hasMoreElements()) {
                            String ksBytes = (String) allValues.nextElement();
                            Base64 base64 = new Base64();
                            byte[] decode = base64.decode(ksBytes.getBytes());
                            ByteArrayInputStream inputStream = new ByteArrayInputStream(decode);
                            if (certificateFormat.equalsIgnoreCase("pkcs12")) {
                                try {
                                    processPKCS12FileFormatAndAddToCertificates(inputStream, certificates);
                                } catch (Exception e) {
                                    closeDirContext(ctx);
                                    throw new NHINDException("", e);
                                }
                            } else {
                                if (certificateFormat.equalsIgnoreCase("X.509") || certificateFormat.equalsIgnoreCase("X509")) {
                                    CertificateFactory cf = CertificateFactory.getInstance("X.509");
                                    X509Certificate addCert = (X509Certificate) cf.generateCertificate(inputStream);
                                    certificates.add(addCert);
                                } else {
                                    closeDirContext(ctx);
                                    throw new NHINDException("Invalid certificate format requested");
                                }
                            }
                        }
                    }
                }
            }
        }
        return certificates;
    } catch (NamingException e) {
        closeDirContext(ctx);
        throw new NHINDException("", e);
    } catch (CertificateException e) {
        closeDirContext(ctx);
        throw new NHINDException("", e);
    }
}
Also used : Base64(org.apache.commons.codec.binary.Base64) Attribute(javax.naming.directory.Attribute) ArrayList(java.util.ArrayList) Attributes(javax.naming.directory.Attributes) SearchResult(javax.naming.directory.SearchResult) CertificateException(java.security.cert.CertificateException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) NHINDException(org.nhindirect.stagent.NHINDException) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) KeyStoreException(java.security.KeyStoreException) NamingException(javax.naming.NamingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NHINDException(org.nhindirect.stagent.NHINDException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ByteArrayInputStream(java.io.ByteArrayInputStream) SearchControls(javax.naming.directory.SearchControls) NamingException(javax.naming.NamingException)

Example 88 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project simba-os by cegeka.

the class SAMLServiceImpl method encodeSAMLRequest.

protected String encodeSAMLRequest(byte[] pSAMLRequest) throws RuntimeException {
    Base64 base64Encoder = new Base64();
    try {
        ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
        Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
        DeflaterOutputStream def = new DeflaterOutputStream(byteArray, deflater);
        def.write(pSAMLRequest);
        def.close();
        byteArray.close();
        String stream = new String(base64Encoder.encode(byteArray.toByteArray()));
        return stream.trim();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Base64(org.apache.commons.codec.binary.Base64) Deflater(java.util.zip.Deflater) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) XMLStreamException(javax.xml.stream.XMLStreamException)

Example 89 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project chuidiang-ejemplos by chuidiang.

the class JustOneClass method start.

@PostConstruct
public void start() {
    LOG.info("Singleton started!!");
    Base64 b64 = new Base64();
    LOG.info("Base 64 = " + b64.encodeAsString("Hola".getBytes()));
}
Also used : Base64(org.apache.commons.codec.binary.Base64) PostConstruct(javax.annotation.PostConstruct)

Example 90 with Base64.encodeBase64String

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

the class CookieSigner method getSignature.

/**
 * Get the signature of the input string based on SHA digest algorithm.
 * @param str Input token
 * @return Signed String
 */
private String getSignature(String str) {
    try {
        MessageDigest md = MessageDigest.getInstance(SHA_STRING);
        md.update(str.getBytes());
        md.update(secretBytes);
        byte[] digest = md.digest();
        return new Base64(0).encodeToString(digest);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException("Invalid SHA digest String: " + SHA_STRING + " " + ex.getMessage(), ex);
    }
}
Also used : Base64(org.apache.commons.codec.binary.Base64) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

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