Sample code for 30+ languages & platforms
Lua

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 Lua Downloads

Lua
local chilkat = require("chilkat")

local jws = chilkat.Jws.new()

-- Load the JWS compact serialization.
local jwsCompact = "eyJhbGciOiJIUzI1NiJ9.VGhpcyBpcyB0aGUgY29udGVudCB0byBzaWduLg.<signature>"
local success = jws:load_jws(jwsCompact)
if not success then
    print(jws.last_error_text)
    return
end

-- 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.
local pubKey = chilkat.PublicKey.new()
success = pubKey:load_from_file("qa_data/signer_pubkey.pem")
if not success then
    print(pubKey.last_error_text)
    return
end

success = jws:set_public_key(0, pubKey)
if not success then
    print(jws.last_error_text)
    return
end

-- Validate the signature.  Validate returns 1 when valid, 0 when not, or a negative value on error.
local v = jws:validate(0)
if v < 0 then
    print(jws.last_error_text)
    return
end

if v ~= 1 then
    print("The signature is not valid.")
    return
end

print("The signature is valid.")