List of usage examples for java.util Base64 getEncoder
public static Encoder getEncoder()
From source file:org.lightjason.agentspeak.action.builtin.crypto.CEncrypt.java
/** * encrypts a datatset//w ww . j a v a 2 s .c o m * * @param p_algorithm algorithm * @param p_key key * @param p_dataset dataset * @param p_return return argument * @return successful execution */ private static boolean encrypt(@Nonnull final EAlgorithm p_algorithm, @Nonnull final Key p_key, @Nonnull final Serializable p_dataset, @Nonnull final List<ITerm> p_return) { try { p_return.add(CRawTerm.from(Base64.getEncoder().encodeToString( p_algorithm.getEncryptCipher(p_key).doFinal(SerializationUtils.serialize(p_dataset))))); return true; } catch (final IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException l_exception) { return false; } }
From source file:org.codice.ddf.branding.impl.DdfBrandingPluginTest.java
@Test public void testProductImage() throws IOException { ddfBrandingPlugin.init();/*from w w w. jav a 2s.c o m*/ assertThat(ddfBrandingPlugin.getBase64ProductImage(), is(equalTo(Base64.getEncoder().encodeToString( IOUtils.toByteArray(DdfBrandingPluginTest.class.getResourceAsStream(productImage)))))); }
From source file:org.codice.ddf.branding.impl.TestDdfBrandingPlugin.java
@Test public void testProductImage() throws IOException { ddfBrandingPlugin.init();/* w ww . j a va 2s.c o m*/ assertThat(ddfBrandingPlugin.getBase64ProductImage(), is(equalTo(Base64.getEncoder().encodeToString( IOUtils.toByteArray(TestDdfBrandingPlugin.class.getResourceAsStream(productImage)))))); }
From source file:no.kantega.publishing.modules.mailsender.MailSender.java
/** * Sends a mail message. The message body is created by using a Velocity template. * * @param from Sender's email address. * @param to Recipient's email address. * @param subject Subject text for the email. * @param contentFile Velocity template file. * @param parameters Parameters used by Velocity to merge values into the template. * @throws SystemException if an unexpected error occurs. * @throws ConfigurationException if a configuration error occurs. *//* ww w . j av a 2 s .co m*/ public static void send(String from, String to, String subject, String contentFile, Map<String, Object> parameters) throws SystemException, ConfigurationException { String content = createStringFromVelocityTemplate(contentFile, parameters); if (log.isDebugEnabled()) { log.debug("Email parameters: " + parameters); log.debug("Email content: " + content); log.debug("Email content, encoded with default charset and then base64 encoded: " + Base64.getEncoder().encodeToString(content.getBytes())); try { log.debug("Email content, encoded with iso-8859-1 and then base64 encoded: " + Base64.getEncoder().encodeToString(content.getBytes("iso-8859-1"))); } catch (UnsupportedEncodingException e) { log.error("Unable to encode email content as iso-8859-1"); } } send(from, to, subject, content); }
From source file:com.formkiq.core.service.crypto.SecureTokenServiceImpl.java
@Override public String encryptToken(final PrivateKey privateKey, final String token) throws GeneralSecurityException { Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] bytes = Strings.getBytes(token); return Base64.getEncoder().encodeToString(cipher.doFinal(bytes)); }
From source file:mp3downloader.ZingSearch.java
private static String getDetailResult(String id, String searchType) throws UnsupportedEncodingException, MalformedURLException, IOException { String data = "{\"id\": \"" + id + "\", \"t\": \"" + searchType + "\"}"; String jsonData = URLEncoder.encode(Base64.getEncoder().encodeToString(data.getBytes("UTF-8")), "UTF-8"); String signature = hash_hmac(jsonData, privateKey); String urlString = detailUrl + "publicKey=" + publicKey + "&signature=" + signature + "&jsondata=" + jsonData;// w ww . ja v a 2 s .c o m URL url = new URL(urlString); URLConnection urlConn = url.openConnection(); urlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"); InputStream is = urlConn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String line; StringBuilder sb = new StringBuilder(); while ((line = reader.readLine()) != null) { sb.append(line); sb.append("\n"); } is.close(); String content = sb.toString(); return content; }
From source file:org.lightjason.agentspeak.action.buildin.crypto.CEncrypt.java
@Override public final IFuzzyValue<Boolean> execute(final IContext p_context, final boolean p_parallel, final List<ITerm> p_argument, final List<ITerm> p_return, final List<ITerm> p_annotation) { final Key l_key = p_argument.get(0).raw(); final EAlgorithm l_algorithm = EAlgorithm.from(l_key.getAlgorithm()); return CFuzzyValue.from(p_argument.subList(1, p_argument.size()).stream() .map(i -> SerializationUtils.serialize(i.raw())).allMatch(i -> { try { p_return.add(CRawTerm.from(Base64.getEncoder() .encodeToString(l_algorithm.getEncryptCipher(l_key).doFinal(i)))); return true; } catch (final NoSuchPaddingException | NoSuchAlgorithmException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException l_exception) { return false; }/* w w w .ja v a 2 s . co m*/ })); }
From source file:com.antonjohansson.elasticsearchshell.client.Client.java
private WebClient client() { String baseURL = connection.getURL(); JacksonJsonProvider provider = new JacksonJsonProvider(MAPPER); WebClient client = WebClient.create(baseURL, asList(provider)).accept(APPLICATION_JSON_TYPE) .type(APPLICATION_JSON_TYPE); if (!isBlank(connection.getUsername())) { String decryptedPassword = passwordEncrypter.decrypt(connection.getUsername(), connection.getPassword()); String authorizationString = connection.getUsername() + ":" + decryptedPassword; String authorization = "Basic " + Base64.getEncoder().encodeToString(authorizationString.getBytes()); client.header("Authorization", authorization); }//from w ww . j a va 2 s.c o m return client; }
From source file:org.sonarlint.languageserver.RuleDescriptionHttpServer.java
private static String getAsBase64(String image) throws IOException { InputStream resourceAsStream = ServerMain.class.getResourceAsStream(image); if (resourceAsStream == null) { throw new IllegalStateException("Unable to load image " + image); }/* w w w . j a va 2 s. com*/ return Base64.getEncoder().encodeToString(IOUtils.toByteArray(resourceAsStream)); }
From source file:waffle.spring.DelegatingNegotiateSecurityFilterTest.java
/** * Test the delegating filter ,in case no custom authentication was passed, the filter would store the auth in the * security context./*from w w w . j a v a2s . co m*/ * * @throws IOException * Signals that an I/O exception has occurred. * @throws ServletException * the servlet exception */ @Test public void testNegotiate() throws IOException, ServletException { final String securityPackage = "Negotiate"; final SimpleFilterChain filterChain = new SimpleFilterChain(); final SimpleHttpRequest request = new SimpleHttpRequest(); final String clientToken = Base64.getEncoder() .encodeToString(WindowsAccountImpl.getCurrentUsername().getBytes(StandardCharsets.UTF_8)); request.addHeader("Authorization", securityPackage + " " + clientToken); final SimpleHttpResponse response = new SimpleHttpResponse(); this.filter.doFilter(request, response, filterChain); final Authentication auth = SecurityContextHolder.getContext().getAuthentication(); Assertions.assertNotNull(auth); final Collection<? extends GrantedAuthority> authorities = auth.getAuthorities(); Assertions.assertNotNull(authorities); Assertions.assertEquals(3, authorities.size()); final List<String> list = new ArrayList<>(); for (final GrantedAuthority grantedAuthority : authorities) { list.add(grantedAuthority.getAuthority()); } Collections.sort(list); Assertions.assertEquals("ROLE_EVERYONE", list.get(0)); Assertions.assertEquals("ROLE_USER", list.get(1)); Assertions.assertEquals("ROLE_USERS", list.get(2)); Assertions.assertEquals(0, response.getHeaderNamesSize()); }