Sample code for 30+ languages & platforms
PowerBuilder

Sign a JWS with a Certificate

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetSigningCert, which sets the certificate whose associated private key is used to create a signature.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. The certificate must have an accessible private key. This is convenient when the signing identity is held as a certificate (for example loaded from a PFX).

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jws
oleobject loo_Header
integer li_BIncludeBom
string ls_PfxPassword
oleobject loo_Cert
string ls_JwsCompact

li_Success = 0

loo_Jws = create oleobject
li_rc = loo_Jws.ConnectToNewObject("Chilkat.Jws")
if li_rc < 0 then
    destroy loo_Jws
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Protected header specifying RSASSA-PKCS1-v1_5 with SHA-256 (RS256).
loo_Header = create oleobject
li_rc = loo_Header.ConnectToNewObject("Chilkat.JsonObject")

loo_Header.UpdateString("alg","RS256")
li_Success = loo_Jws.SetProtectedHeader(0,loo_Header)
if li_Success = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    return
end if

li_BIncludeBom = 0
li_Success = loo_Jws.SetPayload("This is the content to sign.","utf-8",li_BIncludeBom)
if li_Success = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    return
end if

//  The file path is relative to the application's current working directory.  An absolute path
//  may also be used.  Supply the path appropriate to your own environment.
//  The PFX password should come from a secure source rather than being hard-coded.
ls_PfxPassword = "myPfxPassword"

//  Load a certificate that has an associated private key, then set it as the signing certificate for
//  signature 0.  The certificate's private key is used to create the signature.
loo_Cert = create oleobject
li_rc = loo_Cert.ConnectToNewObject("Chilkat.Cert")

li_Success = loo_Cert.LoadPfxFile("qa_data/signer.pfx",ls_PfxPassword)
if li_Success = 0 then
    Write-Debug loo_Cert.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    destroy loo_Cert
    return
end if

li_Success = loo_Jws.SetSigningCert(0,loo_Cert)
if li_Success = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    destroy loo_Cert
    return
end if

ls_JwsCompact = loo_Jws.CreateJws()
if loo_Jws.LastMethodSuccess = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    destroy loo_Cert
    return
end if

Write-Debug ls_JwsCompact


destroy loo_Jws
destroy loo_Header
destroy loo_Cert