Darabonba Signature Utility
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
-
ModuleNotFoundError: No module named 'alibabacloud.darabonba.signature_util'
cause Incorrect import path used; the Python module name does not directly match the PyPI package name structure.fixChange the import statement to `from alibabacloud_darabonba_signature_util_py.util import Darabonba_SignatureUtil`. -
TypeError: expected bytes, str found
cause The signing functions (`get_h_macsha1`, `get_h_macsha256`) were called with `str` objects instead of `bytes` objects.fixEnsure all string inputs (string to sign, secret key) are converted to UTF-8 bytes using `bytes(my_string, 'utf-8')` before passing them to the signing functions.
Warnings
- gotcha The actual Python module name for imports (`alibabacloud_darabonba_signature_util_py`) differs from the PyPI package name (`alibabacloud-darabonba-signature-util`). Forgetting this can lead to `ModuleNotFoundError`.
- gotcha Signing functions like `get_h_macsha1` and `get_h_macsha256` expect `bytes` objects for both the string to sign and the secret key, not standard Python `str` objects. Passing `str` will result in a `TypeError`.
- gotcha This library provides low-level cryptographic primitives. It is primarily an internal component of the Alibaba Cloud SDK. Direct use by end-users for custom security implementations should be done with extreme caution, as incorrect usage of raw crypto can lead to severe security vulnerabilities.
Install
-
pip install alibabacloud-darabonba-signature-util
Imports
- Darabonba_SignatureUtil
from alibabacloud.darabonba.signature_util import Darabonba_SignatureUtil
from alibabacloud_darabonba_signature_util_py.util import Darabonba_SignatureUtil
Quickstart
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.")