List of usage examples for java.util Base64 getEncoder
public static Encoder getEncoder()
From source file:com.netflix.spinnaker.halyard.backup.kms.v1.google.GoogleKms.java
byte[] encryptContents(String plaintext) { plaintext = Base64.getEncoder().encodeToString(plaintext.getBytes()); EncryptRequest encryptRequest = new EncryptRequest().encodePlaintext(plaintext.getBytes()); EncryptResponse response;/* w w w . j a v a 2 s .c o m*/ try { response = cloudKms.projects().locations().keyRings().cryptoKeys() .encrypt(cryptoKey.getName(), encryptRequest).execute(); } catch (IOException e) { throw new HalException(Problem.Severity.FATAL, "Failed to encrypt user data: " + e.getMessage(), e); } return response.decodeCiphertext(); }
From source file:org.votingsystem.web.accesscontrol.ejb.RepresentativeBean.java
public ResponseVS<RepresentativeDocument> saveRepresentativeData(MessageSMIME messageSMIME, byte[] imageBytes) throws Exception { UserVS signer = messageSMIME.getUserVS(); AnonymousDelegation anonymousDelegation = representativeDelegationBean.getAnonymousDelegation(signer); if (anonymousDelegation != null) throw new ValidationExceptionVS(messages.get("representativeRequestWithActiveAnonymousDelegation")); Map requestMap = messageSMIME.getSignedContentMap(); String base64ImageHash = (String) requestMap.get("base64ImageHash"); MessageDigest messageDigest = MessageDigest.getInstance(ContextVS.VOTING_DATA_DIGEST); byte[] resultDigest = messageDigest.digest(imageBytes); String base64ResultDigest = Base64.getEncoder().encodeToString(resultDigest); if (!base64ResultDigest.equals(base64ImageHash)) throw new ValidationExceptionVS(messages.get("imageHashErrorMsg")); //String base64EncodedImage = requestMap.base64RepresentativeEncodedImage //BASE64Decoder decoder = new BASE64Decoder(); //byte[] imageFileBytes = decoder.decodeBuffer(base64EncodedImage); String msg = null;/*from w w w .j a v a 2s .c o m*/ signer.setDescription((String) requestMap.get("representativeInfo")); if (UserVS.Type.REPRESENTATIVE != signer.getType()) { representativeDelegationBean.cancelRepresentationDocument(messageSMIME); msg = messages.get("representativeDataCreatedOKMsg", signer.getFirstName(), signer.getLastName()); } else { msg = messages.get("representativeDataUpdatedMsg", signer.getFirstName(), signer.getLastName()); } dao.merge(signer.setType(UserVS.Type.REPRESENTATIVE).setRepresentative(null)); Query query = dao.getEM().createQuery("select i from ImageVS i where i.userVS =:userVS and i.type =:type") .setParameter("userVS", signer).setParameter("type", ImageVS.Type.REPRESENTATIVE); List<ImageVS> images = query.getResultList(); for (ImageVS imageVS : images) { dao.merge(imageVS.setType(ImageVS.Type.REPRESENTATIVE_CANCELED)); } ImageVS newImage = dao.persist(new ImageVS(signer, messageSMIME, ImageVS.Type.REPRESENTATIVE, imageBytes)); query = dao.getEM() .createQuery( "select r from RepresentativeDocument r where r.userVS =:userVS " + "and r.state =:state") .setParameter("userVS", signer).setParameter("state", RepresentativeDocument.State.OK); RepresentativeDocument representativeDocument = dao.getSingleResult(RepresentativeDocument.class, query); if (representativeDocument != null) { representativeDocument.setState(RepresentativeDocument.State.RENEWED) .setCancellationSMIME(messageSMIME); dao.merge(representativeDocument); } RepresentativeDocument repDocument = dao.persist( new RepresentativeDocument(signer, messageSMIME, (String) requestMap.get("representativeInfo"))); log.info("saveRepresentativeData - user id: " + signer.getId()); //return new ResponseVS(statusCode:ResponseVS.SC_OK, message:msg, type:TypeVS.REPRESENTATIVE_DATA) return new ResponseVS<RepresentativeDocument>(ResponseVS.SC_OK, msg, repDocument); }
From source file:io.adeptj.runtime.server.CredentialMatcher.java
private static String toHash(String pwd) { String hashPassword = pwd;/*from w w w .java 2 s.c o m*/ try { hashPassword = CURLY_BRACE_OPEN + SHA256.toLowerCase() + CURLY_BRACE_CLOSE + new String( Base64.getEncoder().encode(MessageDigest.getInstance(SHA256).digest(pwd.getBytes(UTF8))), UTF8); } catch (Exception ex) { // NOSONAR LOGGER.error("Exception!!", ex); } return hashPassword; }
From source file:org.codice.ddf.platform.error.handler.impl.DefaultErrorHandler.java
@Override public void handleError(int code, String message, String type, Throwable throwable, String uri, HttpServletRequest request, HttpServletResponse response) { initIndexHtml();//from ww w. j a va 2 s. c o m String stack = ExceptionUtils.getFullStackTrace(throwable); Map<String, String> jsonMap = new HashMap<>(); jsonMap.put("code", String.valueOf(code)); jsonMap.put("message", message); jsonMap.put("type", type); jsonMap.put("throwable", stack); jsonMap.put("uri", uri); String data = toJson(jsonMap); String encodedBytes = Base64.getEncoder().encodeToString(data.getBytes(StandardCharsets.UTF_8)); String localIndexHtml = indexHtml.replace("WINDOW_DATA", "\"" + encodedBytes + "\""); response.setStatus(code); response.setContentType("text/html"); try (ByteArrayISO8859Writer writer = new ByteArrayISO8859Writer(BUFFER_SIZE)) { writer.write(localIndexHtml); writer.flush(); writer.writeTo(response.getOutputStream()); } catch (IOException e) { LOGGER.error("Unable to write error html data to client."); } }
From source file:com.thoughtworks.go.util.FileUtil.java
public static String filesystemSafeFileHash(File folder) { String hash = Base64.getEncoder().encodeToString(DigestUtils.sha1(folder.getAbsolutePath().getBytes())); hash = hash.replaceAll("[^0-9a-zA-Z\\.\\-]", ""); return hash;//from ww w . java 2 s. c o m }
From source file:io.github.azige.bbs.service.AccountService.java
public String generateSalt() { byte[] saltBytes = new byte[20]; random.nextBytes(saltBytes);//from w ww. j av a 2 s .c om return Base64.getEncoder().encodeToString(saltBytes); }
From source file:org.pentaho.ctools.cpf.repository.rca.RemoteReadAccess.java
public RemoteReadAccess(String reposURL, String username, String password) { this.reposURL = reposURL; client = ClientBuilder.newClient()// w w w .jav a 2 s . c o m // Register Authentication provider .register((ClientRequestFilter) requestContext -> { byte[] passwordBytes = password.getBytes(); Charset CHARACTER_SET = Charset.forName("iso-8859-1"); final byte[] prefix = (username + ":").getBytes(CHARACTER_SET); final byte[] usernamePassword = new byte[prefix.length + passwordBytes.length]; System.arraycopy(prefix, 0, usernamePassword, 0, prefix.length); System.arraycopy(passwordBytes, 0, usernamePassword, prefix.length, passwordBytes.length); String authentication = "Basic " + new String(Base64.getEncoder().encode(usernamePassword), "ASCII"); requestContext.getHeaders().add(HttpHeaders.AUTHORIZATION, authentication); }) // Register ImportMessage MessageBodyWriter .register(org.pentaho.ctools.cpf.repository.rca.ImportMessageBodyWriter.class); }
From source file:com.centonni.kpakpato.api.sms.OrangeSmsAPI.java
/** * This method should return the authorization that basically contain * the authorization token to acces the provider Api * @see AuthenticationToken/*from ww w . jav a 2 s .com*/ * @return */ AuthenticationToken getToken() { String clientInfos = clientId + ":" + clientSecret; String body = "grant_type=client_credentials"; AuthenticationToken authenticationToken = null; try { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost postRequest = new HttpPost(OrangeSmsAPI.BASE_URL + "/" + OrangeSmsAPI.TOKEN_URL); postRequest.setHeader("Authorization", "Basic " + Base64.getEncoder().encodeToString(clientInfos.getBytes())); StringEntity input = new StringEntity(body); input.setContentType("application/x-www-form-urlencoded"); postRequest.setEntity(input); HttpResponse response = httpClient.execute(postRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException( "Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } final ObjectMapper objectMapper = new ObjectMapper(); authenticationToken = objectMapper.readValue(response.getEntity().getContent(), AuthenticationToken.class); } catch (JsonParseException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } return authenticationToken; }
From source file:br.com.ufjf.labredes.rest.EleitorResource.java
@POST @Path("/votar/{voto}") public String votar(@PathParam("voto") String vote) { byte[] voto_decoded = Base64.getDecoder().decode(vote); SealedObject aux = (SealedObject) SerializationUtils.deserialize(voto_decoded); Voto voto = null;//from w ww .j a v a2 s . com voto = (Voto) rsa.decrypt(aux, client_aes); Response ans = new Response(); if (voto != null) { eleitorService.votar(voto.getCpf(), voto.getNumero()); ans.Ok("Voto computado"); //3.1 Responde byte[] data = SerializationUtils.serialize(rsa.encrypt(ans, client_aes)); String retorno = Base64.getEncoder().encodeToString(data); return retorno; } else { ans.Error("Erro ao decriptar chave"); //3.1 Responde byte[] data = SerializationUtils.serialize(rsa.encrypt(ans, client_aes)); String retorno = Base64.getEncoder().encodeToString(data); return retorno; } }
From source file:io.github.azige.bbs.service.AccountService.java
public String encryptPassword(String password, String salt) { return Base64.getEncoder() .encodeToString(sha1.digest((salt + password).getBytes(Charset.forName("UTF-8")))); }