private Task DeleteAsync(string key) { var url = $"https://rs.qbox.me/delete/{QiniuBase64.UrlSafeBase64Encode($"{options.Value.Bucket}:{key}")}"; AddAuthorization(url); return(client.PostAsync(url, new ByteArrayContent(Array.Empty <byte>()))); }
private async Task BatchDeleteAsync(IEnumerable <string> keys) { var keyArray = keys as string[] ?? keys.ToArray(); if (keyArray.IsNullOrEmpty()) { // Task.CompletedTask; return; } var bucket = options.Value.Bucket; var ops = keyArray.Select(k => $"/delete/{QiniuBase64.UrlSafeBase64Encode($"{bucket}:{k}")}"); // ReSharper disable once PossibleMultipleEnumeration var opsStr = string.Join('&', ops.Select(k => $"op={k}")); var url = $"https://rs.qiniu.com/batch"; var data = Encoding.UTF8.GetBytes(opsStr); AddAuthorization(url, data); // ReSharper disable once PossibleMultipleEnumeration var content = new FormUrlEncodedContent(ops.Select(k => new KeyValuePair <string, string>("op", k))); var response = await client.PostAsync(url, content); Trace.WriteLine(await response.Content.ReadAsStringAsync()); }
/// <summary> /// HTTP请求签名 /// </summary> /// <param name="url">请求目标的URL</param> /// <param name="body">请求的主体数据</param> /// <returns></returns> public string SignRequest(string url, byte[] body) { Uri u = new Uri(url); string pathAndQuery = u.PathAndQuery; byte[] pathAndQueryBytes = Encoding.UTF8.GetBytes(pathAndQuery); using (MemoryStream buffer = new MemoryStream()) { buffer.Write(pathAndQueryBytes, 0, pathAndQueryBytes.Length); buffer.WriteByte((byte)'\n'); if (body != null && body.Length > 0) { buffer.Write(body, 0, body.Length); } #if WINDOWS_UWP var hma = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1); var skBuffer = CryptographicBuffer.ConvertStringToBinary(mac.SecretKey, BinaryStringEncoding.Utf8); var hmacKey = hma.CreateKey(skBuffer); var dataBuffer = CryptographicBuffer.CreateFromByteArray(buffer.ToArray()); var signBuffer = CryptographicEngine.Sign(hmacKey, dataBuffer); byte[] digest; CryptographicBuffer.CopyToByteArray(signBuffer, out digest); #else HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(mac.SecretKey)); byte[] digest = hmac.ComputeHash(buffer.ToArray()); #endif string digestBase64 = QiniuBase64.UrlSafeBase64Encode(digest); return(string.Format("{0}:{1}", mac.AccessKey, digestBase64)); } }
private string encodedSign(byte[] data) { #if WINDOWS_UWP var hma = MacAlgorithmProvider.OpenAlgorithm(MacAlgorithmNames.HmacSha1); var skBuffer = CryptographicBuffer.ConvertStringToBinary(mac.SecretKey, BinaryStringEncoding.Utf8); var hmacKey = hma.CreateKey(skBuffer); var dataBuffer = CryptographicBuffer.CreateFromByteArray(data); var signBuffer = CryptographicEngine.Sign(hmacKey, dataBuffer); byte[] digest; CryptographicBuffer.CopyToByteArray(signBuffer, out digest); #else HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(mac.SecretKey)); byte[] digest = hmac.ComputeHash(data); #endif return(QiniuBase64.UrlSafeBase64Encode(digest)); }
/// <summary> /// 附带数据的签名 /// </summary> /// <param name="data">待签名的数据</param> /// <returns></returns> public string SignWithData(byte[] data) { string sstr = QiniuBase64.UrlSafeBase64Encode(data); return(string.Format("{0}:{1}:{2}", mac.AccessKey, encodedSign(sstr), sstr)); }