Sample code for 30+ languages & platforms
PureBasic

DKIM Signature using Windows Current User Certificate Store

See more DKIM / DomainKey Examples

This is a Windows-specific example to load a certificate from the Current User (registry-based) certificate store, and then use the certificate's associated private key for a DKIM signature.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkCert.pb"
IncludeFile "CkDkim.pb"
IncludeFile "CkPrivateKey.pb"

Procedure ChilkatExample()

    success.i = 0

    ; This example requires the Chilkat API to have been previously unlocked.
    ; See Global Unlock Sample for sample code.

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

    ; The LoadByCommonName method searches the Windows Local Machine and Current User 
    ; registry-based certificate stores for a certificate having the common name specified. 
    ; If found, the certificate is loaded and ready for use.
    success = CkCert::ckLoadByCommonName(cert,"My Certificate ABC")
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

    ; The certificate must have an associated private key installed, and it must be a
    ; private key that has been marked "exportable" when it was originally installed.
    If Not CkCert::ckHasPrivateKey(cert)
        Debug "This certificate does not have a private key available."
        CkCert::ckDispose(cert)
        ProcedureReturn
    EndIf

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

    success = CkCert::ckGetPrivateKey(cert,privKey)
    If success = 0
        Debug CkCert::ckLastErrorText(cert)
        CkCert::ckDispose(cert)
        CkPrivateKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

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

    ; Load the private key into the DKIM object:
    success = CkDkim::ckSetDkimPrivateKey(dkim,privKey)
    If success = 0
        Debug CkDkim::ckLastErrorText(dkim)
        CkCert::ckDispose(cert)
        CkPrivateKey::ckDispose(privKey)
        CkDkim::ckDispose(dkim)
        ProcedureReturn
    EndIf

    ; The private key has been loaded into the DKIM object.  See the other DKIM
    ; examples for guidance on how to create a DKIM signature...


    CkCert::ckDispose(cert)
    CkPrivateKey::ckDispose(privKey)
    CkDkim::ckDispose(dkim)


    ProcedureReturn
EndProcedure