构造 HMAC 签名
可以不使用基于令牌的授权,而改为使用 HMAC 凭证。
这些凭证用于创建类似于 AWS Signature Version 4 的授权头。 计算签名可提供身份验证和传输中数据完整性。 每个签名都与请求的时间戳记绑定,因此不可能复用 authorization 头。 此头包含四个组成部分:算法声明、凭证信息、签名的头和计算的签名。
AWS4-HMAC-SHA256 Credential={access-key}/{date}/{region}/s3/aws4_request,SignedHeaders=host;x-amz-date;{other-required-headers},Signature={signature}
日期以 YYYYMMDD
格式提供,并且区域对应于指定存储区的位置,例如 us
。 始终需要 host
和 x-amz-date
头。 根据请求,还可能需要其他头(例如,在具有有效内容的请求中,需要 x-amz-content-sha256
)。 由于需要重新计算每个请求的签名,因此许多开发者偏好使用将自动生成 authorization 头的工具或 SDK。
创建 authorization
头
首先,需要以标准化格式创建请求。
- 声明要使用的 HTTP 方法(例如,
PUT
)。 - 定义要以标准化方式访问的资源。 这是
http(s)://
与查询字符串之间的地址部分。 对于帐户级别的请求(例如,列出存储区),此部分仅为/
。 - 如果有任何请求参数,那么必须以百分号编码(例如,空格应该表示为
%20
)和字母化方式对这些参数进行标准化。 - 头需要通过除去空格、转换为小写并向每个头添加一个换行符来进行标准化,然后必须按 ASCII 顺序对其进行排序。
- 头以标准格式列出后,必须对其进行“签名”。 这将仅采用头名称,而不采用它们的值,并按字母顺序列出它们,以分号分隔。所有请求都需要
Host
和x-amz-date
。 - 如果请求具有主体(例如,上传对象或创建新的 ACL 时),那么必须使用 SHA-256 算法对请求主体进行散列化,并将其表示为 Base-16 编码的小写字符。
- 将 HTTP 方法、标准化资源、标准化参数、标准化头、签名头和散列请求主体组合在一起,各项之间用换行符分隔,以构成标准化请求。
接下来,我们需要组合一个“string-to-sign”,此字符串将与签名密钥组合在一起,以构成最终签名。 string-to-sign 采用以下格式:
AWS4-HMAC-SHA256
{time}
{date}/{string}/s3/aws4_request
{hashed-standardized-request}
- 时间必须是当前协调世界时,并根据 ISO 8601 规范设置格式(例如,
20161128T152924Z
)。 - 日期为
YYYYMMDD
格式。 - 最后一行是使用 SHA-256 算法进行散列化的先前创建的标准化请求。
现在,我们需要计算签名。
- 首先需要根据帐户的私钥访问密钥、当前日期以及要使用的区域和 API 类型来计算签名密钥。
- 将字符串
AWS4
作为前缀添加到私钥访问密钥,然后将这一新的字符串用作对日期进行散列化的密钥。 - 生成的散列用作对区域进行散列化的密钥。
- 此过程继续,并使用新散列作为对 API 类型进行散列化的密钥。
- 最后,最新的散列用作在创建签名密钥时对字符串
aws4_request
进行散列化的密钥。 - 然后,签名密钥用作在生成最终签名时对 string-to-sign 进行散列化的密钥。
现在,唯一剩下的步骤是组合 authorization
头,如下所示:
AWS4-HMAC-SHA256 Credential={access-key}/{date}/{region}/s3/aws4_request,SignedHeaders=host;x-amz-date;{other-required-headers},Signature={signature}
生成 authorization
头
Python 示例
import os
import datetime
import hashlib
import hmac
import requests
# please don't store credentials directly in code
access_key = os.environ.get('COS_HMAC_ACCESS_KEY_ID')
secret_key = os.environ.get('COS_HMAC_SECRET_ACCESS_KEY')
# request elements
http_method = 'GET'
host = 's3.us.cloud-object-storage.appdomain.cloud'
region = 'us-standard'
endpoint = 'https://s3.us.cloud-object-storage.appdomain.cloud'
bucket = '' # add a '/' before the bucket name to list buckets
object_key = ''
request_parameters = ''
# hashing and signing methods
def hash(key, msg):
return hmac.new(key, msg.encode('utf-8'), hashlib.sha256).digest()
# region is a wildcard value that takes the place of the AWS region value
# as COS doen't use the same conventions for regions, this parameter can accept any string
def createSignatureKey(key, datestamp, region, service):
keyDate = hash(('AWS4' + key).encode('utf-8'), datestamp)
keyString = hash(keyDate, region)
keyService = hash(keyString, service)
keySigning = hash(keyService, 'aws4_request')
return keySigning
# assemble the standardized request
time = datetime.datetime.utcnow()
timestamp = time.strftime('%Y%m%dT%H%M%SZ')
datestamp = time.strftime('%Y%m%d')
standardized_resource = '/' + bucket + '/' + object_key
standardized_querystring = request_parameters
standardized_headers = 'host:' + host + '\n' + 'x-amz-date:' + timestamp + '\n'
signed_headers = 'host;x-amz-date'
payload_hash = hashlib.sha256(''.encode('utf-8')).hexdigest()
standardized_request = (http_method + '\n' +
standardized_resource + '\n' +
standardized_querystring + '\n' +
standardized_headers + '\n' +
signed_headers + '\n' +
payload_hash).encode('utf-8')
# assemble string-to-sign
hashing_algorithm = 'AWS4-HMAC-SHA256'
credential_scope = datestamp + '/' + region + '/' + 's3' + '/' + 'aws4_request'
sts = (hashing_algorithm + '\n' +
timestamp + '\n' +
credential_scope + '\n' +
hashlib.sha256(standardized_request).hexdigest())
# generate the signature
signature_key = createSignatureKey(secret_key, datestamp, region, 's3')
signature = hmac.new(signature_key,
(sts).encode('utf-8'),
hashlib.sha256).hexdigest()
# assemble all elements into the 'authorization' header
v4auth_header = (hashing_algorithm + ' ' +
'Credential=' + access_key + '/' + credential_scope + ', ' +
'SignedHeaders=' + signed_headers + ', ' +
'Signature=' + signature)
# create and send the request
headers = {'x-amz-date': timestamp, 'Authorization': v4auth_header}
# the 'requests' package autmatically adds the required 'host' header
request_url = endpoint + standardized_resource + standardized_querystring
print('\nSending `%s` request to IBM COS -----------------------' % http_method)
print('Request URL = ' + request_url)
request = requests.get(request_url, headers=headers)
print('\nResponse from IBM COS ----------------------------------')
print('Response code: %d\n' % request.status_code)
print(request.text)
Java 示例
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.time.format.DateTimeFormatter;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Formatter;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class CosHMAC {
// please don't store credentials directly in code
private static final String accessKey = System.getenv("COS_HMAC_ACCESS_KEY_ID");
private static final String secretKey = System.getenv("COS_HMAC_SECRET_ACCESS_KEY");
// constants
private static final String httpMethod = "GET";
private static final String host = "s3.us.cloud-object-storage.appdomain.cloud";
private static final String region = "us-standard";
private static final String endpoint = "https://s3.us.cloud-object-storage.appdomain.cloud";
private static final String bucket = ""; // add a '/' before the bucket name to list buckets
private static final String objectKey = "";
private static final String requestParameters = "";
public static void main(String[] args) {
try {
// assemble the standardized request
ZonedDateTime time = ZonedDateTime.now(ZoneOffset.UTC);
String datestamp = time.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
String timestamp = datestamp + "T" + time.format(DateTimeFormatter.ofPattern("HHmmss")) + "Z";
String standardizedResource = bucket + "/" + objectKey;
String standardizedQuerystring = requestParameters;
String standardizedHeaders = "host:" + host + "\n" + "x-amz-date:" + timestamp + "\n";
String signedHeaders = "host;x-amz-date";
String payloadHash = hashHex("");
String standardizedRequest = httpMethod + "\n" +
standardizedResource + "\n" +
standardizedQuerystring + "\n" +
standardizedHeaders + "\n" +
signedHeaders + "\n" +
payloadHash;
// assemble string-to-sign
String hashingAlgorithm = "AWS4-HMAC-SHA256";
String credentialScope = datestamp + "/" + region + "/" + "s3" + "/" + "aws4_request";
String sts = hashingAlgorithm + "\n" +
timestamp + "\n" +
credentialScope + "\n" +
hashHex(standardizedRequest);
// generate the signature
byte[] signatureKey = createSignatureKey(secretKey, datestamp, region, "s3");
String signature = hmacHex(signatureKey, sts);
// assemble all elements into the "authorization" header
String v4auth_header = hashingAlgorithm + " " +
"Credential=" + accessKey + "/" + credentialScope + ", " +
"SignedHeaders=" + signedHeaders + ", " +
"Signature=" + signature;
// create and send the request
String requestUrl = endpoint + standardizedResource + standardizedQuerystring;
URL urlObj = new URL(requestUrl);
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
con.setRequestMethod(httpMethod);
//add request headers
con.setRequestProperty("x-amz-date", timestamp);
con.setRequestProperty("Authorization", v4auth_header);
System.out.printf("\nSending %s request to IBM COS -----------------------", httpMethod);
System.out.println("Request URL = " + requestUrl);
int responseCode = con.getResponseCode();
System.out.println("\nResponse from IBM COS ----------------------------------");
System.out.printf("Response code: %d\n\n", responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
con.disconnect();
}
catch (Exception ex) {
System.out.printf("Error: %s\n", ex.getMessage());
}
}
private static String toHexString(byte[] bytes) {
Formatter formatter = new Formatter();
for (byte b : bytes) {
formatter.format("%02x", b);
}
return formatter.toString();
}
private static byte[] hash(byte[] key, String msg) {
byte[] returnVal = null;
try {
SecretKeySpec signingKey = new SecretKeySpec(key, "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
returnVal = mac.doFinal(msg.getBytes("UTF8"));
}
catch (Exception ex) {
throw ex;
}
finally {
return returnVal;
}
}
private static String hmacHex(byte[] key, String msg) {
String returnVal = null;
try {
returnVal = toHexString(hash(key, msg));
}
catch (Exception ex) {
throw ex;
}
finally {
return returnVal;
}
}
private static String hashHex(String msg) {
String returnVal = null;
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] encodedhash = digest.digest(msg.getBytes(StandardCharsets.UTF_8));
returnVal = toHexString(encodedhash);
}
catch (Exception ex) {
throw ex;
}
finally {
return returnVal;
}
}
// region is a wildcard value that takes the place of the AWS region value
// as COS doesn"t use the same conventions for regions, this parameter can accept any string
private static byte[] createSignatureKey(String key, String datestamp, String region, String service) {
byte[] returnVal = null;
try {
byte[] keyDate = hash(("AWS4" + key).getBytes("UTF8"), datestamp);
byte[] keyString = hash(keyDate, region);
byte[] keyService = hash(keyString, service);
byte[] keySigning = hash(keyService, "aws4_request");
returnVal = keySigning;
}
catch (Exception ex) {
throw ex;
}
finally {
return returnVal;
}
}
}
NodeJS 示例
const crypto = require('crypto');
const moment = require('moment');
const https = require('https');
// please don't store credentials directly in code
const accessKey = process.env.COS_HMAC_ACCESS_KEY_ID;
const secretKey = process.env.COS_HMAC_SECRET_ACCESS_KEY;
const httpMethod = 'GET';
const host = 's3.us.cloud-object-storage.appdomain.cloud';
const region = 'us-standard';
const endpoint = 'https://s3.us.cloud-object-storage.appdomain.cloud';
const bucket = ''; // add a '/' before the bucket name to list buckets
const objectKey = '';
const requestParameters = '';
// hashing and signing methods
function hash(key, msg) {
var hmac = crypto.createHmac('sha256', key);
hmac.update(msg, 'utf8');
return hmac.digest();
}
function hmacHex(key, msg) {
var hmac = crypto.createHmac('sha256', key);
hmac.update(msg, 'utf8');
return hmac.digest('hex');
}
function hashHex(msg) {
var hash = crypto.createHash('sha256');
hash.update(msg);
return hash.digest('hex');
}
// region is a wildcard value that takes the place of the AWS region value
// as COS doesn't use the same conventions for regions, this parameter can accept any string
function createSignatureKey(key, datestamp, region, service) {
keyDate = hash(('AWS4' + key), datestamp);
keyString = hash(keyDate, region);
keyService = hash(keyString, service);
keySigning = hash(keyService, 'aws4_request');
return keySigning;
}
// assemble the standardized request
var time = moment().utc();
var timestamp = time.format('YYYYMMDDTHHmmss') + 'Z';
var datestamp = time.format('YYYYMMDD');
var standardizedResource = bucket + '/' + objectKey;
var standardizedQuerystring = requestParameters;
var standardizedHeaders = 'host:' + host + '\n' + 'x-amz-date:' + timestamp + '\n';
var signedHeaders = 'host;x-amz-date';
var payloadHash = hashHex('');
var standardizedRequest = httpMethod + '\n' +
standardizedResource + '\n' +
standardizedQuerystring + '\n' +
standardizedHeaders + '\n' +
signedHeaders + '\n' +
payloadHash;
// assemble string-to-sign
var hashingAlgorithm = 'AWS4-HMAC-SHA256';
var credentialScope = datestamp + '/' + region + '/' + 's3' + '/' + 'aws4_request';
var sts = hashingAlgorithm + '\n' +
timestamp + '\n' +
credentialScope + '\n' +
hashHex(standardizedRequest);
// generate the signature
var signatureKey = createSignatureKey(secretKey, datestamp, region, 's3');
var signature = hmacHex(signatureKey, sts);
// assemble all elements into the 'authorization' header
var v4authHeader = hashingAlgorithm + ' ' +
'Credential=' + accessKey + '/' + credentialScope + ', ' +
'SignedHeaders=' + signedHeaders + ', ' +
'Signature=' + signature;
// create and send the request
var authHeaders = {'x-amz-date': timestamp, 'Authorization': v4authHeader}
// the 'requests' package autmatically adds the required 'host' header
console.log(authHeaders);
var requestUrl = endpoint + standardizedResource + standardizedQuerystring
console.log(`\nSending ${httpMethod} request to IBM COS -----------------------`);
console.log('Request URL = ' + requestUrl);
var options = {
host: host,
port: 443,
path: standardizedResource + standardizedQuerystring,
method: httpMethod,
headers: authHeaders
}
var request = https.request(options, function (response) {
console.log('\nResponse from IBM COS ----------------------------------');
console.log(`Response code: ${response.statusCode}\n`);
response.on('data', function (chunk) {
console.log(chunk.toString());
});
});
request.end();
后续步骤
您可以在 服务凭证 中查看凭证的文档。 有关认证的概述,请查看 IBM Cloud Identity and Access Management 服务。