You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.8 KiB
C#

11 months ago
using System;
using System.Text;
using System.Security.Cryptography;
using System.Collections.Generic;
namespace BP.Tools
{
public static class AuthV3Util
{
/*
-
appKey : ID
salt :
curtime : ()
signType :
sign :
@param appKey ID
@param appSecret
@param paramsMap
*/
public static void addAuthParams(string appKey, string appSecret, Dictionary<string, string[]> paramsMap)
{
string q = "";
string[] qArray;
if (paramsMap.ContainsKey("q"))
{
qArray = paramsMap["q"];
}
else
{
qArray = paramsMap["img"];
}
foreach (var item in qArray)
{
q += item;
}
string salt = System.Guid.NewGuid().ToString();
string curtime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds() + "";
string sign = calculateSign(appKey, appSecret, q, salt, curtime);
paramsMap.Add("appKey", new string[] { appKey });
paramsMap.Add("salt", new string[] { salt });
paramsMap.Add("curtime", new string[] { curtime });
paramsMap.Add("signType", new string[] { "v3" });
paramsMap.Add("sign", new string[] { sign });
}
/*
-
: sign = sha256(appKey + input(q) + salt + curtime + appSecret)
@param appKey ID
@param appSecret
@param q
@param salt
@param curtime ()
@return sign
*/
public static string calculateSign(string appKey, string appSecret, string q, string salt, string curtime)
{
string strSrc = appKey + getInput(q) + salt + curtime + appSecret;
return encrypt(strSrc);
}
private static string encrypt(string strSrc)
{
Byte[] inputBytes = Encoding.UTF8.GetBytes(strSrc);
HashAlgorithm algorithm = new SHA256CryptoServiceProvider();
Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
return BitConverter.ToString(hashedBytes).Replace("-", "");
}
private static string getInput(string q)
{
if (q == null)
{
return "";
}
int len = q.Length;
return len <= 20 ? q : q.Substring(0, 10) + len + q.Substring(len - 10, 10);
}
}
}