-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetCertificateHash.java
More file actions
64 lines (54 loc) · 2.6 KB
/
GetCertificateHash.java
File metadata and controls
64 lines (54 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.Base64;
import javax.net.ssl.HttpsURLConnection;
public class GetCertificateHash {
public static void main(String[] args) {
String[] domains = {
"ais.osym.gov.tr",
"www.osym.gov.tr",
"cdn.jsdelivr.net",
"fonts.googleapis.com",
"fonts.gstatic.com"
};
for (String domain : domains) {
try {
System.out.println("=== Certificate for " + domain + " ===");
URL url = new URL("https://" + domain);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
// Get the certificate
X509Certificate[] certs = (X509Certificate[]) connection.getServerCertificates();
if (certs.length > 0) {
X509Certificate cert = certs[0];
// Calculate SHA-256 hash
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
byte[] certHash = sha256.digest(cert.getEncoded());
// Convert to Base64
String base64Hash = Base64.getEncoder().encodeToString(certHash);
System.out.println("SHA-256 Pin: " + base64Hash);
System.out.println("Subject: " + cert.getSubjectX500Principal());
System.out.println("Issuer: " + cert.getIssuerX500Principal());
System.out.println("Valid From: " + cert.getNotBefore());
System.out.println("Valid To: " + cert.getNotAfter());
} else {
System.out.println("No certificates found");
}
connection.disconnect();
System.out.println();
} catch (MalformedURLException | NoSuchAlgorithmException | CertificateEncodingException e) {
System.out.println("Error: " + e.getMessage());
System.out.println();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
System.out.println();
}
}
}
}