List of usage examples for org.apache.commons.codec.binary Base64 encodeBase64String
public static String encodeBase64String(final byte[] binaryData)
From source file:com.github.rollercage.CageCommentAuthenticator.java
@Override public String getHtml(javax.servlet.http.HttpServletRequest req) { String token = cage.getTokenGenerator().next(); HttpSession session = req.getSession(true); session.setAttribute(TOKEN, token);/* w w w . ja v a2s. co m*/ StringBuilder result = new StringBuilder(); result.append("<p>Please solve the puzzle: </p>"); result.append("<p>"); result.append("<img alt=\"CAPTCHA\" src=\"data:image/jpeg;base64,"); result.append(Base64.encodeBase64String(cage.draw(token))); result.append("\"/>"); result.append(" <input name=\"" + ANSWER + "\"/>"); result.append("</p>"); return result.toString(); }
From source file:eu.scidipes.toolkits.palibrary.utils.zip.ZipUtils.java
/** * Zips one or more {@link ByteArrayZipEntry} objects together and returns the archive as a base64-encoded * <code>String</code>/*from w w w . ja v a2 s. com*/ * * @param entries * @return a base64-encoded <code>String</code> which is the zip archive of the passed entries * * @throws IOException * in the event of an exception writing to the zip output stream * @throws IllegalArgumentException * if passed entries is null or empty */ public static String byteArrayZipEntriesToBase64(final Set<? extends ByteArrayZipEntry> entries) throws IOException { if (entries == null || entries.isEmpty()) { throw new IllegalArgumentException("entries cannot be null or empty"); } final ByteArrayOutputStream bos = new ByteArrayOutputStream(); try (final ZipOutputStream zos = new ZipOutputStream(bos)) { for (final ByteArrayZipEntry entry : entries) { zos.putNextEntry(entry.getZipEntry()); IOUtils.write(entry.getBytes(), zos); zos.closeEntry(); } } return Base64.encodeBase64String(bos.toByteArray()); }
From source file:be.fedict.eid.idp.common.saml2.AuthenticationResponse.java
public AuthenticationResponse(DateTime authenticationTime, String identifier, SamlAuthenticationPolicy authenticationPolicy, Map<String, Object> attributeMap, Assertion assertion) { this.authenticationTime = authenticationTime; this.identifier = identifier; this.authenticationPolicy = authenticationPolicy; this.attributeMap = attributeMap; // marshall and encode assertion so it is serializble this.encodedAssertion = Base64 .encodeBase64String(Saml2Util.domToString(Saml2Util.marshall(assertion), false).getBytes()); }
From source file:com.k42b3.aletheia.filter.request.BasicAuth.java
protected String computeAuth(String key) { return Base64.encodeBase64String(key.getBytes()); }
From source file:net.officefloor.plugin.jndi.ldap.CredentialStoreTest.java
/** * Ensure able to obtain credentials.// w ww.ja v a 2 s. c o m */ public void testObtainCredentials() throws Exception { final Charset ASCII = Charset.forName("ASCII"); // Calculate the expected credential String expectedRaw = "daniel:officefloor:password"; MessageDigest digest = MessageDigest.getInstance("MD5"); digest.update(expectedRaw.getBytes(ASCII)); byte[] expectedBytes = digest.digest(); String expectedCredentials = Base64.encodeBase64String(expectedBytes).trim(); // Obtain the context DirContext context = this.ldap.getDirContext(); // Obtain the People context DirContext people = (DirContext) context.lookup("ou=People,dc=officefloor,dc=net"); assertNotNull("Should have People context", people); // Search for person NamingEnumeration<SearchResult> results = people.search("", "(&(objectClass=inetOrgPerson)(uid=daniel))", null); assertTrue("Expecting to find daniel entry", results.hasMore()); SearchResult result = results.next(); assertFalse("Should only have the daniel entry", results.hasMore()); // Obtain the digest MD5 credentials for Daniel String digestMd5Credential = null; Attributes attributes = result.getAttributes(); Attribute passwordAttribute = attributes.get("userPassword"); for (NamingEnumeration<?> enumeration = passwordAttribute.getAll(); enumeration.hasMore();) { byte[] credentials = (byte[]) enumeration.next(); String text = new String(credentials, ASCII); // Determine if MD5 credential if (text.toUpperCase().startsWith("{MD5}")) { // Found MD5 credential digestMd5Credential = text.substring("{MD5}".length()); } } assertNotNull("Must have digest MD5 credential", digestMd5Credential); // Ensure correct credentials assertEquals("Incorrect DIGEST MD5 credentials", expectedCredentials, digestMd5Credential); }
From source file:net.shopxx.controller.shop.QRCodeController.java
/** * ?base64? ?? base64//from w ww . ja va 2 s .c o m */ @RequestMapping(value = "/encodeBase64", method = RequestMethod.GET) public void image(String text, String ico, HttpServletRequest request, HttpServletResponse response) throws Exception { text = new String(new BASE64Decoder().decodeBuffer(text), "utf-8"); String[] splits = text.split("\\?"); text = splits[0] + "?parameter=" + Base64.encodeBase64String(splits[1].getBytes()); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Cache-Control", "no-store"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); ServletOutputStream servletOutputStream = null; try { servletOutputStream = response.getOutputStream(); BufferedImage bufferedImage = QRCodeUtil.createImageInUrl(text, ico, true, 0, 0); ImageIO.write(bufferedImage, "jpg", servletOutputStream); servletOutputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(servletOutputStream); } }
From source file:com.github.nukesparrow.htmlunit.DataUrlXmlSerializer.java
public String toDataUrl(final HtmlPage page) throws IOException { if (page.getDocumentElement() == null) { return null; }/*from w ww.j a v a 2 s. c o m*/ return "data:text/html;charset=" + page.getPageEncoding() + ";base64," + Base64.encodeBase64String(asXml(page.getDocumentElement()).getBytes(page.getPageEncoding())); }
From source file:ca.uhn.fhir.rest.client.interceptor.BasicAuthInterceptor.java
@Override public void interceptRequest(IHttpRequest theRequest) { String authorizationUnescaped = StringUtils.defaultString(myUsername) + ":" + StringUtils.defaultString(myPassword); String encoded;/*from www. j a va 2 s . c om*/ try { encoded = Base64.encodeBase64String(authorizationUnescaped.getBytes("ISO-8859-1")); } catch (UnsupportedEncodingException e) { throw new InternalErrorException("Could not find US-ASCII encoding. This shouldn't happen!"); } theRequest.addHeader(Constants.HEADER_AUTHORIZATION, ("Basic " + encoded)); }
From source file:com.javaps.springboot.LicenseController.java
@RequestMapping(value = "/public/license", produces = "text/plain", method = RequestMethod.GET) public String licenseIssue(@RequestParam(value = "ip") String clientIp) throws Exception { SecretKeySpec signingKey = new SecretKeySpec(licenseSecretKey.getBytes(), "HmacSHA1"); Mac mac = Mac.getInstance("HmacSHA1"); mac.init(signingKey);/*from w w w . j a v a2 s . c o m*/ byte[] rawHmac = mac.doFinal(clientIp.getBytes()); return Base64.encodeBase64String(rawHmac); }
From source file:com.servoy.j2db.util.serialize.StringByteArraySerializer.java
@Override public Object marshall(SerializerState state, Object p, Object o) throws MarshallException { if (o instanceof byte[]) { try {/*from www. ja va2 s .com*/ JSONObject val = new JSONObject(); if (ser.getMarshallClassHints()) { val.put("javaClass", byte[].class.getName()); } val.put("base64String", Base64.encodeBase64String((byte[]) o)); return val; } catch (JSONException e) { throw new MarshallException("JSONException: " + e.getMessage(), e); } } return super.marshall(state, p, o); }