public static String hashToBase64(ByteBuf objectState) { ByteBuffer bf = objectState.internalNioBuffer(objectState.readerIndex(), objectState.readableBytes()); long h1 = LongHashFunction.farmUo().hashBytes(bf); long h2 = LongHashFunction.xx().hashBytes(bf); ByteBuf buf = ByteBufAllocator.DEFAULT.buffer((2 * Long.SIZE) / Byte.SIZE); try { buf.writeLong(h1).writeLong(h2); ByteBuf b = Base64.encode(buf); try { String s = b.toString(CharsetUtil.UTF_8); return s.substring(0, s.length() - 2); } finally { b.release(); } } finally { buf.release(); } }
protected void encode(ChannelHandlerContext ctx, Object msg, List out) throws Exception { ByteBuf ENC = Unpooled.wrappedBuffer("ENC ".getBytes(data.encoding)); ByteBuf RN = Unpooled.wrappedBuffer("\r\n".getBytes(data.encoding)); Log.finer("Sending command: ", msg); ByteBuf raw = Unpooled.wrappedBuffer(msg.toString().getBytes(data.encoding)); if (data.security != null) { ByteBuf eb = Base64.encode(data.security.protect(raw), false); ctx.write(ENC); ctx.write(eb); } else { ctx.write(raw); } ctx.writeAndFlush(RN); }
/** Return a Base64-encoded string. */ private static String getBase64EncodedString(String str) { ByteBuf byteBuf = null; ByteBuf encodedByteBuf = null; try { byteBuf = Unpooled.wrappedBuffer(str.getBytes(StandardCharsets.UTF_8)); encodedByteBuf = Base64.encode(byteBuf); return encodedByteBuf.toString(StandardCharsets.UTF_8); } finally { // The release is called to suppress the memory leak error messages raised by netty. if (byteBuf != null) { byteBuf.release(); if (encodedByteBuf != null) { encodedByteBuf.release(); } } } }
/** * Encodes the buffered image into the encoded favicon string. * * @param image the buffered image * @return the favicon string */ private static String encode(BufferedImage image) throws IOException { checkArgument(image.getWidth() == 64, "favicon must be 64 pixels wide"); checkArgument(image.getHeight() == 64, "favicon must be 64 pixels high"); ByteBuf buf = Unpooled.buffer(); try { ImageIO.write(image, "PNG", new ByteBufOutputStream(buf)); ByteBuf base64 = Base64.encode(buf); try { return FAVICON_PREFIX + base64.toString(StandardCharsets.UTF_8); } finally { base64.release(); } } finally { buf.release(); } }
/** * Decodes the buffered image from the encoded favicon string. * * @param encoded the encoded string * @return the buffered image */ private static BufferedImage decode(String encoded) throws IOException { checkArgument(encoded.startsWith(FAVICON_PREFIX), "unknown favicon format"); ByteBuf base64 = Unpooled.copiedBuffer(encoded.substring(FAVICON_PREFIX.length()), StandardCharsets.UTF_8); try { ByteBuf buf = Base64.decode(base64); try { BufferedImage result = ImageIO.read(new ByteBufInputStream(buf)); checkState(result.getWidth() == 64, "favicon must be 64 pixels wide"); checkState(result.getHeight() == 64, "favicon must be 64 pixels high"); return result; } finally { buf.release(); } } finally { base64.release(); } }
static ByteBuf readPrivateKey(File file) throws KeyException { String content; try { content = readContent(file); } catch (IOException e) { throw new KeyException("failed to read a file: " + file, e); } Matcher m = KEY_PATTERN.matcher(content); if (!m.find()) { throw new KeyException("found no private key: " + file); } ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII); ByteBuf der = Base64.decode(base64); base64.release(); return der; }
private void a(ServerPing serverping) { File file = this.d("server-icon.png"); if (file.isFile()) { ByteBuf bytebuf = Unpooled.buffer(); try { BufferedImage bufferedimage = ImageIO.read(file); Validate.validState(bufferedimage.getWidth() == 64, "Must be 64 pixels wide", new Object[0]); Validate.validState(bufferedimage.getHeight() == 64, "Must be 64 pixels high", new Object[0]); ImageIO.write(bufferedimage, "PNG", new ByteBufOutputStream(bytebuf)); ByteBuf bytebuf1 = Base64.encode(bytebuf); serverping.setFavicon("data:image/png;base64," + bytebuf1.toString(Charsets.UTF_8)); } catch (Exception exception) { MinecraftServer.LOGGER.error("Couldn\'t load server icon", exception); } finally { bytebuf.release(); } } }
private void func_147138_a(ServerStatusResponse p_147138_1_) { File var2 = this.getFile("server-icon.png"); if (var2.isFile()) { ByteBuf var3 = Unpooled.buffer(); try { BufferedImage var4 = ImageIO.read(var2); Validate.validState(var4.getWidth() == 64, "Must be 64 pixels wide", new Object[0]); Validate.validState(var4.getHeight() == 64, "Must be 64 pixels high", new Object[0]); ImageIO.write(var4, "PNG", new ByteBufOutputStream(var3)); ByteBuf var5 = Base64.encode(var3); p_147138_1_.func_151320_a("data:image/png;base64," + var5.toString(Charsets.UTF_8)); } catch (Exception var6) { logger.error("Couldn\'t load server icon", var6); } } }
protected void saveKeyPairAndCertificateToFile() throws Exception { if(localPrivateKeyFile==null){ LOGGER.info("not saving private key nor certificate"); return; } //Encode in PEM format, the format prefered by openssl // if(false){ // PEMWriter pemWriter=new PEMWriter(new FileWriter(localPrivateKeyFile)); // pemWriter.writeObject(localPrivateECKey); // pemWriter.close(); // } // else{ String keyText = "-----BEGIN EC PRIVATE KEY-----\n" + Base64.encode(Unpooled.wrappedBuffer(localPrivateECKey.getEncoded()), true).toString(CharsetUtil.US_ASCII) + "\n-----END EC PRIVATE KEY-----\n"; Files.write(keyText, localPrivateKeyFile, CharsetUtil.US_ASCII); Files.write(localId.toString(), new File(localPrivateKeyFile.getParentFile(), "localPublic.hash"), CharsetUtil.US_ASCII); // } PEMWriter certificateWriter=new PEMWriter(new FileWriter(localCertificateFile)); certificateWriter.writeObject(cert); certificateWriter.close(); LOGGER.info("Saved to "+localCertificateFile.getAbsolutePath()); }
/** * Performs auth. */ private void performAuth() { byte[] authToken = ("\0" + jid.getNode() + "\0" + password).getBytes(StandardCharsets.UTF_8); Element auth = new Element("auth", "urn:ietf:params:xml:ns:xmpp-sasl"); auth.setAttribute("mechanism", "PLAIN"); ByteBuf rawCredentials = channel.get().alloc().buffer().writeBytes(authToken); ByteBuf encodedCredentials = Base64.encode(rawCredentials); String encodedCredentialsString = encodedCredentials.toString(StandardCharsets.UTF_8); encodedCredentials.release(); rawCredentials.release(); auth.setText(encodedCredentialsString); channel.get().writeAndFlush(auth); }
public static String create(BufferedImage image) throws IOException { checkArgument(image.getWidth() == 64, "favicon must be 64 pixels wide"); checkArgument(image.getHeight() == 64, "favicon must be 64 pixels high"); ByteBuf buf = Unpooled.buffer(); try { ImageIO.write(image, "PNG", new ByteBufOutputStream(buf)); ByteBuf base64 = Base64.encode(buf); try { return FAVICON_PREFIX + base64.toString(Charsets.UTF_8); } finally { base64.release(); } } finally { buf.release(); } }
static CraftIconCache loadServerIcon0(BufferedImage image) throws Exception { ByteBuf bytebuf = Unpooled.buffer(); Validate.isTrue(image.getWidth() == 64, "Must be 64 pixels wide"); Validate.isTrue(image.getHeight() == 64, "Must be 64 pixels high"); ImageIO.write(image, "PNG", new ByteBufOutputStream(bytebuf)); ByteBuf bytebuf1 = Base64.encode(bytebuf); return new CraftIconCache("data:image/png;base64," + bytebuf1.toString(Charsets.UTF_8)); }
private void decodeProtected(Reply reply) { if (!reply.isProtected()) throw new RuntimeException("Unprotected reply on protected channel."); for (String s : reply.lines()) try { ByteBuf eb = Unpooled.wrappedBuffer(s.getBytes(data.encoding)); ByteBuf db = data.security.unprotect(Base64.decode(eb)); protCodec.feed(new Slice(db).asBytes()); } catch (Exception e) { throw new RuntimeException("Bad reply from server.", e); } }
private Bell<Reply> handshake(final SecurityContext sec, ByteBuf it) { final Bell<Reply> bell = new Bell<Reply>(); try { ByteBuf ot = Base64.encode(sec.handshake(it), false); Log.finer("Sending ADAT: ", ot); new Command("ADAT", ot.toString(data.encoding)) { public void done(Reply r) throws Exception { if (r.isIncomplete()) { String line = r.message().substring(5); ByteBuf bb = Unpooled.wrappedBuffer(line.getBytes(data.encoding)); ByteBuf token = Base64.decode(bb); handshake(sec, token).promise(bell); } else if (r.isComplete()) { promise(bell); } else { throw r.asError(); } } }; } catch (Exception e) { Log.fine("ADAT failed: ", e); bell.ring(e); } return bell; }
private void addFaviconToStatusResponse(ServerStatusResponse response) { File file1 = this.getFile("server-icon.png"); if (file1.isFile()) { ByteBuf bytebuf = Unpooled.buffer(); try { BufferedImage bufferedimage = ImageIO.read(file1); Validate.validState(bufferedimage.getWidth() == 64, "Must be 64 pixels wide", new Object[0]); Validate.validState(bufferedimage.getHeight() == 64, "Must be 64 pixels high", new Object[0]); ImageIO.write(bufferedimage, "PNG", (OutputStream)(new ByteBufOutputStream(bytebuf))); ByteBuf bytebuf1 = Base64.encode(bytebuf); response.setFavicon("data:image/png;base64," + bytebuf1.toString(Charsets.UTF_8)); } catch (Exception exception) { logger.error((String)"Couldn\'t load server icon", (Throwable)exception); } finally { bytebuf.release(); } } }
public void applyServerIconToResponse(ServerStatusResponse response) { File file1 = this.getFile("server-icon.png"); if (!file1.exists()) { file1 = this.getActiveAnvilConverter().getFile(this.getFolderName(), "icon.png"); } if (file1.isFile()) { ByteBuf bytebuf = Unpooled.buffer(); try { BufferedImage bufferedimage = ImageIO.read(file1); Validate.validState(bufferedimage.getWidth() == 64, "Must be 64 pixels wide", new Object[0]); Validate.validState(bufferedimage.getHeight() == 64, "Must be 64 pixels high", new Object[0]); ImageIO.write(bufferedimage, "PNG", (OutputStream)(new ByteBufOutputStream(bytebuf))); ByteBuf bytebuf1 = Base64.encode(bytebuf); response.setFavicon("data:image/png;base64," + bytebuf1.toString(Charsets.UTF_8)); } catch (Exception exception) { LOG.error((String)"Couldn\'t load server icon", (Throwable)exception); } finally { bytebuf.release(); } } }
public Base64AuthToken(String user, String password) throws Exception { _user = user; String data = user + ":" + password; ByteBuf digest = Base64.encode(Unpooled.wrappedBuffer(data .getBytes("UTF-8"))); byte[] digestBytes = new byte[digest.readableBytes()]; digest.readBytes(digestBytes); super.setPassword(new String(digestBytes)); }
static String encodeNBT(NBTTagCompound tag) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(new GZIPOutputStream(baos)); CompressedStreamTools.write(tag, dos); dos.close(); byte[] compressedData = baos.toByteArray(); ByteBuf enc = Base64.encode(Unpooled.copiedBuffer(compressedData)); return new String(enc.array()); }
/** * Extracts the username and password details from the HTTP basic header Authorization. * <p/> * This requires that the <tt>Authorization</tt> HTTP header is provided, and its using Basic. * Currently Digest is <b>not</b> supported. * * @return {@link HttpPrincipal} with username and password details, or <tt>null</tt> if not possible to extract */ protected static HttpPrincipal extractBasicAuthSubject(HttpRequest request) { String auth = request.headers().get("Authorization"); if (auth != null) { String constraint = ObjectHelper.before(auth, " "); if (constraint != null) { if ("Basic".equalsIgnoreCase(constraint.trim())) { String decoded = ObjectHelper.after(auth, " "); // the decoded part is base64 encoded, so we need to decode that ByteBuf buf = NettyConverter.toByteBuffer(decoded.getBytes()); ByteBuf out = Base64.decode(buf); try { String userAndPw = out.toString(Charset.defaultCharset()); String username = ObjectHelper.before(userAndPw, ":"); String password = ObjectHelper.after(userAndPw, ":"); HttpPrincipal principal = new HttpPrincipal(username, password); LOG.debug("Extracted Basic Auth principal from HTTP header: {}", principal); return principal; } finally { buf.release(); out.release(); } } } } return null; }
public static FullHttpResponse theBase64Image1pxGif() { ByteBuf byteBuf = Base64.decode(Unpooled.copiedBuffer(BASE64GIF_BYTES)); FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK , byteBuf); response.headers().set(CONTENT_TYPE, ContentTypePool.GIF); response.headers().set(CONTENT_LENGTH, byteBuf.readableBytes()); response.headers().set(CONNECTION, HEADER_CONNECTION_CLOSE); return response; }
/** * Performs base64 encoding on the specified data * * @param data The data to encode * @return An encoded string containing the data */ static String base64(byte[] data) { ByteBuf encodedData = Unpooled.wrappedBuffer(data); ByteBuf encoded = Base64.encode(encodedData); String encodedString = encoded.toString(CharsetUtil.UTF_8); encoded.release(); return encodedString; }
static ByteBuf[] readCertificates(File file) throws CertificateException { String content; try { content = readContent(file); } catch (IOException e) { throw new CertificateException("failed to read a file: " + file, e); } List<ByteBuf> certs = new ArrayList<ByteBuf>(); Matcher m = CERT_PATTERN.matcher(content); int start = 0; for (;;) { if (!m.find(start)) { break; } ByteBuf base64 = Unpooled.copiedBuffer(m.group(1), CharsetUtil.US_ASCII); ByteBuf der = Base64.decode(base64); base64.release(); certs.add(der); start = m.end(); } if (certs.isEmpty()) { throw new CertificateException("found no certificates: " + file); } return certs.toArray(new ByteBuf[certs.size()]); }