Lua
Lua
DSA R,S Signature Values
See more DSA Examples
Creates a DSA signature. Gets r,s values from the signature. Re-creates the DSA signature ASN.1 from the r,s values. Then verifies the signature using the re-created ASN.1 DSA signature.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local crypt = chilkat.Crypt2.new()
crypt.encoding_mode = "hex"
crypt.hash_algorithm = "sha-1"
local hashStr = crypt:hash_file_enc("qa_data/hamlet.xml")
print("hash to sign: " .. hashStr)
local dsa = chilkat.Dsa.new()
local pemPrivateKey = dsa:load_text("qa_data/dsa/dsaPrivKey2.pem")
local success = dsa:from_pem(pemPrivateKey)
if not success then
print(dsa.last_error_text)
return
end
-- Load the hash to be signed into the DSA object:
success = dsa:set_encoded_hash("hex", hashStr)
if not success then
print(dsa.last_error_text)
return
end
-- Sign the hash.
success = dsa:sign_hash()
if not success then
print(dsa.last_error_text)
return
end
-- Get the ASN.1 signature.
local asnSig = dsa:get_encoded_signature("base64")
print("Signature: " .. asnSig)
-- Examine the details of the ASN.1 signature.
-- We want to get the r,s values as hex strings..
local asn = chilkat.Asn.new()
success = asn:load_encoded(asnSig, "base64")
if not success then
print(asn.last_error_text)
return
end
-- Get the ASN.1 as XML.
local xml = chilkat.Xml.new()
success = xml:load_xml(asn:asn_to_xml())
print("Signature as XML: ")
print(xml:get_xml())
-- Sample XML shown here.
-- The r and s values are the two hex strings in the XML.
-- <?xml version="1.0" encoding="utf-8"?>
-- <sequence>
-- <int>2C187F3AB6E47A66497B86CE97BB39E2133810F5</int>
-- <int>588E53D3F7B69636B48FD7175E99A3961BD7D775</int>
-- </sequence>
-- Pretend we're starting with r,s
local r = "2C187F3AB6E47A66497B86CE97BB39E2133810F5"
local s = "588E53D3F7B69636B48FD7175E99A3961BD7D775"
-- Build the XML that will be converted to ASN.1
xml:clear()
xml.tag = "sequence"
xml:new_child2("int", r)
xml:new_child2("int", s)
-- Convert the XML to ASN.1
success = asn:load_asn_xml(xml:get_xml())
-- Emit the signature as DER encoded ASN.1 (base64)
asnSig = asn:get_encoded_der("base64")
-- --------------------------------------------------------------------
-- Verify the signature using the asnSig we built from the r,s values
-- --------------------------------------------------------------------
local dsa2 = chilkat.Dsa.new()
-- Load the DSA public key to be used for verification:
local pemPublicKey = dsa2:load_text("qa_data/dsa/dsaPubKey2.pem")
success = dsa2:from_public_pem(pemPublicKey)
if not success then
print(dsa2.last_error_text)
return
end
-- Load the hash to be verified.
success = dsa2:set_encoded_hash("hex", hashStr)
if not success then
print(dsa2.last_error_text)
return
end
-- Load the ASN.1 signature:
success = dsa2:set_encoded_signature("base64", asnSig)
if not success then
print(dsa2.last_error_text)
return
end
-- Verify:
success = dsa2:verify()
if not success then
print(dsa2.last_error_text)
else
print("DSA Signature Verified!")
end