private static ByteString decodeB(String encodedText) { ByteString decoded = ByteString.decodeBase64(encodedText); return decoded == null ? ByteString.EMPTY : decoded; }
Pin(String pattern, String pin) { this.pattern = pattern; this.canonicalHostname = pattern.startsWith(WILDCARD) ? HttpUrl.get("http://" + pattern.substring(WILDCARD.length())).host() : HttpUrl.get("http://" + pattern).host(); if (pin.startsWith("sha1/")) { this.hashAlgorithm = "sha1/"; this.hash = ByteString.decodeBase64(pin.substring("sha1/".length())); } else if (pin.startsWith("sha256/")) { this.hashAlgorithm = "sha256/"; this.hash = ByteString.decodeBase64(pin.substring("sha256/".length())); } else { throw new IllegalArgumentException("pins must start with 'sha256/' or 'sha1/': " + pin); } if (this.hash == null) { throw new IllegalArgumentException("pins must be base64: " + pin); } }
@Override public ByteString fromJson(JsonReader reader) throws IOException { String base64 = reader.nextString(); return ByteString.decodeBase64(base64); }
@Override public @Nullable ByteString read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } return ByteString.decodeBase64(in.nextString()); } }
@Override public ByteString fromJson(JsonReader in) throws IOException { return ByteString.decodeBase64(in.nextString()); } }.nullSafe();
private List<Certificate> readCertificateList(BufferedSource source) throws IOException { int length = readInt(source); if (length == -1) return Collections.emptyList(); // OkHttp v1.2 used -1 to indicate null. try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); List<Certificate> result = new ArrayList<>(length); for (int i = 0; i < length; i++) { String line = source.readUtf8LineStrict(); Buffer bytes = new Buffer(); bytes.write(ByteString.decodeBase64(line)); result.add(certificateFactory.generateCertificate(bytes.inputStream())); } return result; } catch (CertificateException e) { throw new IOException(e.getMessage()); } }
public UrlSigner(final String keyString) throws NoSuchAlgorithmException, InvalidKeyException { // Convert from URL-safe base64 to regular base64. String base64 = keyString.replace('-', '+').replace('_', '/'); ByteString decodedKey = ByteString.decodeBase64(base64); if (decodedKey == null) { // NOTE: don't log the exception, in case some of the private key leaks to an end-user. throw new IllegalArgumentException("Private key is invalid."); } mac = Mac.getInstance(ALGORITHM_HMAC_SHA1); mac.init(new SecretKeySpec(decodedKey.toByteArray(), ALGORITHM_HMAC_SHA1)); }
Pin(String pattern, String pin) { this.pattern = pattern; this.canonicalHostname = pattern.startsWith(WILDCARD) ? HttpUrl.get("http://" + pattern.substring(WILDCARD.length())).host() : HttpUrl.get("http://" + pattern).host(); if (pin.startsWith("sha1/")) { this.hashAlgorithm = "sha1/"; this.hash = ByteString.decodeBase64(pin.substring("sha1/".length())); } else if (pin.startsWith("sha256/")) { this.hashAlgorithm = "sha256/"; this.hash = ByteString.decodeBase64(pin.substring("sha256/".length())); } else { throw new IllegalArgumentException("pins must start with 'sha256/' or 'sha1/': " + pin); } if (this.hash == null) { throw new IllegalArgumentException("pins must be base64: " + pin); } }
private List<Certificate> readCertificateList(BufferedSource source) throws IOException { int length = readInt(source); if (length == -1) return Collections.emptyList(); // OkHttp v1.2 used -1 to indicate null. try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); List<Certificate> result = new ArrayList<>(length); for (int i = 0; i < length; i++) { String line = source.readUtf8LineStrict(); Buffer bytes = new Buffer(); bytes.write(ByteString.decodeBase64(line)); result.add(certificateFactory.generateCertificate(bytes.inputStream())); } return result; } catch (CertificateException e) { throw new IOException(e.getMessage()); } }
private List<Certificate> readCertificateList(BufferedSource source) throws IOException { int length = readInt(source); if (length == -1) return Collections.emptyList(); // OkHttp v1.2 used -1 to indicate null. try { CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); List<Certificate> result = new ArrayList<>(length); for (int i = 0; i < length; i++) { String line = source.readUtf8LineStrict(); Buffer bytes = new Buffer(); bytes.write(ByteString.decodeBase64(line)); result.add(certificateFactory.generateCertificate(bytes.inputStream())); } return result; } catch (CertificateException e) { throw new IOException(e.getMessage()); } }
private Buffer decodeBase64(String s) { return new Buffer().write(ByteString.decodeBase64(s)); }
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 decodeBase64WithWhitespace() { assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA AA ").utf8()); assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA A\r\nA ").utf8()); assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64("AA AA").utf8()); assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA AA ").utf8()); assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64(" AA A\r\nA ").utf8()); assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64("A AAA").utf8()); assertEquals("", ByteString.decodeBase64(" ").utf8()); }
ByteString byteString = ByteString.decodeBase64(base64KeyData); if (byteString == null) { Timber.e("autocrypt: error parsing base64 data");
@Test public void decodeBase64() { assertEquals("", ByteString.decodeBase64("").utf8()); assertEquals(null, ByteString.decodeBase64("/===")); // Can't do anything with 6 bits! assertEquals(ByteString.decodeHex("ff"), ByteString.decodeBase64("//==")); assertEquals(ByteString.decodeHex("ff"), ByteString.decodeBase64("__==")); assertEquals(ByteString.decodeHex("ffff"), ByteString.decodeBase64("///=")); assertEquals(ByteString.decodeHex("ffff"), ByteString.decodeBase64("___=")); assertEquals(ByteString.decodeHex("ffffff"), ByteString.decodeBase64("////")); assertEquals(ByteString.decodeHex("ffffff"), ByteString.decodeBase64("____")); assertEquals(ByteString.decodeHex("ffffffffffff"), ByteString.decodeBase64("////////")); assertEquals(ByteString.decodeHex("ffffffffffff"), ByteString.decodeBase64("________")); assertEquals("What's to be scared about? It's just a little hiccup in the power...", ByteString.decodeBase64("V2hhdCdzIHRvIGJlIHNjYXJlZCBhYm91dD8gSXQncyBqdXN0IGEgbGl0dGxlIGhpY2" + "N1cCBpbiB0aGUgcG93ZXIuLi4=").utf8()); // Uses two encoding styles. Malformed, but supported as a side-effect. assertEquals(ByteString.decodeHex("ffffff"), ByteString.decodeBase64("__//")); }
@Nullable @VisibleForTesting AutocryptGossipHeader parseAutocryptGossipHeader(String headerValue) { Map<String,String> parameters = MimeUtility.getAllHeaderParameters(headerValue); String type = parameters.remove(AutocryptHeader.AUTOCRYPT_PARAM_TYPE); if (type != null && !type.equals(AutocryptHeader.AUTOCRYPT_TYPE_1)) { Timber.e("autocrypt: unsupported type parameter %s", type); return null; } String base64KeyData = parameters.remove(AutocryptHeader.AUTOCRYPT_PARAM_KEY_DATA); if (base64KeyData == null) { Timber.e("autocrypt: missing key parameter"); return null; } ByteString byteString = ByteString.decodeBase64(base64KeyData); if (byteString == null) { Timber.e("autocrypt: error parsing base64 data"); return null; } String addr = parameters.remove(AutocryptHeader.AUTOCRYPT_PARAM_ADDR); if (addr == null) { Timber.e("autocrypt: no to header!"); return null; } if (hasCriticalParameters(parameters)) { return null; } return new AutocryptGossipHeader(addr, byteString.toByteArray()); }
@Test public void ignoreUnnecessaryPadding() { assertEquals("", ByteString.decodeBase64("====").utf8()); assertEquals("\u0000\u0000\u0000", ByteString.decodeBase64("AAAA====").utf8()); }
String code = requestUrl.queryParameter("code"); String stateString = requestUrl.queryParameter("state"); ByteString state = stateString != null ? ByteString.decodeBase64(stateString) : null;
@Test public void decodeGolden() throws Exception { SimpleMessage goldenValue = new SimpleMessage.Builder() .required_int32(99) .result("tacos") .build(); ByteString goldenSerialized = ByteString.decodeBase64("rO0ABXNyACdjb20uc3F1YXJldXAud2lyZS5NZXNz" + "YWdlU2VyaWFsaXplZEZvcm0AAAAAAAAAAAIAAlsABWJ5dGVzdAACW0JMAAxtZXNzYWdlQ2xhc3N0ABFMamF2YS9s" + "YW5nL0NsYXNzO3hwdXIAAltCrPMX+AYIVOACAAB4cAAAAAkoY1IFdGFjb3N2cgAtY29tLnNxdWFyZXVwLndpcmUu" + "cHJvdG9zLnNpbXBsZS5TaW1wbGVNZXNzYWdlAAAAAAAAAAACAAxMABRkZWZhdWx0X2ZvcmVpZ25fZW51bXQALkxj" + "b20vc3F1YXJldXAvd2lyZS9wcm90b3MvZm9yZWlnbi9Gb3JlaWduRW51bTtMABNkZWZhdWx0X25lc3RlZF9lbnVt" + "dAA6TGNvbS9zcXVhcmV1cC93aXJlL3Byb3Rvcy9zaW1wbGUvU2ltcGxlTWVzc2FnZSROZXN0ZWRFbnVtO0wAF25v" + "X2RlZmF1bHRfZm9yZWlnbl9lbnVtcQB+AAdMAAFvdAASTGphdmEvbGFuZy9TdHJpbmc7TAAVb3B0aW9uYWxfZXh0" + "ZXJuYWxfbXNndAAxTGNvbS9zcXVhcmV1cC93aXJlL3Byb3Rvcy9zaW1wbGUvRXh0ZXJuYWxNZXNzYWdlO0wADm9w" + "dGlvbmFsX2ludDMydAATTGphdmEvbGFuZy9JbnRlZ2VyO0wAE29wdGlvbmFsX25lc3RlZF9tc2d0AD1MY29tL3Nx" + "dWFyZXVwL3dpcmUvcHJvdG9zL3NpbXBsZS9TaW1wbGVNZXNzYWdlJE5lc3RlZE1lc3NhZ2U7TAAFb3RoZXJxAH4A" + "CUwACHBhY2thZ2VfcQB+AAlMAA9yZXBlYXRlZF9kb3VibGV0ABBMamF2YS91dGlsL0xpc3Q7TAAOcmVxdWlyZWRf" + "aW50MzJxAH4AC0wABnJlc3VsdHEAfgAJeHIAGWNvbS5zcXVhcmV1cC53aXJlLk1lc3NhZ2UAAAAAAAAAAAIAAHhw" ); assertThat(deserialize(goldenSerialized)).isEqualTo(goldenValue); assertThat(serialize(goldenValue)).isEqualTo(goldenSerialized); }
private static byte[] readBytes(BufferedReader reader, String endMarker) throws IOException { String line; StringBuffer buf = new StringBuffer(); while ((line = reader.readLine()) != null) { if (line.indexOf(endMarker) != -1) { return ByteString.decodeBase64(buf.toString()).toByteArray(); } buf.append(line.trim()); } throw new IOException("PEM is invalid : No end marker"); } }