Exemplo n.º 1
0
 /// <summary>
 /// 添加一个用户到当前列表,表示该用户已登录
 /// </summary>
 /// <param name="user">用户名</param>
 /// <param name="password">密码</param>
 /// <param name="sid">唯一ID</param>
 /// <param name="e">地址</param>
 /// <returns></returns>
 private static bool AddUserInList(string user, string password, ref string sid, IPEndPoint e)
 {
     try
     {
         System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
         sid = Convert.ToBase64String(hash.ComputeHash(SendMessage.ToBytes(user + password + e + DateTime.Now)));
         UserInfo info = new UserInfo()
         {
             user     = user,
             password = password,
             e        = e.ToString(),
             lastTime = DateTime.Now,
             sid      = sid
         };
         if (now.Contains(user))
         {
             now[user] = info;
         }
         else
         {
             lock (now)
             {
                 now.Add(user, info);
             }
         }
     }
     catch (Exception ex)
     {
         Log.a("Users can not be increased:" + ex.Message);
         return(false);
     }
     return(true);
 }
Exemplo n.º 2
0
        public void GetWsPasswordDigest(string user, string password, string nonce, string timestamp, string digest = "")
        {
            var nonceDecodeBinary = Convert.FromBase64String(nonce);

            byte[] dateBinary     = Encoding.UTF8.GetBytes(timestamp);
            byte[] passwordBinary = Encoding.UTF8.GetBytes(password);
            Console.WriteLine(string.Format("Nonce decoded from B64 -> Hex: {0} ", BitConverter.ToString(nonceDecodeBinary)));

            byte[] concatData = new byte[nonceDecodeBinary.Length + dateBinary.Length + passwordBinary.Length];
            Buffer.BlockCopy(nonceDecodeBinary, 0, concatData, 0, nonceDecodeBinary.Length);
            Buffer.BlockCopy(dateBinary, 0, concatData, nonceDecodeBinary.Length, dateBinary.Length);
            Buffer.BlockCopy(passwordBinary, 0, concatData, nonceDecodeBinary.Length + dateBinary.Length, passwordBinary.Length);

            System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
            string computedDigest = Convert.ToBase64String(sha1.ComputeHash(concatData));

            Console.WriteLine(string.Format("Current Hash:\t{0}\nOriginal Hash:\t{1}", computedDigest, digest));

            if (digest != "")
            {
                if (computedDigest == digest)
                {
                    MessageBox.Show("Hash match" + txtPassword.Text);
                }
                else
                {
                    MessageBox.Show(string.Format("Hash mismatch\nActual\t{0}\nCalc\t{1}", digest, computedDigest));
                }
            }
        }
Exemplo n.º 3
0
 public static byte[] ComputeSHA1HashByte(this string input)
 {
     using (System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create())
     {
         return(sha.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)));
     }
 }
