Sample code for 30+ languages & platforms
Lua

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

Lua
local chilkat = require("chilkat")

local jws = chilkat.Jws.new()

-- Protected header specifying RSASSA-PKCS1-v1_5 with SHA-256 (RS256).
local header = chilkat.JsonObject.new()
header:update_string("alg", "RS256")
local success = jws:set_protected_header(0, header)
if not success then
    print(jws.last_error_text)
    return
end

local bIncludeBom = false
success = jws:set_payload("This is the content to sign.", "utf-8", bIncludeBom)
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 private key and set it for signature 0.
local privKey = chilkat.PrivateKey.new()
success = privKey:load_pem_file("qa_data/signer_privkey.pem")
if not success then
    print(privKey.last_error_text)
    return
end

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

local jwsCompact = jws:create_jws()
if not jws.last_method_success then
    print(jws.last_error_text)
    return
end

print(jwsCompact)