@Override public String toString() { return hashAlgorithm + hash.base64(); } }
private void encodeBase64Lines(StringBuilder out, ByteString data) { String base64 = data.base64(); for (int i = 0; i < base64.length(); i += 64) { out.append(base64, i, Math.min(i + 64, base64.length())).append('\n'); } }
@Override public String toString() { return hashAlgorithm + hash.base64(); } }
public static String basic(String username, String password, Charset charset) { String usernameAndPassword = username + ":" + password; String encoded = ByteString.encodeString(usernameAndPassword, charset).base64(); return "Basic " + encoded; } }
/** * Returns the SHA-256 of {@code certificate}'s public key. * * <p>In OkHttp 3.1.2 and earlier, this returned a SHA-1 hash of the public key. Both types are * supported, but SHA-256 is preferred. */ public static String pin(Certificate certificate) { if (!(certificate instanceof X509Certificate)) { throw new IllegalArgumentException("Certificate pinning requires X509 certificates"); } return "sha256/" + sha256((X509Certificate) certificate).base64(); }
@Override public void write(JsonWriter out, @Nullable ByteString value) throws IOException { if (value == null) { out.nullValue(); } else { out.value(value.base64()); } }
@Override public void toJson(JsonWriter writer, ByteString value) throws IOException { String string = value.base64(); writer.value(string); } }
/** * Returns the SHA-256 of {@code certificate}'s public key. * * <p>In OkHttp 3.1.2 and earlier, this returned a SHA-1 hash of the public key. Both types are * supported, but SHA-256 is preferred. */ public static String pin(Certificate certificate) { if (!(certificate instanceof X509Certificate)) { throw new IllegalArgumentException("Certificate pinning requires X509 certificates"); } return "sha256/" + sha256((X509Certificate) certificate).base64(); }
public static String acceptHeader(String key) { return ByteString.encodeUtf8(key + WebSocketProtocol.ACCEPT_MAGIC).sha1().base64(); }
public static String basic(String username, String password, Charset charset) { String usernameAndPassword = username + ":" + password; String encoded = ByteString.encodeString(usernameAndPassword, charset).base64(); return "Basic " + encoded; } }
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); } }; }
@Override public void toJson(JsonWriter out, ByteString byteString) throws IOException { out.value(byteString.base64()); }
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()); } }
/** See https://api.slack.com/docs/oauth. */ public HttpUrl authorizeUrl(String scopes, HttpUrl redirectUrl, ByteString state, String team) { HttpUrl.Builder builder = baseUrl.newBuilder("/oauth/authorize") .addQueryParameter("client_id", clientId) .addQueryParameter("scope", scopes) .addQueryParameter("redirect_uri", redirectUrl.toString()) .addQueryParameter("state", state.base64()); if (team != null) { builder.addQueryParameter("team", team); } return builder.build(); }
void checkResponse(Response response) throws ProtocolException { if (response.code() != 101) { throw new ProtocolException("Expected HTTP 101 response but was '" + response.code() + " " + response.message() + "'"); } String headerConnection = response.header("Connection"); if (!"Upgrade".equalsIgnoreCase(headerConnection)) { throw new ProtocolException("Expected 'Connection' header value 'Upgrade' but was '" + headerConnection + "'"); } String headerUpgrade = response.header("Upgrade"); if (!"websocket".equalsIgnoreCase(headerUpgrade)) { throw new ProtocolException( "Expected 'Upgrade' header value 'websocket' but was '" + headerUpgrade + "'"); } String headerAccept = response.header("Sec-WebSocket-Accept"); String acceptExpected = ByteString.encodeUtf8(key + WebSocketProtocol.ACCEPT_MAGIC) .sha1().base64(); if (!acceptExpected.equals(headerAccept)) { throw new ProtocolException("Expected 'Sec-WebSocket-Accept' header value '" + acceptExpected + "' but was '" + headerAccept + "'"); } }
public static String acceptHeader(String key) { return ByteString.encodeUtf8(key + WebSocketProtocol.ACCEPT_MAGIC).sha1().base64(); }
public void run() throws Exception { Point point = new Point(8.0, 15.0); ByteString pointBytes = serialize(point); System.out.println(pointBytes.base64()); ByteString goldenBytes = ByteString.decodeBase64("rO0ABXNyAB5va2lvLnNhbXBsZ" + "XMuR29sZGVuVmFsdWUkUG9pbnTdUW8rMji1IwIAAkQAAXhEAAF5eHBAIAAAAAAAAEAuA" + "AAAAAAA"); Point decoded = (Point) deserialize(goldenBytes); assertEquals(point, decoded); }
@Test public void encodeBase64() { assertEquals("", factory.encodeUtf8("").base64()); assertEquals("AA==", factory.encodeUtf8("\u0000").base64()); assertEquals("AAA=", factory.encodeUtf8("\u0000\u0000").base64()); assertEquals("AAAA", factory.encodeUtf8("\u0000\u0000\u0000").base64()); assertEquals("SG93IG1hbnkgbGluZXMgb2YgY29kZSBhcmUgdGhlcmU/ICdib3V0IDIgbWlsbGlvbi4=", factory.encodeUtf8("How many lines of code are there? 'bout 2 million.").base64()); }
@Test public void open_authExternal() throws Exception { settings.setAuthType(AuthType.EXTERNAL); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "AUTH=EXTERNAL"); server.expect("2 AUTHENTICATE EXTERNAL " + ByteString.encodeUtf8(USERNAME).base64()); server.output("2 OK Success"); postAuthenticationDialogRequestingCapabilities(server); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }
@Test public void open_authPlain() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "AUTH=PLAIN"); server.expect("2 AUTHENTICATE PLAIN"); server.output("+"); server.expect(ByteString.encodeUtf8("\000" + USERNAME + "\000" + PASSWORD).base64()); server.output("2 OK Success"); postAuthenticationDialogRequestingCapabilities(server); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }