/** * Converts the given byte array to a String in HEX representation. * * @param bytes the bytes to convert. * @return a String in HEX format. */ public static String encodeToHex(byte[] bytes) { return encodeToHex(bytes, false, false); }
/** * Converts the given byte array to a String in HEX representation. * * @param bytes the bytes to convert. * @return a String in HEX format. */ public static String encodeToHex(byte[] bytes) { return encodeToHex(bytes, false, false); }
/** * Deprecated. * * @deprecated This method appends an unnecessary colon to the end of the * HEX String, please use * {@link HexUtilities#encodeToHex(byte[])} instead, which does * not do this. */ @Deprecated public static StringBuilder toHex(byte[] bytes, boolean uppercase) { return new StringBuilder(HexUtilities.encodeToHex(bytes, uppercase, true) + ":"); }
/** * Deprecated. * * @deprecated This method appends an unnecessary colon to the end of the * HEX String, please use * {@link HexUtilities#encodeToHex(byte[])} instead, which does * not do this. */ @Deprecated public static StringBuilder toHex(byte[] bytes, boolean uppercase) { return new StringBuilder(HexUtilities.encodeToHex(bytes, uppercase, true) + ":"); }
/** * Deprecated. * * @deprecated This method appends an unnecessary colon to the end of the * HEX String if colonSeparator is set to true, please use * {@link HexUtilities#encodeToHex(byte[], boolean, boolean)} * instead, which does not do this. */ @Deprecated public static StringBuilder toHex(byte[] bytes, boolean uppercase, boolean colonSeparator) { return new StringBuilder( HexUtilities.encodeToHex(bytes, uppercase, colonSeparator) + (colonSeparator ? ":" : "")); } }
/** * Deprecated. * * @deprecated This method appends an unnecessary colon to the end of the * HEX String if colonSeparator is set to true, please use * {@link HexUtilities#encodeToHex(byte[], boolean, boolean)} * instead, which does not do this. */ @Deprecated public static StringBuilder toHex(byte[] bytes, boolean uppercase, boolean colonSeparator) { return new StringBuilder( HexUtilities.encodeToHex(bytes, uppercase, colonSeparator) + (colonSeparator ? ":" : "")); } }
@Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { final X509Certificate leafCertificate = chain[0]; if (isPinned(leafCertificate)) { return; } // Throw a CertificateException with a meaningful message. Note that we // use CERTPLAIN, which tends to be long, so colons as separator are of // no use and most other software UIs show the "public key" without // colons (and using lowercase letters). final String pinHexString = HexUtilities.encodeToHex(leafCertificate.getEncoded()); throw new CertificateNotPinnedException("Certificate not pinned. Use 'CERTPLAIN:" + pinHexString + "' to pin this certificate. But only pin the certificate if you are sure this is the correct certificate!"); }
@Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { final X509Certificate leafCertificate = chain[0]; if (isPinned(leafCertificate)) { return; } // Throw a CertificateException with a meaningful message. Note that we // use CERTPLAIN, which tends to be long, so colons as separator are of // no use and most other software UIs show the "public key" without // colons (and using lowercase letters). final String pinHexString = HexUtilities.encodeToHex(leafCertificate.getEncoded()); throw new CertificateNotPinnedException("Certificate not pinned. Use 'CERTPLAIN:" + pinHexString + "' to pin this certificate. But only pin the certificate if you are sure this is the correct certificate!"); }
@Test public void encodeToHexUppercase_anyByteArray_returnsCorrectHexString() { assertEquals("4B6E6162", HexUtilities.encodeToHex(new byte[] { 75, 110, 97, 98 }, true, false)); }
@Test public void encodeToHex_emptyArray_returnsEmptyString() { assertEquals("", HexUtilities.encodeToHex(new byte[] {})); } }
@Test public void encodeToHex_anyByteArray_returnsCorrectHexString() { assertEquals("4b6e6162", HexUtilities.encodeToHex(new byte[] { 75, 110, 97, 98 })); }
@Test public void pinWithWhitespaces() { // Let's shoot some holes into the pin string final int[] subsequences = new int[] { 4, 17, 21, 32 }; StringBuilder sb = new StringBuilder(); int start = 0; for (int i : subsequences) { sb.append(PIN_STRING_W_COLON.subSequence(start, i)); sb.append(' '); start = i; } sb.append(PIN_STRING_W_COLON.subSequence(start, PIN_STRING_W_COLON.length())); Pin pin = Pin.fromString("CERTSHA256:" + sb.toString()); byte[] pinBytes = pin.getPinBytes(); final String pinString = HexUtilities.encodeToHex(pinBytes, true, true); // String.format() appends a ':' at the very end, so we have to do that too assertEquals(PIN_STRING_W_COLON, pinString); }
@Test public void encodeToHexSemicolons_anyByteArray_returnsCorrectHexString() { assertEquals("4b:6e:61:62", HexUtilities.encodeToHex(new byte[] { 75, 110, 97, 98 }, false, true)); }
@Test public void pinWithFullwidthColon() { Pin pin = Pin.fromString("CERTSHA256:" + PIN_STRING_W_COLON); byte[] pinBytes = pin.getPinBytes(); final String pinString = HexUtilities.encodeToHex(pinBytes, true, true); // String.format() appends a ':' at the very end, so we have to do that too assertEquals(PIN_STRING_W_COLON, pinString); }