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

Azure Key Vault: Sign and Locally Verify ES256

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 HTTPS and Key Vault data-plane API 2026-04-01. Configure vault_name and first run the client-credentials token example. It writes access_token and an expires_at UTC timestamp to qa_data/tokens/azureKeyVaultToken.json. Each request checks expiration; refresh is explicit. The sample stops on errors and does not retry. Production callers should handle 429/Retry-After and transient failures with bounded backoff, and reconcile uncertain writes. Redirects are disabled.

Set key_name and object_version. Requires keys/get and keys/sign. Uses the public JWK from the pinned key version. Sends a SHA-256 digest as base64url; local verification does not call keys/verify. ES256 uses P-256 and the service raw r||s signature, not ASN.1 DER. Chilkat Ecc verification accepts this representation and must return 1.

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

Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.FollowRedirects = False
http.ConnectTimeout = 20
http.ReadTimeout = 60
http.VerboseLogging = False
Dim request As ChilkatHttpRequest
request.Initialize
request.HttpVerb = "GET"
Dim response As ChilkatHttpResponse
response.Initialize
Dim result As ChilkatJsonObject
result.Initialize
result.Strict = True
'  First run Get a Key Vault access token.
Dim token As ChilkatJsonObject
token.Initialize
token.Strict = True
success = token.LoadFile("qa_data/tokens/azureKeyVaultToken.json")
If success <> True Then
    Log("Could not read the private token file.")
    Return
End If

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

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

Dim expires As ChilkatDateTime
expires.Initialize
Dim now As ChilkatDateTime
now.Initialize
success = expires.SetFromTimestamp(token.StringOf("expires_at"))
If success <> True Then
    Log("Invalid token expiration.")
    Return
End If

http.AuthToken = token.StringOf("access_token")
stringCompare.SetString(config.StringOf("key_name"))
Dim stringMatch4 As Boolean = stringCompare.ContentsEqual("", True)
If (config.JsonTypeOf("key_name") <> 1) Or (stringMatch4 = True) Then
    Log("Missing or invalid key_name.")
    Return
End If

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

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

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

Dim charIndex20 As Int = 0
Do While (charIndex20 < validate20.Length)
    If alphabet20.Contains(validate20.GetRange(charIndex20, 1, False), True) <> True Then
        Log("Invalid character in key_name.")
        Return
    End If

    charIndex20 = charIndex20 + 1
Loop
Dim resourceBase As ChilkatStringBuilder
resourceBase.Initialize
success = resourceBase.Append(origin.GetAsString)
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = resourceBase.Append("/keys/")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

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

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

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

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

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

Dim charIndex21 As Int = 0
Do While (charIndex21 < validate21.Length)
    If alphabet21.Contains(validate21.GetRange(charIndex21, 1, False), True) <> True Then
        Log("Invalid character in object_version.")
        Return
    End If

    charIndex21 = charIndex21 + 1
Loop
Dim resourceId As ChilkatStringBuilder
resourceId.Initialize
success = resourceId.Append(resourceBase.GetAsString)
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = resourceId.Append("/")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

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

Dim getUrl As ChilkatStringBuilder
getUrl.Initialize
success = getUrl.Append(resourceId.GetAsString)
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = getUrl.Append("?api-version=2026-04-01")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = now.SetFromCurrentSystemTime
If success <> True Then
    Log("Could not read current time.")
    Return
End If

If expires.DiffSeconds(now) <= 60 Then
    Log("Token is expired or near expiration. Obtain a new token before continuing.")
    Return
End If

request.HttpVerb = "GET"
success = http.HttpReq(getUrl.GetAsString, request, response)
If success <> True Then
    Log("Request did not complete; inspect diagnostics privately.")
    Return
End If

