Sample code for 30+ languages & platforms
PowerBuilder

Sign a JWS with a Private Key

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetPrivateKey, which sets the private key used to create a signature. The example uses RS256 (RSASSA-PKCS1-v1_5 with SHA-256).

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. Public-key JWS signs with a private key so that anyone holding the corresponding public key can verify the signature.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jws
oleobject loo_Header
integer li_BIncludeBom
oleobject loo_PrivKey
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.
//  Load the signer's private key and set it for signature 0.
loo_PrivKey = create oleobject
li_rc = loo_PrivKey.ConnectToNewObject("Chilkat.PrivateKey")

li_Success = loo_PrivKey.LoadPemFile("qa_data/signer_privkey.pem")
if li_Success = 0 then
    Write-Debug loo_PrivKey.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    destroy loo_PrivKey
    return
end if

li_Success = loo_Jws.SetPrivateKey(0,loo_PrivKey)
if li_Success = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_Header
    destroy loo_PrivKey
    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_PrivKey
    return
end if

Write-Debug ls_JwsCompact


destroy loo_Jws
destroy loo_Header
destroy loo_PrivKey