Exemplo n.º 4
0
        /// <summary>
        ///  计算指定文件的SHA1值
        /// </summary>
        /// <param name="fileName">指定文件的完全限定名称</param>
        /// <returns>返回值的字符串形式</returns>
        public static String ComputeSHA1(String fileName)
        {
            String hashSHA1 = String.Empty;

            try
            {
                //检查文件是否存在,如果文件存在则进行计算,否则返回空值
                if (System.IO.File.Exists(fileName))
                {
                    using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
                    {
                        //计算文件的SHA1值
                        System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
                        Byte[] buffer = calculator.ComputeHash(fs);
                        calculator.Clear();
                        //将字节数组转换成十六进制的字符串形式
                        StringBuilder stringBuilder = new StringBuilder();
                        for (int i = 0; i < buffer.Length; i++)
                        {
                            stringBuilder.Append(buffer[i].ToString("x2"));
                        }
                        hashSHA1 = stringBuilder.ToString();
                    }//关闭文件流
                }
            }
            catch (Exception ex)
            { }
            return(hashSHA1);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Get a Hash code of the running app
        /// </summary>
        public static string GetAppHashCode()
        {
            string filename   = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
            string hashstring = string.Empty;

            if (File.Exists(filename))
            {
                try
                {
                    using (System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create())
                    {
                        byte[] hash;

                        using (FileStream stream = File.OpenRead(filename))
                        {
                            hash = sha.ComputeHash(stream);
                        }

                        foreach (byte item in hash)
                        {
                            hashstring += item.ToString("x2");
                        }
                    }
                }
                catch { }
            }
            return(hashstring);
        }
Exemplo n.º 6
0
 public string Encrypt(string str)
 {
     System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
     byte[] preHash = System.Text.Encoding.UTF32.GetBytes(str);
     byte[] hash    = sha.ComputeHash(preHash);
     return(System.Convert.ToBase64String(hash));
 }
Exemplo n.º 7
0
        public bool HashPassword()
        {
            bool   result         = true;
            string _password_hash = string.Empty;

            try
            {
                if (!string.IsNullOrEmpty(_password_salt) && _password_salt.Contains("!PASSWORD!"))
                {
                    System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
                    string str = _password_salt.Replace("!PASSWORD!", _password);
                    _password_hash = BitConverter.ToString(hash.ComputeHash(Encoding.ASCII.GetBytes(str))).Replace("-", "").ToLower();
                }
                else
                {
                    result = false;
                }
            }
            catch
            {
                result = false;
            }

            //We have password hash now, replace password field with the hash.
            if (result)
            {
                Password = _password_hash;
            }

            return(result);
        }
Exemplo n.º 8
0
 /// <summary>
 /// Hash SHA-1 de um Conteúdo em Array de Bytes
 /// </summary>
 /// <param name="input"></param>
 /// <returns>Retorna o Hash em string Hexadecimal</returns>
 public static string SHA1(byte[] input)
 {
     using System.Security.Cryptography.SHA1 hash =
               System.Security.Cryptography.SHA1.Create();
     return(BitConverter.ToString(hash.ComputeHash(input))
            .Replace("-", "").ToLower());
 }
Exemplo n.º 9
0
        private static string HashElf(ElfHeader elf, long basePosition, Stream input)
        {
            using (var sha1 = SHA1.Create())
            {
                foreach (var program in elf.ProgramHeaders)
                {
                    var type  = (uint)program.Type;
                    var flags = (uint)program.Flags;

                    sha1.TransformBlock(BitConverter.GetBytes(type.Swap()), 0, 4, null, 0);
                    sha1.TransformBlock(BitConverter.GetBytes(flags.Swap()), 0, 4, null, 0);

                    if (program.Type == Elf.ProgramType.Loadable && program.MemorySize > 0)
                    {
                        sha1.TransformBlock(BitConverter.GetBytes(program.VirtualAddress.Swap()), 0, 8, null, 0);
                        sha1.TransformBlock(BitConverter.GetBytes(program.MemorySize.Swap()), 0, 8, null, 0);

                        if (program.FileSize > 0)
                        {
                            if (basePosition + program.FileOffset + program.FileSize > input.Length)
                            {
                                return(null);
                            }

                            input.Position = basePosition + program.FileOffset;
                            var programBytes = input.ReadBytes((int)program.FileSize);
                            sha1.TransformBlock(programBytes, 0, programBytes.Length, null, 0);
                        }
                    }
                }
                sha1.TransformFinalBlock(new byte[0], 0, 0);
                return(BitConverter.ToString(sha1.Hash).Replace("-", "").ToLowerInvariant());
            }
        }
Exemplo n.º 10
0
        }        //ComputeCRC32

        /// <summary>
        /// 获取文件的SHA1
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static String GetSHA1(String fileName)
        {
            String hashSHA1 = String.Empty;

            //检查文件是否存在,如果文件存在则进行计算,否则返回空值
            if (File.Exists(fileName))
            {
                using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    //计算文件的SHA1值
                    System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
                    Byte[] buffer = calculator.ComputeHash(fileStream);
                    calculator.Clear();
                    //将字节数组转换成十六进制的字符串形式
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int bufferIdx = 0; bufferIdx < buffer.Length; bufferIdx++)
                    {
                        stringBuilder.Append(buffer[bufferIdx].ToString("x2"));
                    }
                    hashSHA1 = stringBuilder.ToString();
                }                //关闭文件流
            }
            else
            {
                Console.Error.WriteLine("{0}文件找不到!", fileName);
            }
            return(hashSHA1);
        }        //end GetSHA1
Exemplo n.º 11
0
        private bool HashCheck(byte[] newFile, byte[] tmdHash)
        {
            System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
            byte[] fileHash = sha.ComputeHash(newFile);

            return(Wii.Tools.CompareByteArrays(fileHash, tmdHash));
        }
Exemplo n.º 12
0
        public static string ComputeSHA1(string path)
        {
            if (!File.Exists(path))
            {
                throw new NotFileException(path, nameof(path));
            }

            string hashSHA1 = string.Empty;

            //检查文件是否存在,如果文件存在则进行计算,否则返回空值

            using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                //计算文件的SHA1值
                System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
                Byte[] buffer = calculator.ComputeHash(fs);
                calculator.Clear();
                //将字节数组转换成十六进制的字符串形式
                StringBuilder stringBuilder = new StringBuilder();
                for (int i = 0; i < buffer.Length; i++)
                {
                    stringBuilder.Append(buffer[i].ToString("x2"));
                }
                hashSHA1 = stringBuilder.ToString();
            }//关闭文件流

            return(hashSHA1);
        }
