Sample code for 30+ languages & platforms
PureBasic

RSA Sign utf-8 Byte Representation of String

See more RSA Examples

Demontstrates how to sign the utf-8 byte representation of a string.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkPrivateKey.pb"
IncludeFile "CkRsa.pb"

Procedure ChilkatExample()

    success.i = 0

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

    ; Load an RSA private key for signing.
    privKey.i = CkPrivateKey::ckCreate()
    If privKey.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkPrivateKey::ckLoadEncryptedPemFile(privKey,"qa_data/pem/rsa_passwd.pem","passwd")
    If success = 0
        Debug CkPrivateKey::ckLastErrorText(privKey)
        CkPrivateKey::ckDispose(privKey)
        ProcedureReturn
    EndIf

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

    success = CkRsa::ckUsePrivateKey(rsa,privKey)
    If success = 0
        Debug CkRsa::ckLastErrorText(rsa)
        CkPrivateKey::ckDispose(privKey)
        CkRsa::ckDispose(rsa)
        ProcedureReturn
    EndIf

    originalData.s = "This is the string to be hashed an RSA signed."

    ; Indicate that we want the utf-8 byte representation of the string to be signed
    CkRsa::setCkCharset(rsa, "utf-8")

    ; We want the RSA signature in base64 format
    CkRsa::setCkEncodingMode(rsa, "base64")

    sigBase64.s = CkRsa::ckSignStringENC(rsa,originalData,"sha256")
    Debug sigBase64


    CkPrivateKey::ckDispose(privKey)
    CkRsa::ckDispose(rsa)


    ProcedureReturn
EndProcedure