Sample code for 30+ languages & platforms
Lua Requires Chilkat v11.0.0+

Verfies an RSA Signature

See more Apple Keychain Examples

Verifies an RSA signature against the original data.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- The following data was signed by the following example:
-- RSA Sign using a Private Key on a USB Token or Smartcard
local bd = chilkat.BinData.new()
for i = 0, 100 do
    bd:append_encoded("000102030405060708090A0B0C0D0E0F", "hex")
end

-- Load the signature
local bdSig = chilkat.BinData.new()
local success = bdSig:load_file("rsaSignatures/test1.sig")
if not success then
    print("Failed to load the RSA signature")
    return
end

-- Get the public key to be used for signature verification.
local pubKey = chilkat.PublicKey.new()
success = pubKey:load_from_file("rsaKeys/chilkat-rsa-2048.pem")
if not success then
    print(pubKey.last_error_text)
    return
end

local rsa = chilkat.Rsa.new()
success = rsa:use_public_key(pubKey)
if not success then
    print(rsa.last_error_text)
    return
end

-- Verify the hash of the data against the signature.
-- We pass in the original data.  Internally, the hash is generated
-- and used to validate the signature.
-- Validating the RSA signature means two things:
-- (1) the original data is exactly what was signed, and
-- (2) it was signed by the owner of the RSA private key.
success = rsa:verify_bd(bd, "sha256", bdSig)

if not success then
    print(rsa.last_error_text)
    print("Signature invalid.")
else
    print("Signature valid.")
end