Exemplo n.º 13
0
        private static byte[] GetHashCode(string compiledFile, string[] scriptFiles, bool debug)
        {
            using (MemoryStream ms = new MemoryStream()) {
                using (BinaryWriter bin = new BinaryWriter(ms)) {
                    FileInfo fileInfo = new FileInfo(compiledFile);

                    bin.Write(fileInfo.LastWriteTimeUtc.Ticks);

                    foreach (string scriptFile in scriptFiles)
                    {
                        fileInfo = new FileInfo(scriptFile);

                        bin.Write(fileInfo.LastWriteTimeUtc.Ticks);
                    }

                    bin.Write(debug);

                    ms.Position = 0;

                    using (System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create()) {
                        return(sha1.ComputeHash(ms));
                    }
                }
            }
        }
Exemplo n.º 14
0
        public static string Sha1(string fileName)
        {
            string returnValue = null;

            using (System.IO.FileStream fs = System.IO.File.OpenRead(fileName))
            {
                using (System.IO.BufferedStream bs = new System.IO.BufferedStream(fs))
                {
                    using (System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create())
                    {
                        byte[] hash = sha1.ComputeHash(bs);
                        System.Text.StringBuilder formatted = new System.Text.StringBuilder(2 * hash.Length);

                        for (int i = 0; i < hash.Length; ++i)
                        {
                            formatted.AppendFormat(System.Globalization.CultureInfo.InvariantCulture, "{0:x2}", hash[i]);
                        } // Next b

                        returnValue      = formatted.ToString();
                        formatted.Length = 0;
                        formatted        = null;
                    } // End Sha1
                }     // End Using bs
            }         // End Using fs

            return(returnValue);
        } // End Function Sha1
Exemplo n.º 15
0
        /// <summary>
        /// 获取字符串的SHA1
        /// </summary>
        /// <param name="_this"></param>
        /// <param name="_UpCase">是否大写</param>
        /// <returns></returns>
        public static String SHA1(this String _this, Boolean _UpCase = false)
        {
            System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
            Byte[] bytes = sha1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(_this));
            String hash  = BitConverter.ToString(bytes).Replace("-", "");

            return(_UpCase?hash:hash.ToLower());
        }
