using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
namespace FineUIPro.iWareWms.Util
{
    public class PasswordUtil
    {
        #region field & constructor
        //private static readonly Log _log = new Log(typeof(PasswordUtil));
        private const int saltLength = 4;
        public PasswordUtil() { }
        #endregion
        /// 
        /// 加密
        /// 
        /// 
        /// 
        public static string PassWord_StringEncoding(string pwd)//加密
        {
            return EncryptDES(pwd, "0c6b0450");
        }
        /// 
        /// 解密
        /// 
        /// 
        /// 
        public static string PassWord_StringDecoding(string pwd)//加密
        {
            return DecryptDES(pwd, "0c6b0450");
        }
        /// 
        /// 对比用户明文密码是否和加密后密码一致
        /// 
        /// 数据库中单向加密后的密码
        /// 用户明文密码
        /// 
        public static bool ComparePasswords(string dbPassword, string userPassword)
        {
            if (string.IsNullOrEmpty(dbPassword) || string.IsNullOrEmpty(userPassword))
                return false;
            if (dbPassword.Equals(EncryptDES(userPassword, "0c6b0450")))
                return true;
            else
                return false;
        }
        /// 
        /// DES加密字符串
        /// 
        /// 待加密的字符串
        /// 加密密钥,要求为8位
        /// 加密成功返回加密后的字符串,失败返回源串 
        public static string EncryptDES(string encryptString, string encryptKey)
        {
            byte[] Keys = { 0x22, 0x44, 0x86, 0xA8, 0x9A, 0xAF, 0xCD, 0x4F };
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));//转换为字节
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();//实例化数据加密标准
                MemoryStream mStream = new MemoryStream();//实例化内存流
                //将数据流链接到加密转换的流
                CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Convert.ToBase64String(mStream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }
        /// 
        /// DES解密字符串
        /// 
        /// 待解密的字符串
        /// 解密密钥,要求为8位,和加密密钥相同
        /// 解密成功返回解密后的字符串,失败返源串
        public static string DecryptDES(string decryptString, string decryptKey)
        {
            byte[] Keys = { 0x22, 0x44, 0x86, 0xA8, 0x9A, 0xAF, 0xCD, 0x4F };
            try
            {
                byte[] rgbKey = Encoding.UTF8.GetBytes(decryptKey);
                byte[] rgbIV = Keys;
                byte[] inputByteArray = Convert.FromBase64String(decryptString);
                DESCryptoServiceProvider DCSP = new DESCryptoServiceProvider();
                MemoryStream mStream = new MemoryStream();
                CryptoStream cStream = new CryptoStream(mStream, DCSP.CreateDecryptor(rgbKey, rgbIV), CryptoStreamMode.Write);
                cStream.Write(inputByteArray, 0, inputByteArray.Length);
                cStream.FlushFinalBlock();
                return Encoding.UTF8.GetString(mStream.ToArray());
            }
            catch
            {
                return decryptString;
            }
        }
        /// 
        /// 创建用户的数据库密码
        /// 
        /// 
        /// 
        public static string CreateDbPassword(string userPassword)
        {
            //return PassWordHelp.StringEncoding(userPassword);
            return PassWord_StringEncoding(userPassword);
        }
        #region 私有函数
        /// 
        /// 将一个字符串哈希化
        /// 
        /// 
        /// 
        private static byte[] HashString(string str)
        {
            byte[] pwd = System.Text.Encoding.UTF8.GetBytes(str);
            SHA1 sha1 = SHA1.Create();
            byte[] saltedPassword = sha1.ComputeHash(pwd);
            return saltedPassword;
        }
        private static bool CompareByteArray(byte[] array1, byte[] array2)
        {
            if (array1.Length != array2.Length)
                return false;
            for (int i = 0; i < array1.Length; i++)
            {
                if (array1[i] != array2[i])
                    return false;
            }
            return true;
        }
        // create a salted password given the salt value
        private static byte[] CreateSaltedPassword(byte[] saltValue, byte[] unsaltedPassword)
        {
            // add the salt to the hash
            byte[] rawSalted = new byte[unsaltedPassword.Length + saltValue.Length];
            unsaltedPassword.CopyTo(rawSalted, 0);
            saltValue.CopyTo(rawSalted, unsaltedPassword.Length);
            //Create the salted hash			
            SHA1 sha1 = SHA1.Create();
            byte[] saltedPassword = sha1.ComputeHash(rawSalted);
            // add the salt value to the salted hash
            byte[] dbPassword = new byte[saltedPassword.Length + saltValue.Length];
            saltedPassword.CopyTo(dbPassword, 0);
            saltValue.CopyTo(dbPassword, saltedPassword.Length);
            return dbPassword;
        }
        #endregion
    }
}