Azure Key Vault: Sign a PDF with SHA-256
See more Azure Key Vault Examples
Requires Chilkat 11.6.0 or later. Keep qa_data/azure/keyvault.json and token/PFX files private, outside web content and source control, with access restricted to the application identity. Use placeholders replaced by your configuration, never commit credentials. Create output directories before running file-writing examples. These examples target Azure public-cloud vaults, not Managed HSM or sovereign-cloud endpoints.
Uses Chilkat's cloud convenience integration with client_id, client_secret, tenant_id (directory GUID), vault_name, and cert_name in private configuration. The installed Chilkat library manages authentication and service API versions for this helper; this example does not select a REST api-version. Use the explicit Http examples when that control is required. Keep the runtime current and inspect diagnostics privately.
Set explicit cert_version and load its matching public certificate from qa_data/certs/myCert.cer. Keep the version pinned rather than implicitly following certificate rotation. The backing key must permit signing, and the application needs certificates/get and keys/sign. The private key remains in Key Vault; the document is assembled locally and its digest is signed remotely. A cryptographically valid signature does not establish certificate trust, revocation status, or a trusted signing time.
Creates a PDF signature without a TSA timestamp or long-term validation evidence. It does not claim LTV compliance.
Chilkat Ruby Downloads
require 'chilkat'
success = false
stringCompare = Chilkat::CkStringBuilder.new()
# Assumes Chilkat is unlocked. See Global Unlock Sample.
# Keep this configuration private, outside public web content and source control.
config = Chilkat::CkJsonObject.new()
config.put_Strict(true)
success = config.LoadFile("qa_data/azure/keyvault.json")
if (success != true)
print "Could not load private Key Vault configuration." + "\n";
exit
end
stringCompare.SetString(config.stringOf("vault_name"))
stringMatch1 = stringCompare.ContentsEqual("",true)
if ((config.JsonTypeOf("vault_name") != 1) or (stringMatch1 == true))
print "Missing or invalid vault_name." + "\n";
exit
end
validate1 = Chilkat::CkStringBuilder.new()
alphabet1 = Chilkat::CkStringBuilder.new()
success = validate1.Append(config.stringOf("vault_name"))
if (success != true)
print "Could not validate vault_name." + "\n";
exit
end
success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-")
if (success != true)
print "Could not initialize validation." + "\n";
exit
end
if ((validate1.get_Length() < 3) or (validate1.get_Length() > 24))
print "Invalid vault_name length." + "\n";
exit
end
charIndex1 = 0
while (charIndex1 < validate1.get_Length())
if (alphabet1.Contains(validate1.getRange(charIndex1,1,false),true) != true)
print "Invalid character in vault_name." + "\n";
exit
end
charIndex1 = charIndex1 + 1
end
vaultName = Chilkat::CkStringBuilder.new()
vaultName.Append(config.stringOf("vault_name"))
letters = Chilkat::CkStringBuilder.new()
letters.Append("abcdefghijklmnopqrstuvwxyz")
if ((letters.Contains(vaultName.getRange(0,1,false),true) != true) or (vaultName.EndsWith("-",true) == true) or (vaultName.Contains("--",true) == true))
print "Invalid vault_name." + "\n";
exit
end
origin = Chilkat::CkStringBuilder.new()
success = origin.Append("https://")
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
success = origin.Append(config.stringOf("vault_name"))
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
success = origin.Append(".vault.azure.net")
if (success != true)
print "Could not construct URL or identifier." + "\n";
exit
end
stringCompare.SetString(config.stringOf("cert_name"))
stringMatch2 = stringCompare.ContentsEqual("",true)
if ((config.JsonTypeOf("cert_name") != 1) or (stringMatch2 == true))
print "Missing or invalid cert_name." + "\n";
exit
end
validate2 = Chilkat::CkStringBuilder.new()
alphabet2 = Chilkat::CkStringBuilder.new()
success = validate2.Append(config.stringOf("cert_name"))
if (success != true)
print "Could not validate cert_name." + "\n";
exit
end
success = alphabet2.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
if (success != true)
print "Could not initialize validation." + "\n";
exit
end
if ((validate2.get_Length() < 1) or (validate2.get_Length() > 127))
print "Invalid cert_name length." + "\n";
exit
end
charIndex2 = 0
while (charIndex2 < validate2.get_Length())
if (alphabet2.Contains(validate2.getRange(charIndex2,1,false),true) != true)
print "Invalid character in cert_name." + "\n";
exit
end
charIndex2 = charIndex2 + 1
end
stringCompare.SetString(config.stringOf("cert_version"))
stringMatch3 = stringCompare.ContentsEqual("",true)
if ((config.JsonTypeOf("cert_version") != 1) or (stringMatch3 == true))
print "Missing or invalid cert_version." + "\n";
exit
end
validate3 = Chilkat::CkStringBuilder.new()
alphabet3 = Chilkat::CkStringBuilder.new()
success = validate3.Append(config.stringOf("cert_version"))
if (success != true)
print "Could not validate cert_version." + "\n";
exit
end
success = alphabet3.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
if (success != true)
print "Could not initialize validation." + "\n";
exit
end
if ((validate3.get_Length() < 1) or (validate3.get_Length() > 127))
print "Invalid cert_version length." + "\n";
exit
end
charIndex3 = 0
while (charIndex3 < validate3.get_Length())
if (alphabet3.Contains(validate3.getRange(charIndex3,1,false),true) != true)
print "Invalid character in cert_version." + "\n";
exit
end
charIndex3 = charIndex3 + 1
end
stringCompare.SetString(config.stringOf("client_id"))
stringMatch4 = stringCompare.ContentsEqual("",true)
if ((config.JsonTypeOf("client_id") != 1) or (stringMatch4 == true))
print "Missing or invalid client_id." + "\n";
exit
end
stringCompare.SetString(config.stringOf("client_secret"))
stringMatch5 = stringCompare.ContentsEqual("",true)
if ((config.JsonTypeOf("client_secret") != 1) or (stringMatch5 == true))
print "Missing or invalid client_secret." + "\n";
exit
end
stringCompare.SetString(config.stringOf("tenant_id"))
stringMatch6 = stringCompare.ContentsEqual("",true)
if ((config.JsonTypeOf("tenant_id") != 1) or (stringMatch6 == true))
print "Missing or invalid tenant_id." + "\n";
exit
end
validate4 = Chilkat::CkStringBuilder.new()
alphabet4 = Chilkat::CkStringBuilder.new()
success = validate4.Append(config.stringOf("tenant_id"))
if (success != true)
print "Could not validate tenant GUID." + "\n";
exit
end
success = alphabet4.Append("0123456789abcdefABCDEF-")
if (success != true)
print "Could not initialize validation." + "\n";
exit
end
if ((validate4.get_Length() < 36) or (validate4.get_Length() > 36))
print "Invalid tenant GUID length." + "\n";
exit
end
charIndex4 = 0
while (charIndex4 < validate4.get_Length())
if (alphabet4.Contains(validate4.getRange(charIndex4,1,false),true) != true)
print "Invalid character in tenant GUID." + "\n";
exit
end
charIndex4 = charIndex4 + 1
end
cert = Chilkat::CkCert.new()
success = cert.LoadFromFile("qa_data/certs/myCert.cer")
if (success != true)
print "Could not load the public signing certificate." + "\n";
exit
end
cloud = Chilkat::CkJsonObject.new()
success = cloud.UpdateString("service","azure_keyvault")
if (success != true)
print "Could not set service." + "\n";
exit
end
success = cloud.UpdateString("client_id",config.stringOf("client_id"))
if (success != true)
print "Could not set client_id." + "\n";
exit
end
success = cloud.UpdateString("client_secret",config.stringOf("client_secret"))
if (success != true)
print "Could not set client_secret." + "\n";
exit
end
success = cloud.UpdateString("tenant_id",config.stringOf("tenant_id"))
if (success != true)
print "Could not set tenant_id." + "\n";
exit
end
success = cloud.UpdateString("vault_name",config.stringOf("vault_name"))
if (success != true)
print "Could not set vault_name." + "\n";
exit
end
success = cloud.UpdateString("cert_name",config.stringOf("cert_name"))
if (success != true)
print "Could not set cert_name." + "\n";
exit
end
success = cloud.UpdateString("cert_version",config.stringOf("cert_version"))
if (success != true)
print "Could not set cert_version." + "\n";
exit
end
success = cert.SetCloudSigner(cloud)
if (success != true)
print "Could not configure the Key Vault signer; inspect diagnostics privately." + "\n";
exit
end
pdf = Chilkat::CkPdf.new()
success = pdf.LoadFile("qa_data/pdf/helloWorld.pdf")
if (success != true)
print "Could not load PDF." + "\n";
exit
end
options = Chilkat::CkJsonObject.new()
success = options.UpdateInt("page",1)
if (success != true)
print "Could not set signature page." + "\n";
exit
end
success = options.UpdateString("hashAlgorithm","sha256")
if (success != true)
print "Could not set hashAlgorithm." + "\n";
exit
end
success = options.UpdateString("appearance.y","top")
if (success != true)
print "Could not set appearance.y." + "\n";
exit
end
success = options.UpdateString("appearance.x","left")
if (success != true)
print "Could not set appearance.x." + "\n";
exit
end
success = options.UpdateString("appearance.fontScale","10.0")
if (success != true)
print "Could not set appearance.fontScale." + "\n";
exit
end
success = options.UpdateString("appearance.text[0]","Digitally signed by: cert_cn")
if (success != true)
print "Could not set appearance.text[0]." + "\n";
exit
end
success = options.UpdateString("appearance.text[1]","current_dt")
if (success != true)
print "Could not set appearance.text[1]." + "\n";
exit
end
success = pdf.SetSigningCert(cert)
if (success != true)
print "Could not select signing certificate." + "\n";
exit
end
success = pdf.SignPdf(options,"qa_output/hello_azure_signed.pdf")
if (success != true)
print "PDF signing failed; inspect diagnostics privately." + "\n";
exit
end
print "Signed PDF saved to qa_output/hello_azure_signed.pdf" + "\n";