Kaynağa Gözat

活动地点

郑毅 2 ay önce
ebeveyn
işleme
f7324ef14e
3 değiştirilmiş dosya ile 124 ekleme ve 3 silme
  1. 97 0
      BytePlusAuthUtils.cs
  2. 27 3
      Program.cs
  3. 0 0
      files/携程酒店(1).txt

+ 97 - 0
BytePlusAuthUtils.cs

@@ -0,0 +1,97 @@
+using System.Security.Cryptography;
+using System.Text;
+
+public class BytePlusAuthUtils
+{
+    /**
+     * 
+     * @param ak: AccessKey
+     * @param sk: SecretKey
+     * @param expirationSeconds: The expiration time. Units are in seconds
+     * @param method: GET, POST, PUT
+     * @param uriPath: The requestd path. Not a complete URL
+     * @param params: The requested parameters
+     * @param body: The requested json body
+     * @return
+     */
+    public static string Sign(string ak, string sk, int expirationSeconds, string method, string uriPath, Dictionary<string, string> parameters, string body)
+    {
+        string cm = CanonicalMethod(method);
+        string cu = CanonicalUrl(uriPath);
+        string cp = CanonicalParam(parameters);
+        string cb = CanonicalBody(body);
+        string text = cm + "\n" + cu + "\n" + cp + "\n" + cb;
+        return DoSign(ak, sk, expirationSeconds, text);
+    }
+
+    private static string CanonicalMethod(string method)
+    {
+        return "HTTPMethod:" + method;
+    }
+
+    private static string CanonicalUrl(string url)
+    {
+        return "CanonicalURI:" + url;
+    }
+
+    private static string CanonicalParam(Dictionary<string, string> parameters)
+    {
+        string res = "CanonicalQueryString:";
+        if (parameters == null || parameters.Count == 0)
+        {
+            return res;
+        }
+        foreach (var key in parameters.Keys)
+        {
+            res += FormatKeyValue(key, parameters[key]) + "&";
+        }
+        return res.Substring(0, res.Length - 1);
+    }
+
+    private static string FormatKeyValue(string key, string value)
+    {
+        return key + "=" + value;
+    }
+
+    private static string CanonicalBody(string body)
+    {
+        string res = "CanonicalBody:";
+        if (body == null)
+        {
+            return res;
+        }
+        else
+        {
+            return res + body;
+        }
+    }
+
+    private static string DoSign(string ak, string sk, int expiration, string text)
+    {
+        string signKeyInfo = "ak-v1/" + ak + "/" + (long)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds + "/" + expiration;
+        string signKey = Sha256Hmac(signKeyInfo, sk);
+        string signResult = Sha256Hmac(text, signKey);
+        return signKeyInfo + "/" + signResult;
+    }
+
+    private static string Sha256Hmac(string message, string secret)
+    {
+        string hash = "";
+        using (var hmacsha256 = new HMACSHA256(Convert.FromBase64String(secret)))
+        {
+            var bytes = hmacsha256.ComputeHash(Encoding.UTF8.GetBytes(message));
+            hash = ByteArrayToHexString(bytes);
+        }
+        return hash;
+    }
+
+    private static string ByteArrayToHexString(byte[] b)
+    {
+        StringBuilder hs = new StringBuilder();
+        foreach (byte c in b)
+        {
+            hs.Append(c.ToString("x2"));
+        }
+        return hs.ToString().ToLower();
+    }
+}

+ 27 - 3
Program.cs

@@ -10,6 +10,33 @@ class Program
     static string IDPATTERN = "ObjectId\\(\"(.+)\"\\)";
     static void Main(string[] args)
     {
+        BytePlusAuthTest();
+
+        Console.WriteLine("Press any key...");
+        Console.ReadLine();
+    }
+
+    private static string accessKey = "AKAPZGNkNGYzZmJiNGVmNDUxNjk0MDM5MmI4NDM5MmY5YmY";
+    private static string secretKey = "WkRjNVpEVTBPVEkyTUdJeU5EUTFZems0TURFeU9UZGxOV1JsTURreFpEYw==";
+    private static int expirationSeconds = 86400;
+
+    private static void BytePlusAuthTest()
+    {
+        string method = "POST";
+        // The requested path. Not a complete URL
+        string uri = "/dataprofile/openapi/v1/751/users/185";
+        var exampleQueryParams = new Dictionary<string, string>
+        {
+            { "set_once", "true" }
+        };
+        string exampleQueryBodyJson = "{\"name\":\"name\",\"value\":\"zhangsan\"}";
+
+        string authorization = BytePlusAuthUtils.Sign(accessKey, secretKey, expirationSeconds,
+            method, uri, exampleQueryParams, exampleQueryBodyJson);
+        Console.WriteLine("authorization: " + authorization);
+    }
+
+    static void Test1(string[] args) {
         int idx = 0;
         foreach (var item in args)
         {   
@@ -37,8 +64,5 @@ class Program
             }
             idx++;
         }
-
-        Console.WriteLine("Press any key...");
-        Console.ReadLine();
     }
 }

Dosya farkı çok büyük olduğundan ihmal edildi
+ 0 - 0
files/携程酒店(1).txt


Bu fark içinde çok fazla dosya değişikliği olduğu için bazı dosyalar gösterilmiyor