Sample code for 30+ languages & platforms
PureBasic

X509Certificate GetCertHash SHA256

See more Certificates Examples

Shows how to get the SHA256 hash of a certificate.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkBinData.pb"
IncludeFile "CkCert.pb"

Procedure ChilkatExample()

    success.i = 0

    cert.i = CkCert::ckCreate()
    If cert.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; Load a certificate object with a certificate.
    ; The certificate object could have come from any source,
    ; or perhaps was obtained from another Chilkat function.
    ; The source does not matter.  What matters is that we have
    ; a cert object loaded with the certificate..
    success = CkCert::ckLoadFromFile(cert,"qa_data/certs/cert_test123.cer")
    If success <> 1
        Debug CkCert::ckLastErrorText(cert)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ; Get the binary DER of the certificae, and compute the SHA256 hash:
    bdDer.i = CkBinData::ckCreate()
    If bdDer.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkCert::ckExportCertDerBd(cert,bdDer)

    ; Get the base64 representation of the SHA256 hash.
    certHash.s = CkBinData::ckGetHash(bdDer,"sha256","base64")

    Debug "Certificate hash in base64 = " + certHash


    CkCert::ckDispose(cert)
    CkBinData::ckDispose(bdDer)


    ProcedureReturn
EndProcedure