Created
March 3, 2017 22:50
-
-
Save patrickfav/d3ea9bfcb1669389f0bb52240bace1f3 to your computer and use it in GitHub Desktop.
Read out all Security Provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MessageDigest digest = null; | |
try { | |
digest = MessageDigest.getInstance("SHA-256"); | |
byte[] hash = digest.digest("hallo".getBytes(Charset.forName("UTF-8"))); | |
} catch (NoSuchAlgorithmException e) { | |
e.printStackTrace(); | |
} | |
Provider[] providers = Security.getProviders(); | |
for (Provider provider : providers) { | |
System.out.println("= " + provider.getName() + " ="); | |
System.out.println("\n\n== MESSAGE DIGEST =="); | |
showHashAlgorithms(provider, MessageDigest.class); | |
System.out.println("\n\n== CIPHER =="); | |
showHashAlgorithms(provider, Cipher.class); | |
System.out.println("\n\n== MAC =="); | |
showHashAlgorithms(provider, Mac.class); | |
System.out.println("\n\n== KEY AGREEMENT =="); | |
showHashAlgorithms(provider, KeyAgreement.class); | |
System.out.println("\n\n== SIGNATURE =="); | |
showHashAlgorithms(provider, Signature.class); | |
System.out.println("\n\n== SECRET KEY FACTORY =="); | |
showHashAlgorithms(provider, SecretKeyFactory.class); | |
System.out.println("\n\n== KEY PAIR GEN =="); | |
showHashAlgorithms(provider, KeyPairGenerator.class); | |
} | |
} | |
private static final void showHashAlgorithms(Provider prov, Class<?> typeClass) { | |
String type = typeClass.getSimpleName(); | |
List<Provider.Service> algos = new ArrayList<>(); | |
Set<Provider.Service> services = prov.getServices(); | |
for (Provider.Service service : services) { | |
if (service.getType().equalsIgnoreCase(type)) { | |
algos.add(service); | |
} | |
} | |
if (!algos.isEmpty()) { | |
System.out.printf(" --- Provider %s, version %.2f --- %n", prov.getName(), prov.getVersion()); | |
for (Provider.Service service : algos) { | |
String algo = service.getAlgorithm(); | |
System.out.printf("Algorithm name: \"%s\"%n", algo); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment