Get every algorithm MessageDigest can use?

2019-05-13 23:26发布

问题:

MessageDigest takes a string for what algorithm to use when you're about to hash. I've used some of the code here to accumulate a rather large list of 220 "algorithms" on my android device. I iterated through all of them using them as an algorithm in MessageDigest and very few of them actually produce a hash.

How can I, at runtime, determine what hashes MessageDigest will produce an output for without trying every one?

回答1:

List the Services of each provider, then call the getType() method. If it is MessageDigest, then this is a hash algorithm. Something like:

Provider[] providers = Security.getProviders();
for (Provider p : providers) {
  String providerStr = String.format("%s/%s/%f\n", p.getName(),
                    p.getInfo(), p.getVersion());
  Set<Service> services = p.getServices();
  for (Service s : services) {
    if ("MessageDigest".equals(s.getType())) {
       System.out.printf("\t%s/%s/%s", s.getType(),
                            s.getAlgorithm(), s.getClassName());
    }
  }
}


标签: android hash