Lua Requires Chilkat v11.0.0+
Lua
Verify the RSA Signature of a SHA256 Hash
See more RSA Examples
Demonstrates how to verify an RSA signature of a SHA256 hash.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- Let's say you have a file containing the 32-bytes of a SHA256 hash,
-- and a file that is an RSA signature of those 32 bytes.
-- Here's how you verify using the RSA public key found in a PEM.
local pubKey = chilkat.PublicKey.new()
local success = pubKey:load_from_file("rsaPubKey.pem")
if not success then
print(pubKey.last_error_text)
return
end
local rsa = chilkat.Rsa.new()
-- Get the public key.
success = rsa:use_public_key(pubKey)
if not success then
print(rsa.last_error_text)
return
end
-- Get the 32-byte SHA256 hash.
local bdHash = chilkat.BinData.new()
success = bdHash:load_file("myHash.sha256")
if not success then
print("Failed to load SHA256 hash.")
return
end
-- Get the RSA signature to be validated.
local bdSig = chilkat.BinData.new()
success = bdSig:load_file("mySig.sig")
if not success then
print("Failed to load RSA signature.")
return
end
-- Verify the signature against the SHA256 hash.
local enc = "base64"
rsa.encoding_mode = enc
success = rsa:verify_hash_enc(bdHash:get_encoded(enc), "sha256", bdSig:get_encoded(enc))
if not success then
print(rsa.last_error_text)
return
end
print("Signature validated.")