Sample code for 30+ languages & platforms
PowerBuilder

Validate a JWS with a Public Key

See more JSON Web Signatures (JWS) Examples

Demonstrates Jws.SetPublicKey, which sets the public key used to validate a signature of a loaded JWS.

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 public key verifies a signature created with the corresponding private key. Validate returns 1 when valid, 0 when not, or a negative value on error.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Jws
string ls_JwsCompact
oleobject loo_PubKey
integer v

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

//  Load the JWS compact serialization.
ls_JwsCompact = "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>"
li_Success = loo_Jws.LoadJws(ls_JwsCompact)
if li_Success = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    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 public key and set it for validating signature 0.
loo_PubKey = create oleobject
li_rc = loo_PubKey.ConnectToNewObject("Chilkat.PublicKey")

li_Success = loo_PubKey.LoadFromFile("qa_data/signer_pubkey.pem")
if li_Success = 0 then
    Write-Debug loo_PubKey.LastErrorText
    destroy loo_Jws
    destroy loo_PubKey
    return
end if

li_Success = loo_Jws.SetPublicKey(0,loo_PubKey)
if li_Success = 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_PubKey
    return
end if

//  Validate the signature.  Validate returns 1 when valid, 0 when not, or a negative value on error.
v = loo_Jws.Validate(0)
if v < 0 then
    Write-Debug loo_Jws.LastErrorText
    destroy loo_Jws
    destroy loo_PubKey
    return
end if

if v <> 1 then
    Write-Debug "The signature is not valid."
    destroy loo_Jws
    destroy loo_PubKey
    return
end if

Write-Debug "The signature is valid."


destroy loo_Jws
destroy loo_PubKey