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());
}
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);
}
}
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);
}
}
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()));
}
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);
}
}
Aggregations