Exemplo n.º 16
0
        /// <summary>
        /// 对敏感数据进行哈希加密
        /// </summary>
        /// <param name="code">需要加密的字段</param>
        /// <returns></returns>
        public static string Hash(string code, long memberID)
        {
            tb_Mem_Member member = GetMemberByID(memberID);

            System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1Managed.Create();
            code = Convert.ToBase64String(hash.ComputeHash(System.Text.Encoding.Unicode.GetBytes(code + member.Salt)));
            return(code);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Gets SHA1 implementation.
        /// </summary>
        /// <returns><see cref="Sha1"/>.</returns>
        public static Sha1 GetSha1Provider()
        {
#if NetCore
            return(Sha1.Create());
#else
            return(new Sha1());
#endif
        }
Exemplo n.º 18
0
        public static string SHA1(string value)
        {
            byte[] inBuf = Encoding.UTF8.GetBytes(value);
            System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
            var hash = sha1.ComputeHash(inBuf);

            return(OtpTools.ByteArrayToHexString(hash));
        }
Exemplo n.º 19
0
 public byte[] HashPassword(string password)
 {
     System.Security.Cryptography.SHA1 hash    = System.Security.Cryptography.SHA1.Create();
     System.Text.ASCIIEncoding         encoder = new System.Text.ASCIIEncoding();
     byte[] combined = encoder.GetBytes(password);
     hash.ComputeHash(combined);
     return(hash.Hash);
 }
Exemplo n.º 20
0
        public static string ComputeCodeHash(this byte[] code)
        {
            var sha1 = SHA1.Create();

            sha1.TransformFinalBlock(code, 0, code.Length);
            var digest = sha1.Hash;

            return(BitConverter.ToString(digest).Replace("-", "").ToLower());
        }
        public static bool LogFileEquivalence(bool expectMatch, string file1, string file2)
        {
            System.Security.Cryptography.SHA1 hasher = System.Security.Cryptography.SHA1.Create();
            byte[] file1hash = null;
            byte[] file2hash = null;

            using (Stream file1strm = File.OpenRead(file1))
            {
                using (Stream file2strm = File.OpenRead(file2))
                {
                    file1hash = hasher.ComputeHash(file1strm);
                    file2hash = hasher.ComputeHash(file2strm);
                }
            }

            if (null == file1hash || null == file2hash)
            {
                LogFail("The two files did not hash correctly.");
                return(false);
            }

            if (file1hash.Length != file2hash.Length)
            {
                LogFail("The two files did not hash correctly.");
                return(false);
            }

            bool allMatched = true;

            for (int i = 0; i < file1hash.Length; ++i)
            {
                if (expectMatch && file1hash[i] != file2hash[i])
                {
                    LogFail(Path.GetFileName(file1) + " != " + Path.GetFileName(file2));
                    return(false);
                }
                else if (!expectMatch && file1hash[1] != file2hash[i])
                {
                    allMatched = false;
                }
            }

            if (!expectMatch && allMatched)
            {
                LogFail(Path.GetFileName(file1) + " == " + Path.GetFileName(file2));
            }
            else if (expectMatch)
            {
                LogPass(Path.GetFileName(file1) + " == " + Path.GetFileName(file2));
            }
            else
            {
                LogPass(Path.GetFileName(file1) + " != " + Path.GetFileName(file2));
            }

            return(true);
        }
Exemplo n.º 22
0
        public static string GenerateHashString(string s)
        {
            System.Security.Cryptography.SHA1 sha1enc = System.Security.Cryptography.SHA1.Create();
            byte[] wordbytes = Encoding.UTF8.GetBytes(s);
            sha1enc.ComputeHash(wordbytes);
            string hash = BitConverter.ToString(sha1enc.Hash).Replace("-", "");

            return(hash);
        }
Exemplo n.º 23
0
        public static string SHA1Encrypt(string source)
        {
            byte[] sourceBytes = Encoding.UTF8.GetBytes(source);
            using (System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create())
            {
                var targetBytes = sha1.ComputeHash(sourceBytes);

                return(string.Join("", targetBytes.Select(i => i.ToString("x2"))));
            }
        }
Exemplo n.º 24
0
        public void Imorted_sha1_algo_should_generate_same_hash_as_mscorelib_version()
        {
            byte[] source       = Encoding.UTF8.GetBytes("hashmeplease");
            var    mscorlibSha1 = MsCoreLibSha1.Create();
            var    importedSha1 = SHA1.Create();

            var mscorelibHash = mscorlibSha1.ComputeHash(source);
            var importedHash  = importedSha1.ComputeHash(source);

            mscorelibHash.Should().BeEquivalentTo(importedHash);
        }
Exemplo n.º 25
0
        public byte[] GetHash(byte[] data)
        {
            byte[] hash = null;

            using (System.Security.Cryptography.SHA1 crypto = System.Security.Cryptography.SHA1.Create())
            {
                hash = crypto.ComputeHash(data);
            }

            return(hash);
        }
Exemplo n.º 26
0
        string ICedts_PartnerRepository.HashPassword(string str)
        {
            string rethash = string.Empty;

            System.Security.Cryptography.SHA1 hash    = System.Security.Cryptography.SHA1.Create();
            System.Text.ASCIIEncoding         encoder = new System.Text.ASCIIEncoding();
            byte[] combined = encoder.GetBytes(str);
            hash.ComputeHash(combined);
            rethash = Convert.ToBase64String(hash.Hash);
            return(rethash);
        }
Exemplo n.º 27
0
        private string HashPassword(string str)
        {
            string rethash = "";

            System.Security.Cryptography.SHA1 hash    = System.Security.Cryptography.SHA1.Create();
            System.Text.ASCIIEncoding         encoder = new System.Text.ASCIIEncoding();
            byte[] combined = encoder.GetBytes(str);
            hash.ComputeHash(combined);
            rethash = Convert.ToBase64String(hash.Hash);
            return(rethash);
        }
Exemplo n.º 28
0
 public string GetHash(string filename)
 {
     using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
     {
         System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create();
         byte[] bs = sha1.ComputeHash(fs);
         sha1.Clear();
         string h = BitConverter.ToString(bs).ToLower().Replace("-", "");
         return($"{h}_{Model}_{PitchShift:0.0000}_{FormantShift}");
     }
 }
Exemplo n.º 29
0
        public static string Sha1Hex(string value)
        {
            System.Security.Cryptography.SHA1 algorithm = System.Security.Cryptography.SHA1.Create();
            byte[] data = algorithm.ComputeHash(System.Text.Encoding.UTF8.GetBytes(value));
            string sh1  = "";

            for (int i = 0; i < data.Length; i++)
            {
                sh1 += data[i].ToString("x2").ToUpperInvariant();
            }
            return(sh1);
        }
Exemplo n.º 30
0
        }//ComputeCRC32

        /// <summary>
        ///  计算指定文件的SHA1值
        /// </summary>
        /// <param name="fs">文件流</param>
        /// <returns>返回值的字符串形式</returns>
        public static Byte[] SHA1(FileStream fs)
        {
            String hashSHA1 = String.Empty;

            //检查文件是否存在,如果文件存在则进行计算,否则返回空值

            //计算文件的SHA1值
            System.Security.Cryptography.SHA1 calculator = System.Security.Cryptography.SHA1.Create();
            Byte[] buffer = calculator.ComputeHash(fs);
            calculator.Clear();
            return(buffer);
        }
Exemplo n.º 31
0
			public void Dispose()
			{
				sha1.Dispose();
				sha1 = null;
			}