Lua
Lua
Sign a JWS with a Certificate
See more JSON Web Signatures (JWS) Examples
Demonstrates Jws.SetSigningCert, which sets the certificate whose associated private key is used to create a signature.
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 certificate must have an accessible private key. This is convenient when the signing identity is held as a certificate (for example loaded from a PFX).
Chilkat Lua Downloads
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.
-- The PFX password should come from a secure source rather than being hard-coded.
local pfxPassword = "myPfxPassword"
-- Load a certificate that has an associated private key, then set it as the signing certificate for
-- signature 0. The certificate's private key is used to create the signature.
local cert = chilkat.Cert.new()
success = cert:load_pfx_file("qa_data/signer.pfx", pfxPassword)
if not success then
print(cert.last_error_text)
return
end
success = jws:set_signing_cert(0, cert)
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)