Sample code for 30+ languages & platforms
B4X Requires Chilkat v11.6.0+

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.

Microsoft reference

Chilkat B4X Downloads

B4X
Dim success As Boolean = False

Dim stringCompare As ChilkatStringBuilder
stringCompare.Initialize
'  Assumes Chilkat is unlocked. See Global Unlock Sample.
'  Keep this configuration private, outside public web content and source control.
Dim config As ChilkatJsonObject
config.Initialize
config.Strict = True
success = config.LoadFile("qa_data/azure/keyvault.json")
If success <> True Then
    Log("Could not load private Key Vault configuration.")
    Return
End If

stringCompare.SetString(config.StringOf("vault_name"))
Dim stringMatch1 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("vault_name") <> 1) Or (stringMatch1 = True) Then
    Log("Missing or invalid vault_name.")
    Return
End If

Dim validate1 As ChilkatStringBuilder
validate1.Initialize
Dim alphabet1 As ChilkatStringBuilder
alphabet1.Initialize
success = validate1.Append(config.StringOf("vault_name"))
If success <> True Then
    Log("Could not validate vault_name.")
    Return
End If

success = alphabet1.Append("abcdefghijklmnopqrstuvwxyz0123456789-")
If success <> True Then
    Log("Could not initialize validation.")
    Return
End If

If (validate1.Length < 3) Or (validate1.Length > 24) Then
    Log("Invalid vault_name length.")
    Return
End If

Dim charIndex1 As Int = 0
Do While (charIndex1 < validate1.Length)
    If alphabet1.Contains(validate1.GetRange(charIndex1, 1, False), True) <> True Then
        Log("Invalid character in vault_name.")
        Return
    End If

    charIndex1 = charIndex1 + 1
Loop
Dim vaultName As ChilkatStringBuilder
vaultName.Initialize
vaultName.Append(config.StringOf("vault_name"))
Dim letters As ChilkatStringBuilder
letters.Initialize
letters.Append("abcdefghijklmnopqrstuvwxyz")
If (letters.Contains(vaultName.GetRange(0, 1, False), True) <> True) Or (vaultName.EndsWith("-", True) = True) Or (vaultName.Contains("--", True) = True) Then
    Log("Invalid vault_name.")
    Return
End If

Dim origin As ChilkatStringBuilder
origin.Initialize
success = origin.Append("https://")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = origin.Append(config.StringOf("vault_name"))
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = origin.Append(".vault.azure.net")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

stringCompare.SetString(config.StringOf("cert_name"))
Dim stringMatch2 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("cert_name") <> 1) Or (stringMatch2 = True) Then
    Log("Missing or invalid cert_name.")
    Return
End If

Dim validate2 As ChilkatStringBuilder
validate2.Initialize
Dim alphabet2 As ChilkatStringBuilder
alphabet2.Initialize
success = validate2.Append(config.StringOf("cert_name"))
If success <> True Then
    Log("Could not validate cert_name.")
    Return
End If

success = alphabet2.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If success <> True Then
    Log("Could not initialize validation.")
    Return
End If

If (validate2.Length < 1) Or (validate2.Length > 127) Then
    Log("Invalid cert_name length.")
    Return
End If

Dim charIndex2 As Int = 0
Do While (charIndex2 < validate2.Length)
    If alphabet2.Contains(validate2.GetRange(charIndex2, 1, False), True) <> True Then
        Log("Invalid character in cert_name.")
        Return
    End If

    charIndex2 = charIndex2 + 1
Loop
stringCompare.SetString(config.StringOf("cert_version"))
Dim stringMatch3 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("cert_version") <> 1) Or (stringMatch3 = True) Then
    Log("Missing or invalid cert_version.")
    Return
End If

Dim validate3 As ChilkatStringBuilder
validate3.Initialize
Dim alphabet3 As ChilkatStringBuilder
alphabet3.Initialize
success = validate3.Append(config.StringOf("cert_version"))
If success <> True Then
    Log("Could not validate cert_version.")
    Return
End If

success = alphabet3.Append("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-")
If success <> True Then
    Log("Could not initialize validation.")
    Return
End If

If (validate3.Length < 1) Or (validate3.Length > 127) Then
    Log("Invalid cert_version length.")
    Return
End If

Dim charIndex3 As Int = 0
Do While (charIndex3 < validate3.Length)
    If alphabet3.Contains(validate3.GetRange(charIndex3, 1, False), True) <> True Then
        Log("Invalid character in cert_version.")
        Return
    End If

    charIndex3 = charIndex3 + 1
