protected Pin(String pinHexString) { pinBytes = HexUtilities.decodeFromHex(pinHexString); }
/** * 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); }
protected Pin(String pinHexString) { pinBytes = HexUtilities.decodeFromHex(pinHexString); }
/** * 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) + ":"); }
public static X509Certificate decodeX509Certificate(String hexString) { return decodeX509Certificate(HexUtilities.decodeFromHex(hexString)); }
/** * 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) + ":"); }
public static PublicKey decodeX509PublicKey(String hexString) { return decodeX509PublicKey(HexUtilities.decodeFromHex(hexString)); }
/** * 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 ? ":" : "")); } }
public static X509Certificate decodeX509Certificate(String hexString) { return decodeX509Certificate(HexUtilities.decodeFromHex(hexString)); }
/** * 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 ? ":" : "")); } }
public static PublicKey decodeX509PublicKey(String hexString) { return decodeX509PublicKey(HexUtilities.decodeFromHex(hexString)); }
@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(expected = IllegalArgumentException.class) public void decodeFromHex_divisibleByTwoWithInvalidCharactersString_throwsException() { HexUtilities.decodeFromHex("This is not a valid hex String"); }
@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(expected = IllegalArgumentException.class) public void decodeFromHex_notDivisibleByTwoHexString_throwsException() { HexUtilities.decodeFromHex("4b6e616"); }
@Test public void encodeToHexUppercase_anyByteArray_returnsCorrectHexString() { assertEquals("4B6E6162", HexUtilities.encodeToHex(new byte[] { 75, 110, 97, 98 }, true, false)); }
@Test public void decodeFromHex_lowercaseHexString_returnsCorrectByteArray() { assertArrayEquals(new byte[] { 75, 110, 97, 98 }, HexUtilities.decodeFromHex("4b6e6162")); }
@Test public void encodeToHex_emptyArray_returnsEmptyString() { assertEquals("", HexUtilities.encodeToHex(new byte[] {})); } }
@Test public void decodeFromHex_allCaseHexStringWithWhitespacesAndSemicolons_returnsCorrectByteArray() { assertArrayEquals(new byte[] { 75, 110, 97, 98 }, HexUtilities.decodeFromHex("4B 6e 61:62")); }