C#处理SHA1加密和Signature签名验证

多用于手机APP接口加密

可限制请求有效时间



       /// <summary>
        /// 检验signature
        /// </summary>
        /// <param name="signature">加密签名</param>
        /// <param name="timestamp">时间戳</param>
        /// <param name="token">密钥</param>
        /// <returns></returns>
        public static bool CheckSignature(string signature, string timestamp, string token)
        {
            string strResult = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(token + timestamp, "SHA1");
            return signature.Equals(strResult, StringComparison.InvariantCultureIgnoreCase);
        }
        //SHA1加密
        public static String GetHash(String input)
        {
            //建立SHA1对象
            SHA1 sha = new SHA1CryptoServiceProvider();

            //将mystr转换成byte[]
            UTF8Encoding enc = new UTF8Encoding();
            byte[] dataToHash = enc.GetBytes(input);

            //Hash运算
            byte[] dataHashed = sha.ComputeHash(dataToHash);

            //将运算结果转换成string
            string hash = BitConverter.ToString(dataHashed).Replace("-", "");

            return hash;
        }