If response.StatusCode <> 200 Then
    Log("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
    Return
End If

success = result.Load(response.BodyStr)
If success <> True Then
    Log("Invalid JSON response.")
    Return
End If

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

stringCompare.SetString(result.StringOf("key.kid"))
Dim stringMatch7 As Boolean = stringCompare.ContentsEqual(resourceId.GetAsString, True)
If stringMatch7 <> True Then
    Log("Response identifier does not match the requested version.")
    Return
End If

Dim jwk As ChilkatJsonObject
jwk.Initialize
success = result.ObjectOf2("key", jwk)
If success <> True Then
    Log("Missing key JWK.")
    Return
End If

Dim publicKey As ChilkatPublicKey
publicKey.Initialize
success = publicKey.LoadFromString(jwk.Emit)
If success <> True Then
    Log("Could not load public JWK.")
    Return
End If

stringCompare.SetString(jwk.StringOf("kty"))
Dim stringMatch8 As Boolean = stringCompare.ContentsEqual("EC", True)
If stringMatch8 <> True Then
    Log("Wrong key type for this algorithm.")
    Return
End If

stringCompare.SetString(jwk.StringOf("crv"))
Dim stringMatch9 As Boolean = stringCompare.ContentsEqual("P-256", True)
If stringMatch9 <> True Then
    Log("ES256 requires P-256.")
    Return
End If

Dim message As String = "Key Vault signing example"
Dim hash As ChilkatCrypt2
hash.Initialize("hash")
hash.Charset = "utf-8"
hash.HashAlgorithm = "sha256"
hash.EncodingMode = "base64url"
Dim digest As String = hash.HashStringENC(message)
If hash.LastMethodSuccess <> True Then
    Log("Could not hash message.")
    Return
End If

Dim body As ChilkatJsonObject
body.Initialize
success = body.UpdateString("alg", "ES256")
If success <> True Then
    Log("Could not set alg.")
    Return
End If

success = body.UpdateString("value", digest)
If success <> True Then
    Log("Could not set value.")
    Return
End If

Dim operationUrl As ChilkatStringBuilder
operationUrl.Initialize
success = operationUrl.Append(resourceId.GetAsString)
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = operationUrl.Append("/sign?api-version=2026-04-01")
If success <> True Then
    Log("Could not construct URL or identifier.")
    Return
End If

success = now.SetFromCurrentSystemTime
If success <> True Then
    Log("Could not read current time.")
    Return
End If

If expires.DiffSeconds(now) <= 60 Then
    Log("Token is expired or near expiration. Obtain a new token before continuing.")
    Return
End If

success = http.HttpJson("POST", operationUrl.GetAsString, body, "application/json", response)
If success <> True Then
    Log("Request failed; reconcile uncertain writes before retrying.")
    Return
End If

If response.StatusCode <> 200 Then
    Log("Unexpected HTTP status; inspect request ID and diagnostics privately. Reconcile writes before retrying.")
    Return
End If

success = result.Load(response.BodyStr)
If success <> True Then
    Log("Invalid JSON response.")
    Return
End If

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

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

stringCompare.SetString(result.StringOf("kid"))
Dim stringMatch12 As Boolean = stringCompare.ContentsEqual(resourceId.GetAsString, True)
If stringMatch12 <> True Then
    Log("Response identifier does not match the requested version.")
    Return
End If

Dim validate30 As ChilkatStringBuilder
validate30.Initialize
Dim alphabet30 As ChilkatStringBuilder
alphabet30.Initialize
success = validate30.Append(result.StringOf("value"))
If success <> True Then
    Log("Could not validate signature.")
    Return
End If

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

If (validate30.Length < 1) Or (validate30.Length > 10485760) Then
    Log("Invalid signature length.")
    Return
End If

Dim charIndex30 As Int = 0
Do While (charIndex30 < validate30.Length)
    If alphabet30.Contains(validate30.GetRange(charIndex30, 1, False), True) <> True Then
        Log("Invalid character in signature.")
        Return
    End If

    charIndex30 = charIndex30 + 1
Loop
Dim signature As ChilkatBinData
signature.Initialize
success = signature.AppendEncoded(result.StringOf("value"), "base64url")
If success <> True Then
    Log("Could not decode signature.")
    Return
End If

If signature.NumBytes <> 64 Then
    Log("ES256 must return fixed-width 32-byte r followed by 32-byte s.")
    Return
End If

Dim verifier As ChilkatEcc
verifier.Initialize
Dim verified As Int = verifier.VerifyHashENC(digest, result.StringOf("value"), "base64url", publicKey)
If verified <> 1 Then
    Log("ECDSA signature invalid or verification failed.")
    Return
End If

Log("Example completed.")