做刷题网站赚钱么,仲恺住房和城乡建设局网站,网络推广服务投诉,智能建站实验报告目录
一、HMAC-SHA1
二、UriEncode
三、Date
四、Content-MD5
五、参数操作
六、阿里云API鉴权 一、HMAC-SHA1 使用 RFC 2104 中定义的 HMAC-SHA1 方法生成带有密钥的哈希值#xff1a; private static string CalculateSignature(string secret, string data){byte[] k…目录
一、HMAC-SHA1
二、UriEncode
三、Date
四、Content-MD5
五、参数操作
六、阿里云API鉴权 一、HMAC-SHA1 使用 RFC 2104 中定义的 HMAC-SHA1 方法生成带有密钥的哈希值 private static string CalculateSignature(string secret, string data){byte[] keyBytes Encoding.UTF8.GetBytes(secret);byte[] dataBytes Encoding.UTF8.GetBytes(data);using (var hmacsha1 new HMACSHA1(keyBytes)){byte[] hashBytes hmacsha1.ComputeHash(dataBytes);// 将哈希值转换为十六进制字符串StringBuilder hexString new StringBuilder(hashBytes.Length * 2);foreach (byte b in hashBytes){hexString.AppendFormat({0:x2}, b);}return hexString.ToString();}}
二、UriEncode 按照RFC 3986进行 URI 编码
private static string Rfc3986Encode(string value)
{// 使用 Uri.EscapeDataString 进行初步编码var encoded Uri.EscapeDataString(value);// 根据 RFC 3986 的要求替换字符encoded encoded.Replace(%20, );encoded encoded.Replace(%21, !);encoded encoded.Replace(%27, );encoded encoded.Replace(%28, ();encoded encoded.Replace(%29, ));encoded encoded.Replace(%7E, ~ );return encoded;
}
三、Date 当前 UTC 时间以 ISO 8601 yyyyMMddTHHmmssZ 格式表示
string date DateTime.UtcNow.ToString(yyyyMMddTHHmmssZ); HTTP 1.1 协议中规定的 GMT 时间示例Wed, 01 Mar 2006 12:00:00 GMT public static string GetCurrentDateTimeInStandardFormat(){// 获取当前时间并转换到指定时区var dateTimeOffset DateTimeOffset.UtcNow.ToOffset(TimeSpan.FromHours(8));// 将日期时间格式化为字符串var formattedDateTime dateTimeOffset.ToString(yyyy-MM-ddTHH:mm:sszzz);// 替换时区部分的冒号return formattedDateTime.Substring(0, 22) formattedDateTime.Substring(23);}
四、Content-MD5 RFC 1864 定义的请求体Body md5 摘要128 位二进制表示并使用 base64 编码 private static string CalculateMD5(byte[] data){using (var md5 MD5.Create()){byte[] hashBytes md5.ComputeHash(data);return Convert.ToBase64String(hashBytes);}}
五、参数操作 对 URL 中所有参数名、参数值单独 UriEncode按参数名字母升序的顺序依次将参数名、参数值用 及 拼接 private static string ConstructCanonicalQueryString(Dictionarystring, string queryParams){if (queryParams null || queryParams.Count 0)return ;var sortedQueryParams queryParams.OrderBy(kvp kvp.Key);var encodedQueryParams sortedQueryParams.Select(kvp ${Rfc3986Encode(kvp.Key)}{Rfc3986Encode(kvp.Value)});return string.Join(, encodedQueryParams);}
六、阿里云API鉴权 下方是阿里云上传文件API鉴权及请求示例其实接入SDK更方便一些。阿里云上传文件后路径为https://桶名称.oss-cn-beijing.aliyuncs.com/文件夹/文件
using UnityEngine;
using System.Collections;
using System.Security.Cryptography;
using System.Text;
using System;
using System.IO;
using UnityEngine.Networking;public class AliYunManager : MonoBehaviour
{private const string OSS_HOST oss-cn-beijing.aliyuncs.com;//域名private const string BUCKET_NAME *****;//bucket名称private const string ACCESS_KEY_ID ************;private const string ACCESS_KEY_SECRET **************;// Use this for initializationvoid Start(){UploadFileAsync(D:\\FCJProject\\YiDongYanFaBu\\StudentPartner\\StudentPartner\\Bundles\\StandaloneWindows\\DefaultPackage\\5.0.0\\1ba6bb9f41d66f71f7deb9d2d63b29e4.bundle, /debug/PC/9.92.0);}/// summary/// /// /summary/// param nameimagePath本地文件绝对路径/param/// param namebucketPath需要保存的文件在bucket上的相对路径前后不需要加/也不需要加文件名/parampublic void UploadFileAsync(string filePath, string bucketPath){StartCoroutine(UploadFile(filePath, bucketPath));}IEnumerator UploadFile(string filePath, string bucketPath){// Load the image from filebyte[] imageBytes File.ReadAllBytes(filePath);string fileName Path.GetFileName(filePath);string bucketFilePath / bucketPath / fileName;string url https:// BUCKET_NAME . OSS_HOST bucketFilePath;string contentMD5 ;// ToMD5(imageBytes);string contentType application/octet-stream;string utcGMT DateTime.UtcNow.ToString(r);string canonicalizedOSSHeaders ;string canonicalizedResource / BUCKET_NAME bucketFilePath;string authorization GetAuthorization(PUT, contentMD5, contentType, utcGMT, canonicalizedOSSHeaders, canonicalizedResource);Debug.Log(Authorization: authorization);Debug.Log(url: url);//Debug.Log(contentMD5: contentMD5);// Create UnityWebRequestUnityWebRequest request new UnityWebRequest(url, PUT);//request.uploadHandler new UploadHandlerRaw(imageData);request.downloadHandler new DownloadHandlerBuffer();// Set headersrequest.SetRequestHeader(Content-Length, imageBytes.Length.ToString());request.SetRequestHeader(Content-Type, contentType);request.SetRequestHeader(Host, BUCKET_NAME . OSS_HOST);request.SetRequestHeader(Date, utcGMT);request.SetRequestHeader(Authorization, authorization);// Set the body of the requestUploadHandlerRaw uploadHandler new UploadHandlerRaw(imageBytes);uploadHandler.contentType contentType;request.uploadHandler uploadHandler;// Send the request and wait for a responseyield return request.SendWebRequest();if (request.result ! UnityWebRequest.Result.Success){Debug.LogError($Error: {request.error});Debug.LogError($Response Code: {request.responseCode});Debug.LogError($Response Text: {request.downloadHandler.text});}elseDebug.Log(request.downloadHandler.text);}private string GetAuthorization(string method, string contentMD5, string contentType,string utcGMT, string canonicalizedOSSHeaders, string canonicalizedResource){string data method \n contentMD5 \n contentType \n utcGMT \n canonicalizedOSSHeaders canonicalizedResource;Debug.Log(data: data);string signature ToHMACSHA1_Base64(ACCESS_KEY_SECRET, data);string authorization OSS ACCESS_KEY_ID : signature;return authorization;}private static string ToHMACSHA1_Base64(string key, string data){using (HMACSHA1 hmacsha1 new HMACSHA1(Encoding.UTF8.GetBytes(key))){byte[] dataBuffer Encoding.UTF8.GetBytes(data);byte[] hashBytes hmacsha1.ComputeHash(dataBuffer);string result Convert.ToBase64String(hashBytes);return result;}}private static string ToMD5(byte[] data){using (MD5 md5Hash MD5.Create()){byte[] hashBytes md5Hash.ComputeHash(data);Debug.Log(hashBytes.Length);return Convert.ToBase64String(hashBytes);}}
}
文章转载自: http://www.morning.fkgcd.cn.gov.cn.fkgcd.cn http://www.morning.lngyd.cn.gov.cn.lngyd.cn http://www.morning.qqbw.cn.gov.cn.qqbw.cn http://www.morning.wmfr.cn.gov.cn.wmfr.cn http://www.morning.bflwj.cn.gov.cn.bflwj.cn http://www.morning.jbshh.cn.gov.cn.jbshh.cn http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn http://www.morning.kjnfs.cn.gov.cn.kjnfs.cn http://www.morning.pwlxy.cn.gov.cn.pwlxy.cn http://www.morning.mxmzl.cn.gov.cn.mxmzl.cn http://www.morning.wiitw.com.gov.cn.wiitw.com http://www.morning.xptkl.cn.gov.cn.xptkl.cn http://www.morning.w58hje.cn.gov.cn.w58hje.cn http://www.morning.kdfqx.cn.gov.cn.kdfqx.cn http://www.morning.bxczt.cn.gov.cn.bxczt.cn http://www.morning.mbfj.cn.gov.cn.mbfj.cn http://www.morning.wpqcj.cn.gov.cn.wpqcj.cn http://www.morning.lmknf.cn.gov.cn.lmknf.cn http://www.morning.qclmz.cn.gov.cn.qclmz.cn http://www.morning.jwqqd.cn.gov.cn.jwqqd.cn http://www.morning.mngh.cn.gov.cn.mngh.cn http://www.morning.wqhlj.cn.gov.cn.wqhlj.cn http://www.morning.gqfks.cn.gov.cn.gqfks.cn http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn http://www.morning.zympx.cn.gov.cn.zympx.cn http://www.morning.dqwkm.cn.gov.cn.dqwkm.cn http://www.morning.ypnxq.cn.gov.cn.ypnxq.cn http://www.morning.ygqjn.cn.gov.cn.ygqjn.cn http://www.morning.pyxwn.cn.gov.cn.pyxwn.cn http://www.morning.qxycf.cn.gov.cn.qxycf.cn http://www.morning.bnxfj.cn.gov.cn.bnxfj.cn http://www.morning.djlxz.cn.gov.cn.djlxz.cn http://www.morning.slkqd.cn.gov.cn.slkqd.cn http://www.morning.gnlyq.cn.gov.cn.gnlyq.cn http://www.morning.xsqbx.cn.gov.cn.xsqbx.cn http://www.morning.wblpn.cn.gov.cn.wblpn.cn http://www.morning.rmqmc.cn.gov.cn.rmqmc.cn http://www.morning.mslsn.cn.gov.cn.mslsn.cn http://www.morning.rqxtb.cn.gov.cn.rqxtb.cn http://www.morning.c7617.cn.gov.cn.c7617.cn http://www.morning.wfysn.cn.gov.cn.wfysn.cn http://www.morning.jncxr.cn.gov.cn.jncxr.cn http://www.morning.xdttq.cn.gov.cn.xdttq.cn http://www.morning.ltbwq.cn.gov.cn.ltbwq.cn http://www.morning.tsxg.cn.gov.cn.tsxg.cn http://www.morning.bmts.cn.gov.cn.bmts.cn http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.ggnfy.cn.gov.cn.ggnfy.cn http://www.morning.xxiobql.cn.gov.cn.xxiobql.cn http://www.morning.fthcq.cn.gov.cn.fthcq.cn http://www.morning.ttrdr.cn.gov.cn.ttrdr.cn http://www.morning.rfgkf.cn.gov.cn.rfgkf.cn http://www.morning.vnuwdy.cn.gov.cn.vnuwdy.cn http://www.morning.mjbkp.cn.gov.cn.mjbkp.cn http://www.morning.yslfn.cn.gov.cn.yslfn.cn http://www.morning.mslsn.cn.gov.cn.mslsn.cn http://www.morning.jkrrg.cn.gov.cn.jkrrg.cn http://www.morning.lxctl.cn.gov.cn.lxctl.cn http://www.morning.bnjnp.cn.gov.cn.bnjnp.cn http://www.morning.qfkxj.cn.gov.cn.qfkxj.cn http://www.morning.rgkd.cn.gov.cn.rgkd.cn http://www.morning.jzlkq.cn.gov.cn.jzlkq.cn http://www.morning.xfmzk.cn.gov.cn.xfmzk.cn http://www.morning.rjynd.cn.gov.cn.rjynd.cn http://www.morning.qnywy.cn.gov.cn.qnywy.cn http://www.morning.xwqxz.cn.gov.cn.xwqxz.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.wlsrd.cn.gov.cn.wlsrd.cn http://www.morning.jzkqg.cn.gov.cn.jzkqg.cn http://www.morning.hbfqm.cn.gov.cn.hbfqm.cn http://www.morning.xhsxj.cn.gov.cn.xhsxj.cn http://www.morning.knlbg.cn.gov.cn.knlbg.cn http://www.morning.cpnlq.cn.gov.cn.cpnlq.cn http://www.morning.stpkz.cn.gov.cn.stpkz.cn http://www.morning.mnsmb.cn.gov.cn.mnsmb.cn http://www.morning.zcfsq.cn.gov.cn.zcfsq.cn http://www.morning.gxeqedd.cn.gov.cn.gxeqedd.cn http://www.morning.hksxq.cn.gov.cn.hksxq.cn http://www.morning.lflnb.cn.gov.cn.lflnb.cn http://www.morning.skrcn.cn.gov.cn.skrcn.cn