移动云的签名算法

  Treatment TT = new Treatment();
        string AccessKey = "***************************";
        string AccessKeySecret = "***************************";
        string SignatureNonce = TT.GetRandomNumber(32);
        string time = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ssZ");
        //string AccessKey = "testid";
        //string AccessKeySecret = "testsecret";
        //string SignatureNonce = "9d81ffbeaaf7477390db5df577bb3299";
        //string time = "2017-01-11T15:15:11Z";
        List list_par = new List();
        list_par.Add($"AccessKey={AccessKey}");
        list_par.Add("SignatureMethod=HmacSHA1");
        list_par.Add($"SignatureNonce={SignatureNonce}");
        list_par.Add("SignatureVersion=V2.0");
        list_par.Add($"Timestamp={Server.UrlEncode(time).ToUpper()}");
        list_par.Add("Version=2016-12-05");
        list_par.Sort();
        string CanonicalizedQueryString = string.Join("&", list_par);
        string hashString = string.Empty;
        // 创建SHA-256哈希对象  
        using (SHA256 sha256 = SHA256.Create())
        {
            // 将要加密的字符串转换为字节数组  
            byte[] inputBytes = Encoding.UTF8.GetBytes(CanonicalizedQueryString);
            // 计算哈希值  
            byte[] hashBytes = sha256.ComputeHash(inputBytes);
            // 将哈希值转换为十六进制字符串表示  
            hashString = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
            // 输出哈希值  
        }
        //string path = "/api/keypair";
        string path = "/api/v2/server/web/without/network";
        string StringToSign = $"GET\n{path.Replace("/", "%2F")}\n{hashString}";
        // 创建HMAC-SHA1哈希对象  
        byte[] keyBytes = Encoding.UTF8.GetBytes($"BC_SIGNATURE&{AccessKeySecret}");
        byte[] dataBytes = Encoding.UTF8.GetBytes(StringToSign);
        string Signature = string.Empty;
        using (HMACSHA1 hmac = new HMACSHA1(keyBytes))
        {
            byte[] hashBytes = hmac.ComputeHash(dataBytes);
            Signature = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
        }
        //string url = $"http://api-guangzhou-2.cmecloud.cn/api/keypair?Version=2016-12-05&AccessKey={AccessKey}&Timestamp={time}&Signature={Signature}&SignatureMethod=HmacSHA1&SignatureVersion=V2.0&SignatureNonce={SignatureNonce}";
        string url = $"https://api-ningbo-1.cmecloud.cn:8443/{path}?AccessKey={AccessKey}&Timestamp={time}&Signature={Signature}&SignatureMethod=HmacSHA1&SignatureNonce=206&SignatureVersion=V2.0&detail=true";
        Response.Write(url);

你可能感兴趣的:(c#,开发语言)