Sample code for 30+ languages & platforms
Visual Basic 6.0

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 Visual Basic 6.0 Downloads

Visual Basic 6.0
Dim success As Long
success = 0

Dim jws As New ChilkatJws

'  Load the JWS compact serialization.
Dim jwsCompact As String
jwsCompact = "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>"
success = jws.LoadJws(jwsCompact)
If (success = 0) Then
    Debug.Print jws.LastErrorText
    Exit Sub
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.
Dim pubKey As New PublicKey
success = pubKey.LoadFromFile("qa_data/signer_pubkey.pem")
If (success = 0) Then
    Debug.Print pubKey.LastErrorText
    Exit Sub
End If

success = jws.SetPublicKey(0,pubKey)
If (success = 0) Then
    Debug.Print jws.LastErrorText
    Exit Sub
End If

'  Validate the signature.  Validate returns 1 when valid, 0 when not, or a negative value on error.
Dim v As Long
v = jws.Validate(0)
If (v < 0) Then
    Debug.Print jws.LastErrorText
    Exit Sub
End If

If (v <> 1) Then
    Debug.Print "The signature is not valid."
    Exit Sub
End If

Debug.Print "The signature is valid."