public static String encodeWork(Work work) { try { try (ByteArrayOutputStream binaryOutput = new ByteArrayOutputStream()) { try (ObjectOutputStream objectStream = new ObjectOutputStream(binaryOutput)) { objectStream.writeObject(work); } return Base64.getEncoder().encodeToString(binaryOutput.toByteArray()); } } catch (IOException e) { throw bomb(e); } }
byte [] data = Base64.getDecoder().decode( s ); ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream( data ) ); Object o = ois.readObject(); ois.close(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream( baos ); oos.writeObject( o ); oos.close(); return Base64.getEncoder().encodeToString(baos.toByteArray());
/** * Base64-encode the given byte array using the RFC 4648 * "URL and Filename Safe Alphabet". * @param src the original byte array * @return the encoded byte array * @since 4.2.4 */ public static byte[] encodeUrlSafe(byte[] src) { if (src.length == 0) { return src; } return Base64.getUrlEncoder().encode(src); }
import java.util.Base64; byte[] bytes = "Hello, World!".getBytes("UTF-8"); String encoded = Base64.getEncoder().encodeToString(bytes); byte[] decoded = Base64.getDecoder().decode(encoded);
if (sourceManifestBytes != null) { try { sourceManifest = new Manifest(new ByteArrayInputStream(sourceManifestBytes)); } catch (IOException e) { throw new IllegalArgumentException("Failed to parse source MANIFEST.MF", e); ByteArrayOutputStream manifestOut = new ByteArrayOutputStream(); Attributes mainAttrs = new Attributes(); entryAttrs.putValue( entryDigestAttributeName, Base64.getEncoder().encodeToString(entryDigest)); ByteArrayOutputStream sectionOut = new ByteArrayOutputStream(); byte[] sectionBytes; try { ManifestWriter.writeIndividualSection(sectionOut, entryName, entryAttrs); sectionBytes = sectionOut.toByteArray(); manifestOut.write(sectionBytes); } catch (IOException e) {
public static Work decodeWork(String data) { try { byte[] binary = Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8)); try (ObjectInputStream objectStream = new ObjectInputStream(new ByteArrayInputStream(binary))) { return (Work) objectStream.readObject(); } } catch (ClassNotFoundException | IOException e) { throw bomb(e); } }
mainAttrs.putValue( getManifestDigestAttributeName(manifestDigestAlgorithm), Base64.getEncoder().encodeToString(md.digest(manifest.contents))); ByteArrayOutputStream out = new ByteArrayOutputStream(); try { SignatureFileWriter.writeMainSection(out, mainAttrs); String sectionName = manifestSection.getKey(); byte[] sectionContents = manifestSection.getValue(); byte[] sectionDigest = md.digest(sectionContents); Attributes attrs = new Attributes(); attrs.putValue( entryDigestAttributeName, Base64.getEncoder().encodeToString(sectionDigest)); if ((out.size() > 0) && ((out.size() % 1024) == 0)) { try { SignatureFileWriter.writeSectionDelimiter(out);
protected static String createExpectedResponse(String secKey) throws IOException { try { final String concat = secKey + MAGIC_NUMBER; final MessageDigest digest = MessageDigest.getInstance("SHA1"); digest.update(concat.getBytes("UTF-8")); final byte[] bytes = digest.digest(); return Base64.getEncoder().encodeToString(bytes); } catch (NoSuchAlgorithmException e) { throw new IOException(e); } } }
private String serialize(final Serializable serializable) { if (serializable == null) { return null; } ByteArrayOutputStream out = new ByteArrayOutputStream(); try { final Marshaller marshaller = factory.createMarshaller(configuration); marshaller.start(new OutputStreamByteOutput(out)); marshaller.writeObject(serializable); marshaller.finish(); out.flush(); } catch (IOException e) { throw new RuntimeException(e); } return Base64.getEncoder().encodeToString(out.toByteArray()); }
/** * Encodes the specified {@code message} into a deflated base64 string. */ static String toDeflatedBase64(SAMLObject message) { requireNonNull(message, "message"); final String messageStr; try { messageStr = nodeToString(XMLObjectSupport.marshall(message)); } catch (MarshallingException e) { throw new SamlException("failed to serialize a SAML message", e); } final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); try (DeflaterOutputStream deflaterStream = new DeflaterOutputStream(Base64.getEncoder().wrap(bytesOut), new Deflater(Deflater.DEFLATED, true))) { deflaterStream.write(messageStr.getBytes(StandardCharsets.UTF_8)); } catch (IOException e) { throw new SamlException("failed to deflate a SAML message", e); } return bytesOut.toString(); }
/** * MD5 Base64 加密 * * @param str 待加密的字符串 * @return 加密后的字符串 */ public static String md5Base64(String str) { //确定计算方法 try { MessageDigest md5 = MessageDigest.getInstance(Constants.MD5); //加密后的字符串 byte[] src = md5.digest(str.getBytes(StandardCharsets.UTF_8)); return Base64.getEncoder().encodeToString(src); } catch (Exception e) { throw ExceptionUtils.mpe(e); } } }
ByteArrayInputStream inputBytes = new ByteArrayInputStream(dataBytes); try (DataInputStream input = new DataInputStream(inputBytes)) { ByteBuffer byteBuffer = StandardCharsets.UTF_8.encode(CharBuffer.wrap(chars)); bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); Arrays.fill(byteBuffer.array(), (byte)0); tmpBytes[i] = (byte)chars[i]; // PBE only stores the lower 8 bits, so this narrowing is ok bytes = Base64.getDecoder().decode(tmpBytes); Arrays.fill(tmpBytes, (byte)0);
final String authorization = httpRequest.getHeader("Authorization"); if (authorization != null && authorization.startsWith("Basic")) { // Authorization: Basic base64credentials String base64Credentials = authorization.substring("Basic".length()).trim(); String credentials = new String(Base64.getDecoder().decode(base64Credentials), Charset.forName("UTF-8")); // credentials = username:password final String[] values = credentials.split(":",2);
public static String md5(byte[] bytes, int offset, int len) { try { MessageDigest md = MessageDigestUtils.pooledMd5MessageDigest(); md.update(bytes, offset, len); return Base64.getEncoder().encodeToString(md.digest()); } catch (Exception e) { throw new RuntimeException(e); } }
public static String getAcceptKey(String key) { return Base64.getEncoder().encodeToString(pooledSha1MessageDigest().digest( (key + MAGIC_GUID).getBytes(US_ASCII))); } }
@Test public void testGetBinary() { byte[] bytes = TestUtils.randomByteArray(100); jsonObject.put("foo", bytes); assertArrayEquals(bytes, jsonObject.getBinary("foo")); assertEquals(Base64.getEncoder().encodeToString(bytes), jsonObject.getValue("foo")); String val = jsonObject.getString("foo"); assertNotNull(val); byte[] retrieved = Base64.getDecoder().decode(val); assertTrue(TestUtils.byteArraysEqual(bytes, retrieved));
@Test public void testGetValue() { jsonObject.put("foo", 123); assertEquals(123, jsonObject.getValue("foo")); jsonObject.put("foo", 123l); assertEquals(123l, jsonObject.getValue("foo")); jsonObject.put("foo", 123f); byte[] bytes = TestUtils.randomByteArray(100); jsonObject.put("foo", bytes); assertTrue(TestUtils.byteArraysEqual(bytes, Base64.getDecoder().decode((String) jsonObject.getValue("foo")))); jsonObject.putNull("foo"); assertNull(jsonObject.getValue("foo"));
private static String decompress(String s) throws UnsupportedEncodingException { return new String( decompressAlgorithm( Base64.getUrlDecoder().decode( URLDecoder.decode(s, ENCODING.name()) .getBytes(ENCODING) ) ), ENCODING); }
/** * Base64-encode the given byte array. * @param src the original byte array * @return the encoded byte array */ public static byte[] encode(byte[] src) { if (src.length == 0) { return src; } return Base64.getEncoder().encode(src); }
@Test public void testFromJsonObject() { JsonObject object = new JsonObject(); object.put("string", "the_string"); object.put("integer", 4); object.put("boolean", true); object.put("binary", "hello".getBytes()); object.put("object", new JsonObject().put("nested", 4)); object.put("array", new JsonArray().add(1).add(2).add(3)); Map<String, Object> map = ConversionHelper.fromObject(object); assertEquals(6, map.size()); assertEquals("the_string", map.get("string")); assertEquals(4, map.get("integer")); assertEquals(true, map.get("boolean")); assertEquals("hello", new String(Base64.getDecoder().decode((String)map.get("binary")))); assertEquals(Collections.singletonMap("nested", 4), map.get("object")); assertEquals(Arrays.asList(1, 2, 3), map.get("array")); }