cf图床代码问题请教,签名

[复制链接]
查看: 552   回复: 9
发表于 2021-9-21 16:00:46 | 显示全部楼层 |阅读模式
https://developers.cloudflare.com/images/serving-images/serving-private-images-using-signed-url-tokens

有大佬有现成的C# 的么。
或下面这怎么翻译C#的
const encoder = new TextEncoder();
  const secretKeyData = encoder.encode(KEY);
  const key = await crypto.subtle.importKey(
    "raw",
    secretKeyData,
    { name: "HMAC", hash: "SHA-256" },
    false,
    ["sign"]
  );
回复

使用道具 举报

发表于 2021-9-21 16:01:01 | 显示全部楼层
https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.hmacsha256
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 16:01:58 | 显示全部楼层
sha256 会了,主要没看懂他这个逻辑。
这是啥语言的代码,nodejs么。
const encoder = new TextEncoder();
  const secretKeyData = encoder.encode(KEY);
比如不明白encoder.encode 是url 编码么?
url.searchParams.set 是做什么的没明白。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 16:02:26 | 显示全部楼层
https://developer.mozilla.org/zh-CN/docs/Web/API/TextEncoder

new TextEncoder().encode() 简单点就 Encoding.UTF8.GetBytes()

url.searchParams 是 https://docs.microsoft.com/en-us/dotnet/api/system.uri.query
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 16:02:44 | 显示全部楼层
string KEY = "11111111";
            int EXPIRATION = 60 * 60 * 24;
            int ex = (int)Math.Floor((double)(GetTimeStamp(DateTime.Now) / 1000)) + EXPIRATION;
            string url = "https:/imagedelivery.net?key="+ex;
            
            var sha256 = new SHA256Managed();
            var Asc = new UTF8Encoding();
            var tmpByte = Asc.GetBytes(KEY);
            var bufferToHex = sha256.ComputeHash(tmpByte);
            sha256.Clear();
只会到这了,求大佬指点。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 16:03:14 | 显示全部楼层
  1.         const string KEY = "YOUR_KEY_FROM_IMAGES_DASHBOARD";        const int EXPIRATION = 60 * 60 * 24; // 1 day        string generateSignedUrl(string url)        {            // `url` is a full imagedelivery.net URL            // e.g. https://imagedelivery.net/cheeW4oKsx5ljh8e8BoL2A/bc27a117-9509-446b-8c69-c81bfeac0a01/mobile            var key = Encoding.UTF8.GetBytes(KEY);            using var hmac = new HMACSHA256(key);            // Attach the expiration value to the `url`            var expiry = DateTimeOffset.UtcNow.ToUnixTimeSeconds() + EXPIRATION;            // `url` now looks like            // https://imagedelivery.net/cheeW4oKsx5ljh8e8BoL2A/bc27a117-9509-446b-8c69-c81bfeac0a01/mobile?exp=1631289275            var stringToSign = $"{new Uri(url).AbsolutePath}?exp={expiry}";            // e.g. /cheeW4oKsx5ljh8e8BoL2A/bc27a117-9509-446b-8c69-c81bfeac0a01/mobile?exp=1631289275            // Generate the signature            var mac = hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign));            var sig = Convert.ToHexString(mac).ToLower();            // And attach it to the `url`            return $"{url}?exp={expiry}&sig={sig}";        }
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 16:03:36 | 显示全部楼层
大佬,net core3 没有ToHexString
我改成 var sig =  BitConverter.ToString(mac);
           sig = sig.Replace("-", "");
        
应该一样吧。
最后得到的签名格式如下。对么。
sig=3346494CFA9CF00D67B8F004C4927DE8E174E3DEC3895977E9273EF641F943AD

好像有另一个文档。上传图片的。
https://api.cloudflare.com/#cloudflare-images-update-image

用httpclient 上传文件成功,好像不能传递可选参数。求助大佬。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 16:03:55 | 显示全部楼层
url 格式是这样。好像不太对。
// `url` now looks like
  // https://imagedelivery.net/cheeW4oKsx5ljh8e8BoL2A/bc27a117-9509-446b-8c69-c81bfeac0a01/mobile?exp=1631289275
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 16:04:53 | 显示全部楼层
加密的值和官方的不一样,设置同样的key和内容加密测试的。可能c# 写的算法不一样
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-21 16:05:43 | 显示全部楼层
我没绑卡 所以没法测试 stringToSign 应该是不带域名和协议的 之前看漏了 现在改了 顺便将 sign 也给转小写了 应该没啥问题
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则