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

Aadhaar Paperless Offline e-kyc

See more XML Digital Signatures Examples

Opens an encrypted .zip containing Aadhaar Paperless Offline e-KYC XML. Gets the XML and validates the digital signature. Then computes the hash for the mobile number and Email ID.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

-- Open the .zip containing the Aadhaar Paperless Offline e-KYC XML.
-- The .zip is encrypted using the "Share Phrase".
local zip = chilkat.Zip.new()
local success = zip:open_zip("qa_data/xml_dsig/offline_paperless_kyc.zip")
if not success then
    print(zip.last_error_text)
    return
end

-- The .zip should contain 1 XML file.
local entry = chilkat.ZipEntry.new()
success = zip:entry_at(0, entry)
if not success then
    print(zip.last_error_text)
    return
end

-- To get the contents, we need to specify the Share Phrase.
local sharePhrase = "Lock@487"
zip.decrypt_password = sharePhrase

local bdXml = chilkat.BinData.new()
-- The XML file will be unzipped into the bdXml object.
success = entry:unzip_to_bd(bdXml)
if not success then
    print(entry.last_error_text)
    return
end

-- First verify the XML digital signature.
local dsig = chilkat.XmlDSig.new()
success = dsig:load_signature_bd(bdXml)
if not success then
    print(dsig.last_error_text)
    return
end

-- The UIDAI XML signature does not contain the KeyInfo, so we must load the uidai certificate
-- and indicate that its public key is to be used for verifying the signature.
local cert = chilkat.Cert.new()
success = cert:load_from_file("qa_data/xml_dsig/uidai_auth_sign_prod_2023.cer")
if not success then
    print(cert.last_error_text)
    return
end

-- Get the certificate's public key.
local pubKey = chilkat.PublicKey.new()
cert:get_public_key(pubKey)

dsig:set_public_key(pubKey)

-- The XML in this example contains only 1 signature.
local bVerifyReferenceDigests = true
local bVerified = dsig:verify_signature(bVerifyReferenceDigests)
if not bVerified then
    print(dsig.last_error_text)
    print("The signature was not valid.")
    return
end

print("The XML digital signature is valid.")

-- Let's compute the hash for the Mobile Number.

--     Hashing logic for Mobile Number :
--     Sha256(Sha256(Mobile+SharePhrase))*number of times last digit of Aadhaar number
--     (Ref ID field contains last 4 digits).
--
--     Example :
--     Mobile: 1234567890
--     Aadhaar Number:XXXX XXXX 3632
--     Passcode : Lock@487
--     Hash: Sha256(Sha256(1234567890Lock@487))*2
--     In case of Aadhaar number ends with Zero we will hashed one time.

local crypt = chilkat.Crypt2.new()
crypt.hash_algorithm = "sha256"
crypt.encoding_mode = "hexlower"

local strToHash = "1234567890Lock@487"
local bdHash = chilkat.BinData.new()
success = bdHash:append_string(strToHash, "utf-8")

-- Hash a number of times equal to the last digit of your Aadhaar number.
-- If the Aadhaar number ends with 0, then hash one time.
-- For this example, we'll just set the number of times to hash
-- for the case where an Aadhaar number ends in "9"
local numTimesToHash = 9
for i = 1, numTimesToHash do
    local tmpStr = crypt:hash_bd_enc(bdHash)
    bdHash:clear()
    bdHash:append_string(tmpStr, "utf-8")
end

print("Computed Mobile hash = " .. bdHash:get_string("utf-8"))

-- Let's get the mobile hash stored in the XML and compare it with our computed hash.
local xml = chilkat.Xml.new()
success = xml:load_bd(bdXml, true)
local m_hash = xml:chilkat_path("UidData|Poi|(m)")

print("Stored Mobile hash   = " .. m_hash)

-- Now do the same thing for the email hash:

strToHash = "abc@gm.comLock@487"
bdHash:clear()
success = bdHash:append_string(strToHash, "utf-8")

for i = 1, numTimesToHash do
    local tmpStr = crypt:hash_bd_enc(bdHash)
    bdHash:clear()
    bdHash:append_string(tmpStr, "utf-8")
end

print("Computed Email hash = " .. bdHash:get_string("utf-8"))

local e_hash = xml:chilkat_path("UidData|Poi|(e)")
print("Stored Email hash   = " .. e_hash)