private ByteString randomToken() { byte[] bytes = new byte[16]; secureRandom.nextBytes(bytes); return ByteString.of(bytes); }
/** * Returns the RSA private key encoded in <a href="https://tools.ietf.org/html/rfc5208">PKCS * #8</a> <a href="https://tools.ietf.org/html/rfc7468">PEM format</a>. */ public String privateKeyPkcs8Pem() { StringBuilder result = new StringBuilder(); result.append("-----BEGIN PRIVATE KEY-----\n"); encodeBase64Lines(result, ByteString.of(keyPair.getPrivate().getEncoded())); result.append("-----END PRIVATE KEY-----\n"); return result.toString(); }
static ByteString sha256(X509Certificate x509Certificate) { return ByteString.of(x509Certificate.getPublicKey().getEncoded()).sha256(); }
static ByteString sha1(X509Certificate x509Certificate) { return ByteString.of(x509Certificate.getPublicKey().getEncoded()).sha1(); }
/** * Returns the certificate encoded in <a href="https://tools.ietf.org/html/rfc7468">PEM * format</a>. */ public String certificatePem() { try { StringBuilder result = new StringBuilder(); result.append("-----BEGIN CERTIFICATE-----\n"); encodeBase64Lines(result, ByteString.of(certificate.getEncoded())); result.append("-----END CERTIFICATE-----\n"); return result.toString(); } catch (CertificateEncodingException e) { throw new AssertionError(e); } }
private ByteString pkcs1Bytes() { try { PrivateKeyInfo privateKeyInfo = PrivateKeyInfo.getInstance(keyPair.getPrivate().getEncoded()); return ByteString.of(privateKeyInfo.parsePrivateKey().toASN1Primitive().getEncoded()); } catch (IOException e) { throw new AssertionError(e); } }
public RealWebSocket(Request request, WebSocketListener listener, Random random, long pingIntervalMillis) { if (!"GET".equals(request.method())) { throw new IllegalArgumentException("Request must be GET: " + request.method()); } this.originalRequest = request; this.listener = listener; this.random = random; this.pingIntervalMillis = pingIntervalMillis; byte[] nonce = new byte[16]; random.nextBytes(nonce); this.key = ByteString.of(nonce).base64(); this.writerRunnable = () -> { try { while (writeOneFrame()) { } } catch (IOException e) { failWebSocket(e, null); } }; }
private void writeCertList(BufferedSink sink, List<Certificate> certificates) throws IOException { try { sink.writeDecimalLong(certificates.size()) .writeByte('\n'); for (int i = 0, size = certificates.size(); i < size; i++) { byte[] bytes = certificates.get(i).getEncoded(); String line = ByteString.of(bytes).base64(); sink.writeUtf8(line) .writeByte('\n'); } } catch (CertificateEncodingException e) { throw new IOException(e.getMessage()); } }
private ByteString gzipHeaderWithFlags(byte flags) { byte[] result = gzipHeader.toByteArray(); result[3] = flags; return ByteString.of(result); }
/** Reads a potentially Huffman encoded byte string. */ ByteString readByteString() throws IOException { int firstByte = readByte(); boolean huffmanDecode = (firstByte & 0x80) == 0x80; // 1NNNNNNN int length = readInt(firstByte, PREFIX_7_BITS); if (huffmanDecode) { return ByteString.of(Huffman.get().decode(source.readByteArray(length))); } else { return source.readByteString(length); } } }
@Test public void ofByteBuffer() { byte[] bytes = "Hello, World!".getBytes(Charsets.UTF_8); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); byteBuffer.position(2).limit(11); ByteString byteString = ByteString.of(byteBuffer); // Verify that the bytes were copied out. byteBuffer.put(4, (byte) 'a'); assertEquals("llo, Worl", byteString.utf8()); }
@Test public void ofCopyRange() { byte[] bytes = "Hello, World!".getBytes(Charsets.UTF_8); ByteString byteString = ByteString.of(bytes, 2, 9); // Verify that the bytes were copied out. bytes[4] = (byte) 'a'; assertEquals("llo, Worl", byteString.utf8()); }
@Test public void ofCopy() { byte[] bytes = "Hello, World!".getBytes(Charsets.UTF_8); ByteString byteString = ByteString.of(bytes); // Verify that the bytes were copied out. bytes[4] = (byte) 'a'; assertEquals("Hello, World!", byteString.utf8()); }
@Test public void asByteBuffer() { assertEquals(0x42, ByteString.of((byte) 0x41, (byte) 0x42, (byte) 0x43).asByteBuffer().get(1)); } }
@Test public void decodeNullCharset() throws Exception { try { ByteString.of().string(null); fail(); } catch (IllegalArgumentException expected) { } }
@Test public void read() throws Exception { InputStream in = new ByteArrayInputStream("abc".getBytes(Charsets.UTF_8)); assertEquals(ByteString.decodeHex("6162"), ByteString.read(in, 2)); assertEquals(ByteString.decodeHex("63"), ByteString.read(in, 1)); assertEquals(ByteString.of(), ByteString.read(in, 0)); }
@Test public void accessByteByByte() throws Exception { Buffer buffer = bufferFactory.newBuffer(); try (UnsafeCursor cursor = buffer.readUnsafe()) { byte[] actual = new byte[(int) buffer.size()]; for (int i = 0; i < buffer.size(); i++) { cursor.seek(i); actual[i] = cursor.data[cursor.start]; } assertEquals(ByteString.of(actual), buffer.snapshot()); } }
@Test public void accessByteByByteReverse() throws Exception { Buffer buffer = bufferFactory.newBuffer(); try (UnsafeCursor cursor = buffer.readUnsafe()) { byte[] actual = new byte[(int) buffer.size()]; for (int i = (int) (buffer.size() - 1); i >= 0; i--) { cursor.seek(i); actual[i] = cursor.data[cursor.start]; } assertEquals(ByteString.of(actual), buffer.snapshot()); } }
@Test public void accessByteByByteAlwaysResettingToZero() throws Exception { Buffer buffer = bufferFactory.newBuffer(); try (UnsafeCursor cursor = buffer.readUnsafe()) { byte[] actual = new byte[(int) buffer.size()]; for (int i = 0; i < buffer.size(); i++) { cursor.seek(i); actual[i] = cursor.data[cursor.start]; cursor.seek(0L); } assertEquals(ByteString.of(actual), buffer.snapshot()); } }
@SuppressWarnings("SelfEquals") @Test public void equals() throws Exception { ByteString byteString = factory.decodeHex("000102"); assertTrue(byteString.equals(byteString)); assertTrue(byteString.equals(ByteString.decodeHex("000102"))); assertTrue(factory.decodeHex("").equals(ByteString.EMPTY)); assertTrue(factory.decodeHex("").equals(ByteString.of())); assertTrue(ByteString.EMPTY.equals(factory.decodeHex(""))); assertTrue(ByteString.of().equals(factory.decodeHex(""))); assertFalse(byteString.equals(new Object())); assertFalse(byteString.equals(ByteString.decodeHex("000201"))); }