Darabonba Signature Utility

0.0.4 · active · verified Fri Apr 17

The `alibabacloud-darabonba-signature-util` library, version 0.0.4, is a low-level utility component of the Alibaba Cloud Python SDK ecosystem. It provides essential cryptographic functions for signing requests, including HMAC-SHA1 and HMAC-SHA256, and base64 encoding, based on the Darabonba framework. It's primarily designed for internal use by other Alibaba Cloud SDK modules rather than direct end-user application. The library currently has an infrequent release cadence, with the last significant update being over a year ago.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `Darabonba_SignatureUtil` to sign a string using HMAC-SHA1 and HMAC-SHA256, which are common requirements for authenticating requests to cloud services. It highlights the critical step of converting input strings to UTF-8 bytes before passing them to the signing functions, and then base64 encoding the resulting signature bytes.

import os
import base64
from alibabacloud_darabonba_signature_util_py.util import Darabonba_SignatureUtil

# These credentials would typically be loaded securely, e.g., from environment variables
access_key_id = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_ID", "YOUR_ACCESS_KEY_ID")
access_key_secret = os.environ.get("ALIBABA_CLOUD_ACCESS_KEY_SECRET", "YOUR_ACCESS_KEY_SECRET")

# Example string to sign, often a canonicalized HTTP request string
string_to_sign = "GET\n\n\n1442129102\n/oss/"

# The signing functions expect bytes, so convert strings to utf-8 bytes
try:
    signed_bytes_sha1 = Darabonba_SignatureUtil.get_h_macsha1(
        bytes(string_to_sign, 'utf-8'),
        bytes(access_key_secret, 'utf-8')
    )
    signature_sha1 = base64.b64encode(signed_bytes_sha1).decode('utf-8')

    signed_bytes_sha256 = Darabonba_SignatureUtil.get_h_macsha256(
        bytes(string_to_sign, 'utf-8'),
        bytes(access_key_secret, 'utf-8')
    )
    signature_sha256 = base64.b64encode(signed_bytes_sha256).decode('utf-8')

    print(f"String to Sign: '{string_to_sign}'")
    print(f"HMAC-SHA1 Signature (Base64 Encoded): {signature_sha1}")
    print(f"HMAC-SHA256 Signature (Base64 Encoded): {signature_sha256}")
except Exception as e:
    print(f"An error occurred: {e}")
    print("Please ensure ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET are set.")

view raw JSON →