使用python

时间:2019-05-29 23:26:06

标签: utf-8 sha256 azure-storage-account

我正在尝试创建用于使用Azure存储REST API的Authorization标头。什么样的恶梦。我尝试这样做的原因是因为我试图使用工作流生成器(Alteryx)来调用API,所以我唯一的编程选项是Alteryx,python或命令行。

我想我已经接近了,但是在本文之后https://docs.microsoft.com/en-us/azure/storage/common/storage-rest-api-auth?toc=%2fazure%2fstorage%2fblobs%2ftoc.json

我只是不明白这最后三行代码

//现在将其转换为字节数组。 byte [] SignatureBytes = Encoding.UTF8.GetBytes(MessageSignature);

//创建存储密钥的HMACSHA256版本。 HMACSHA256 SHA256 =新的HMACSHA256(Convert.FromBase64String(storageAccountKey));

//计算SignatureBytes的哈希并将其转换为base64字符串。 字符串签名= Convert.ToBase64String(SHA256.ComputeHash(SignatureBytes));

因此,如果我正确地遵循了这一点,我必须创建存储密钥的SHA256版本,但是随后我将特征字节的SHA256哈希作为SHA256哈希吗?

我目前正在使用Google搜索,但并没有走得很远,但基本上是在尝试使用python在.net中做同样的事情。

1 个答案:

答案 0 :(得分:1)

在python中,您可以只使用以下代码行:

signed_string = base64.b64encode(hmac.new(base64.b64decode(storage_account_key), msg=string_to_sign.encode('utf-8'), digestmod=hashlib.sha256).digest()).decode()

这是使用List blobs api的完整代码:

import requests
import datetime
import hmac
import hashlib
import base64

storage_account_name = 'xx'
storage_account_key = 'xxx'
container_name='aa1'
api_version = '2017-07-29'
request_time = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')

string_params = {
    'verb': 'GET',
    'Content-Encoding': '',
    'Content-Language': '',
    'Content-Length': '',
    'Content-MD5': '',
    'Content-Type': '',
    'Date': '',
    'If-Modified-Since': '',
    'If-Match': '',
    'If-None-Match': '',
    'If-Unmodified-Since': '',
    'Range': '',
    'CanonicalizedHeaders': 'x-ms-date:' + request_time + '\nx-ms-version:' + api_version + '\n',
    'CanonicalizedResource': '/' + storage_account_name +'/'+container_name+ '\ncomp:list\nrestype:container'
}

string_to_sign = (string_params['verb'] + '\n' 
                  + string_params['Content-Encoding'] + '\n'
                  + string_params['Content-Language'] + '\n'
                  + string_params['Content-Length'] + '\n'
                  + string_params['Content-MD5'] + '\n' 
                  + string_params['Content-Type'] + '\n' 
                  + string_params['Date'] + '\n' 
                  + string_params['If-Modified-Since'] + '\n'
                  + string_params['If-Match'] + '\n'
                  + string_params['If-None-Match'] + '\n'
                  + string_params['If-Unmodified-Since'] + '\n'
                  + string_params['Range'] + '\n'
                  + string_params['CanonicalizedHeaders']
                  + string_params['CanonicalizedResource'])

signed_string = base64.b64encode(hmac.new(base64.b64decode(storage_account_key), msg=string_to_sign.encode('utf-8'), digestmod=hashlib.sha256).digest()).decode()

headers = {
    'x-ms-date' : request_time,
    'x-ms-version' : api_version,
    'Authorization' : ('SharedKey ' + storage_account_name + ':' + signed_string)
}

url = ('https://' + storage_account_name + '.blob.core.windows.net/'+container_name+'?restype=container&comp=list')

r = requests.get(url, headers = headers)
print(r.status_code)
print('\n\n'+r.text)

测试结果:

enter image description here