< prev index next >

src/java.base/share/classes/sun/security/x509/X509CertImpl.java

Print this page

1959     }
1960 
1961     private ConcurrentHashMap<String,String> fingerprints =
1962             new ConcurrentHashMap<>(2);
1963 
1964     public String getFingerprint(String algorithm) {
1965         return fingerprints.computeIfAbsent(algorithm,
1966             x -> getFingerprint(x, this));
1967     }
1968 
1969     /**
1970      * Gets the requested finger print of the certificate. The result
1971      * only contains 0-9 and A-F. No small case, no colon.
1972      */
1973     public static String getFingerprint(String algorithm,
1974             X509Certificate cert) {
1975         try {
1976             byte[] encCertInfo = cert.getEncoded();
1977             MessageDigest md = MessageDigest.getInstance(algorithm);
1978             byte[] digest = md.digest(encCertInfo);
1979             StringBuilder sb = new StringBuilder(digest.length * 2);
1980             for (int i = 0; i < digest.length; i++) {
1981                 byte2hex(digest[i], sb);
1982             }
1983             return sb.toString();
1984         } catch (NoSuchAlgorithmException | CertificateEncodingException e) {
1985             // ignored
1986         }
1987         return "";
1988     }
1989 
1990     /**
1991      * Converts a byte to hex digit and writes to the supplied builder
1992      */
1993     private static void byte2hex(byte b, StringBuilder buf) {
1994         char[] hexChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
1995                 '9', 'A', 'B', 'C', 'D', 'E', 'F' };
1996         int high = ((b & 0xf0) >> 4);
1997         int low = (b & 0x0f);
1998         buf.append(hexChars[high])
1999             .append(hexChars[low]);
2000     }
2001 }

1959     }
1960 
1961     private ConcurrentHashMap<String,String> fingerprints =
1962             new ConcurrentHashMap<>(2);
1963 
1964     public String getFingerprint(String algorithm) {
1965         return fingerprints.computeIfAbsent(algorithm,
1966             x -> getFingerprint(x, this));
1967     }
1968 
1969     /**
1970      * Gets the requested finger print of the certificate. The result
1971      * only contains 0-9 and A-F. No small case, no colon.
1972      */
1973     public static String getFingerprint(String algorithm,
1974             X509Certificate cert) {
1975         try {
1976             byte[] encCertInfo = cert.getEncoded();
1977             MessageDigest md = MessageDigest.getInstance(algorithm);
1978             byte[] digest = md.digest(encCertInfo);
1979             return Hex.encoder().encode(digest);




1980         } catch (NoSuchAlgorithmException | CertificateEncodingException e) {
1981             // ignored
1982         }
1983         return "";
1984     }












1985 }
< prev index next >