Friday, August 23, 2013

Display JKS content in Python (WLST)



from java.security import KeyStore
from java.security import MessageDigest
from java.io import FileInputStream


def hexify(bytes):
    hexDigits = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
    buf = StringBuffer()
    for i in range(len(bytes)):
        buf.append(hexDigits[(bytes[i] & 0xf0) >> 4])
        buf.append(hexDigits[bytes[i] & 0x0f])
        if i < len(bytes) - 1:
            buf.append(':')
    return buf.toString()


filestore='myacme.jks'
password='111111'
ks = KeyStore.getInstance("JKS")
ks.load(FileInputStream(filestore), password)
md = MessageDigest.getInstance("MD5")

for item in ks.aliases():
 print "alias", item, "isCertificate", ks.isCertificateEntry(item)
 if ks.isCertificateEntry(item):
  print "isCertificate"
  cert = ks.getCertificate(item)
  der = cert.getEncoded()
  md.update(der)
  print hexify(md.digest())



Inspired by http://stackoverflow.com/questions/1270703/how-to-retrieve-compute-an-x509-certificates-thumbprint-in-java

Javadoc: MessageDigest , KeyStore, Certificate, Key

No comments: