Thank you for your time and patience, as per above request you are looking for code to sign a JSON containing data with Azure Key Vault secret and return the hash of the signed data.
- I was able to get the following Py code, kindly try this and you may either try to load json data from file
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient
import json
import hashlib
import base64
from cryptography.hazmat.primitives.asymmetric import padding, rsa
from cryptography.hazmat.primitives import serialization
# Replace with your key vault name and secret name
key_vault_name = "<your-key-vault-name>"
secret_name = "<your-secret-name>"
# Create a SecretClient object to access the secret
credential = DefaultAzureCredential()
vault_uri = f"https://{key_vault_name}.vault.azure.net"
secret_client = SecretClient(vault_uri=vault_uri, credential=credential)
# Get the secret value
secret = secret_client.get_secret(secret_name)
secret_value = secret.value
# Create a hash of the JSON data
with open (r'C:\Users\Downloads\parameters.json')as file: # local path or URL of JSON file
# Load JSON data from file
data = json.load(file)
json_string = json.dumps(data)
hash_value = hashlib.sha256(json_string.encode()).hexdigest()
print(hash_value)
# Sign the hash with the secret value
private_key = serialization.load_pem_private_key(secret_value.encode(), password=None)
signature = private_key.sign(
hash_value.encode(),
padding.PKCS1v15(),
hashlib.sha256()
)
# Return the hash of the signed data
print(hash_value)
**Please "Accept the answer (Yes)" and "share your feedback ". This will help us and others in the community as well.
Thanks,
Akshay Kaushik**