当前位置: 首页 > news >正文

Unity | API鉴权用到的函数汇总

目录

一、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(Dictionary<string, 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 name="imagePath">本地文件绝对路径</param>/// <param name="bucketPath">需要保存的文件在bucket上的相对路径,前后不需要加'/',也不需要加文件名</param>public 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.lryc.cn/news/386288.html

相关文章:

  • 【python】socket通信代码解析
  • FastGPT 手动部署错误:MongooseServerSelectionError: getaddrinfo EAI_AGAIN mongo
  • 用英文介绍芝加哥(1):Making Modern Chicago Part 1 Building a Boomtown
  • 【启明智显分享】低成本RISC-V工业级HMI方案推荐
  • 深入探索STM32的SPI功能:W25Q64 Flash存储器全攻略
  • 【SQL Server数据库】带函数查询和综合查询(1)
  • 使用WebService接口进行数据通信
  • AI进阶指南第五课,大模型相关概念(知识库,微调)
  • 【深度学习基础】`view` 和 `reshape` 的参数详解
  • 【笔记】Spring Cloud Gateway 实现 gRPC 代理
  • 云顶之弈数据网站
  • Linux(Ubuntu)下源码开发整个流程完成版本(下载->编译->模拟器运行)
  • el-form表单实现校验
  • 一台TrinityCore服务器客户端连接网速慢(未解决)
  • [系统运维|Xshell]宿主机无法连接上NAT网络下的虚拟机进行维护?主机ping不通NAT网络下的虚拟机,虚拟机ping的通主机!解决办法
  • C 语言实例 - 查找数组中最大的元素值
  • MySQL之可扩展性(七)
  • 微服务框架中Nacos的个人学习心得
  • Unity Animator 运行时修改某个动画状态的播放速度
  • 阿里云常用的操作
  • 【MATLAB源码-第231期】基于matlab的polar码编码译码仿真,对比SC,SCL,BP,SCAN,SSC等译码算法误码率。
  • 创新实训(十三) 项目开发——实现用户终止对话功能
  • 基于Java+MySQL停车场车位管理系统详细设计和实现(源码+LW+调试文档+讲解等)
  • LeetCode 53.最大子数组和(dp)
  • IOS17闪退问题Assertion failure in void _UIGraphicsBeginImageContextWithOptions
  • float8格式
  • 云效BizDevOps上手亲测
  • 亚太杯赛题思路发布(中文版)
  • 【Linux】部署 GitLab 服务
  • cs与msf权限传递以及mimikatz抓取win2012明文密码