Loop
stringCompare.SetString(config.StringOf("client_id"))
Dim stringMatch4 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("client_id") <> 1) Or (stringMatch4 = True) Then
    Log("Missing or invalid client_id.")
    Return
End If

stringCompare.SetString(config.StringOf("client_secret"))
Dim stringMatch5 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("client_secret") <> 1) Or (stringMatch5 = True) Then
    Log("Missing or invalid client_secret.")
    Return
End If

stringCompare.SetString(config.StringOf("tenant_id"))
Dim stringMatch6 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("tenant_id") <> 1) Or (stringMatch6 = True) Then
    Log("Missing or invalid tenant_id.")
    Return
End If

Dim validate4 As ChilkatStringBuilder
validate4.Initialize
Dim alphabet4 As ChilkatStringBuilder
alphabet4.Initialize
success = validate4.Append(config.StringOf("tenant_id"))
If success <> True Then
    Log("Could not validate tenant GUID.")
    Return
End If

success = alphabet4.Append("0123456789abcdefABCDEF-")
If success <> True Then
    Log("Could not initialize validation.")
    Return
End If

If (validate4.Length < 36) Or (validate4.Length > 36) Then
    Log("Invalid tenant GUID length.")
    Return
End If

Dim charIndex4 As Int = 0
Do While (charIndex4 < validate4.Length)
    If alphabet4.Contains(validate4.GetRange(charIndex4, 1, False), True) <> True Then
        Log("Invalid character in tenant GUID.")
        Return
    End If

    charIndex4 = charIndex4 + 1
Loop
Dim cert As ChilkatCert
cert.Initialize("cert")
success = cert.LoadFromFile("qa_data/certs/myCert.cer")
If success <> True Then
    Log("Could not load the public signing certificate.")
    Return
End If

Dim cloud As ChilkatJsonObject
cloud.Initialize
success = cloud.UpdateString("service", "azure_keyvault")
If success <> True Then
    Log("Could not set service.")
    Return
End If

success = cloud.UpdateString("client_id", config.StringOf("client_id"))
If success <> True Then
    Log("Could not set client_id.")
    Return
End If

success = cloud.UpdateString("client_secret", config.StringOf("client_secret"))
If success <> True Then
    Log("Could not set client_secret.")
    Return
End If

success = cloud.UpdateString("tenant_id", config.StringOf("tenant_id"))
If success <> True Then
    Log("Could not set tenant_id.")
    Return
End If

success = cloud.UpdateString("vault_name", config.StringOf("vault_name"))
If success <> True Then
    Log("Could not set vault_name.")
    Return
End If

success = cloud.UpdateString("cert_name", config.StringOf("cert_name"))
If success <> True Then
    Log("Could not set cert_name.")
    Return
End If

success = cloud.UpdateString("cert_version", config.StringOf("cert_version"))
If success <> True Then
    Log("Could not set cert_version.")
    Return
End If

success = cert.SetCloudSigner(cloud)
If success <> True Then
    Log("Could not configure the Key Vault signer; inspect diagnostics privately.")
    Return
End If

Dim pdf As ChilkatPdf
pdf.Initialize("pdf")
success = pdf.LoadFile("qa_data/pdf/helloWorld.pdf")
If success <> True Then
    Log("Could not load PDF.")
    Return
End If

Dim options As ChilkatJsonObject
options.Initialize
success = options.UpdateInt("page", 1)
If success <> True Then
    Log("Could not set signature page.")
    Return
End If

success = options.UpdateString("hashAlgorithm", "sha256")
If success <> True Then
    Log("Could not set hashAlgorithm.")
    Return
End If

success = options.UpdateString("appearance.y", "top")
If success <> True Then
    Log("Could not set appearance.y.")
    Return
End If

success = options.UpdateString("appearance.x", "left")
If success <> True Then
    Log("Could not set appearance.x.")
    Return
End If

success = options.UpdateString("appearance.fontScale", "10.0")
If success <> True Then
    Log("Could not set appearance.fontScale.")
    Return
End If

success = options.UpdateString("appearance.text[0]", "Digitally signed by: cert_cn")
If success <> True Then
    Log("Could not set appearance.text[0].")
    Return
End If

success = options.UpdateString("appearance.text[1]", "current_dt")
If success <> True Then
    Log("Could not set appearance.text[1].")
    Return
End If

success = pdf.SetSigningCert(cert)
If success <> True Then
    Log("Could not select signing certificate.")
    Return
End If

success = pdf.SignPdf(options, "qa_output/hello_azure_signed.pdf")
If success <> True Then
    Log("PDF signing failed; inspect diagnostics privately.")
    Return
End If

Log("Signed PDF saved to qa_output/hello_azure_signed.pdf")