static void Main(string[] args)
        {
            Console.WriteLine("MD5 und SHA Beispiel");
            Console.WriteLine("====================");

            //Eingabe lesen und in ein Byte-Array verwandeln
            var bytes = new ASCIIEncoding().GetBytes(Input());

            //MD5
            var md5 = new MD5Cng();
            string md5Hash = BitConverter.ToString(md5.ComputeHash(bytes)).Replace("-", "").ToLower();
            Console.WriteLine("MD5-Hash:\t{0}", md5Hash);

            //SHA1
            var sha1Cng = new SHA1Cng();
            string sha1Hash = BitConverter.ToString(sha1Cng.ComputeHash(bytes)).Replace("-","").ToLower();
            Console.WriteLine("SHA1-Hash:\t{0}", sha1Hash);

            //SHA256
            var sha256 = new SHA256Cng();
            string sha256Hash = BitConverter.ToString(sha256.ComputeHash(bytes)).Replace("-", "").ToLower();
            Console.WriteLine("SHA256-Hash:\t{0}", sha256Hash);

            Console.WriteLine("Beliebige Taste drücken zum beenden");
            Console.ReadKey();
        }
예제 #2
0
        public void AddFile(DeduplicatorState state, FileInfo sourceFile, string destinationPath)
        {
            if (state.DestinationToFileHash.ContainsKey(destinationPath))
            {
                // File has already been added.
                return;
            }

            // Read the source file.
            var memory = new MemoryStream();
            using (var stream = new BufferedStream(new FileStream(sourceFile.FullName, FileMode.Open, FileAccess.Read, FileShare.None), 1200000))
            {
                stream.CopyTo(memory);
            }

            // Hash the memory stream.
            var sha1 = new SHA1Cng();
            memory.Seek(0, SeekOrigin.Begin);
            var hashBytes = sha1.ComputeHash(memory);
            var hashString = BitConverter.ToString(hashBytes).Replace("-", "").ToLowerInvariant();
            memory.Seek(0, SeekOrigin.Begin);

            // Add to the file hash -> source map if not already present.
            if (!state.FileHashToSource.ContainsKey(hashString))
            {
                state.FileHashToSource.Add(hashString, memory);
            }
            else
            {
                memory.Dispose();
            }

            state.DestinationToFileHash.Add(destinationPath, hashString);
        }
        static void Main(string[] args)
        {
            Console.WriteLine("MD5 und SHA Beispiel - Salt & Pepper");
            Console.WriteLine("====================================");

            //sollte aus einer anderen Quelle gelesen werden. Der Einfachheit halber wird ein String verwendet
            var pepper = "@ktdRdotewStee31223!!!33sdrt532sv224vvl420094";

            //Eingabe lesen und in ein Byte-Array verwandeln
            var salt = Salt("Benutzer");
            var bytes = new ASCIIEncoding().GetBytes(string.Format("{0}{1}{2}",Input(), salt, pepper));

            //MD5
            var md5 = new MD5Cng();
            string md5Hash = BitConverter.ToString(md5.ComputeHash(bytes)).Replace("-", "").ToLower();
            Console.WriteLine("MD5-Hash:\t{0}", md5Hash);

            //SHA1
            var sha1Cng = new SHA1Cng();
            string sha1Hash = BitConverter.ToString(sha1Cng.ComputeHash(bytes)).Replace("-","").ToLower();
            Console.WriteLine("SHA1-Hash:\t{0}", sha1Hash);

            //SHA256
            var sha256 = new SHA256Cng();
            string sha256Hash = BitConverter.ToString(sha256.ComputeHash(bytes)).Replace("-", "").ToLower();
            Console.WriteLine("SHA256-Hash:\t{0}", sha256Hash);

            Console.WriteLine("Beliebige Taste drücken zum beenden");
            Console.ReadKey();
        }
예제 #4
0
        /// <summary>
        /// SHA1Cng
        /// 此类型的任何公共static成员都是线程安全的。但不保证所有实例成员都是线程安全的
        /// </summary>
        private static string Encrypt_static(string clearText, Encoding encode)
        {
            SHA1 sha = null;
            try
            {
                byte[] originalBytes = encode.GetBytes(clearText); //Encoding.UTF8.GetBytes(clearText);

                sha = new SHA1Cng();
                byte[] data = sha.ComputeHash(originalBytes);

                //return BitConverter.ToString(data); //将指定的字节数组的每个元素的数值转换为它的等效十六进制字符串表示形式
                StringBuilder builder = new StringBuilder();
                foreach (var item in data)
                {
                    builder.Append(item.ToString("X2"));//将该哈希作为 32 字符的十六进制格式字符串返回
                }

                return builder.ToString();
            }
            catch (ArgumentNullException) { return clearText; }
            catch (EncoderFallbackException) { return clearText; }
            catch (ObjectDisposedException) { return clearText; }
            catch (Exception) { return clearText; }
            finally
            {
                if (sha != null)
                {
                    sha.Clear();
                    sha.Dispose();
                }

                sha = null;
            }
        }
예제 #5
0
        public static string Compute(HashType type, bool format, string data, string salt)
        {
            HashAlgorithm hash;
            string prefix;
            switch (type)
            {
                case HashType.MD5:
                    prefix = "MD5";
                    hash = new MD5Cng();
                    break;
                case HashType.SHA1:
                    prefix = "SHA1";
                    hash = new SHA1Cng();
                    break;
                case HashType.SHA256:
                    prefix = "SHA256";
                    hash = new SHA256Cng();
                    break;
                case HashType.SHA384:
                    prefix = "SHA384";
                    hash = new SHA384Cng();
                    break;
                case HashType.SHA512:
                    prefix = "SHA512";
                    hash = new SHA512Cng();
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(type), type, null);
            }

            byte[] inputBytes = System.Text.Encoding.UTF8.GetBytes(salt+data);
            byte[] hashed = hash.ComputeHash(inputBytes);
            return format ? $"{{{prefix}:{GetHashHex(hashed)}}}" : GetHashHex(hashed);
        }
예제 #6
0
        /// <summary>
        /// Takes 32-bit unsigned integer and gives the LSB 32-bit of SHA1 hash result as UInt32. 
        /// </summary>
        /// <param name="input">32-bit unsigned integer</param>
        /// <returns>LSB 32-bit of SHA1 Hash result.</returns>
        public static uint SHA1_LSB32bit(uint input)
        {
            //SHA1 sha1 = SHA1.Create();
            //SHA1Managed sha1mng = new SHA1Managed();
            SHA1Cng sha1cng = new SHA1Cng();

            return sha1cng.ComputeHash(input.ToByteArray()).ToUInt32();
        }
예제 #7
0
        public static uint OneWayFunction32Bit(byte[] input)
        {
            //SHA1 sha1 = SHA1.Create();
            //SHA1Managed sha1mng = new SHA1Managed();
            SHA1Cng sha1cng = new SHA1Cng();

            return sha1cng.ComputeHash(input).ReduceSHA1to32bit();
        }
예제 #8
0
        // returns the 8-byte hash for a given url
        public static string CreateURLHash(Uri url)
        {
            using (var sha1 = new SHA1Cng())
            {
                byte[] hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(url.ToString()));

                return BitConverter.ToString(hash).Replace("-", "").Substring(0, 8);
            }
        }
예제 #9
0
파일: FileService.cs 프로젝트: Zor-X-L/CKAN
        public string GetFileHashSha1(string filePath)
        {
            using (FileStream fs = new FileStream(@filePath, FileMode.Open))
            using (BufferedStream bs = new BufferedStream(fs))
            using (var sha1 = new SHA1Cng())
            {
                byte[] hash = sha1.ComputeHash(bs);

                return BitConverter.ToString(hash).Replace("-", "");
            }
        }
예제 #10
0
        public static string GetHashedPassword(string username, string password)
        {
            const string SEED = "oZ7QE6LcLJp6fiWzdqZc";

            var finalString = username.ToLower() + password + SEED;
            var finalSha1PassBytes = Encoding.UTF8.GetBytes(finalString);
            var retBytes = new SHA1Cng().ComputeHash(finalSha1PassBytes);

            var ret = BitConverter.ToString(retBytes).Replace("-", "");

            return ret;
        }
예제 #11
0
        /// <summary>
        /// Returns the sha1 sum of the given filename.
        /// Returns null if passed a directory.
        /// Throws an exception on failure to access the file.
        /// </summary>
        private static string Sha1Sum(string path)
        {
            if (Directory.Exists(path))
            {
                return null;
            }

            SHA1 hasher = new SHA1Cng();

            // Even if we throw an exception, the using block here makes sure
            // we close our file.
            using (var fh = File.OpenRead(path))
            {
                string sha1 = BitConverter.ToString(hasher.ComputeHash(fh));
                fh.Close();
                return sha1;
            }
        }
예제 #12
0
        public static HashData ComputeHashes(string fileName)
        {
            try
            {
                using (FileStream stream = File.OpenRead(fileName))
                {
                    using (var bufferedStream = new BufferedStream(stream, 1024 * 32))
                    {
                        string md5, sha1, sha256;
                        byte[] checksum;

                        using (var md5Cng = new MD5Cng())
                        {
                            checksum = md5Cng.ComputeHash(bufferedStream);
                            md5 = BitConverter.ToString(checksum).Replace("-", string.Empty);
                        }

                        stream.Seek(0, SeekOrigin.Begin);
                        bufferedStream.Seek(0, SeekOrigin.Begin);

                        using (var sha1Cng = new SHA1Cng())
                        {
                            checksum = sha1Cng.ComputeHash(bufferedStream);
                            sha1 = BitConverter.ToString(checksum).Replace("-", string.Empty);
                        }

                        stream.Seek(0, SeekOrigin.Begin);
                        bufferedStream.Seek(0, SeekOrigin.Begin);

                        using (var sha256Cng = new SHA256Cng())
                        {
                            checksum = sha256Cng.ComputeHash(bufferedStream);
                            sha256 = BitConverter.ToString(checksum).Replace("-", string.Empty);
                        }

                        return new HashData(md5, sha1, sha256);
                    }
                }
            }
            catch (IOException) { }
            catch (UnauthorizedAccessException) { }
            return null;
        }
예제 #13
0
		protected override void SetUp ()
		{
			hash = new SHA1Cng ();
		}
예제 #14
0
        public void SignHash_CompareSignaturesOfRSACSPAndRSACipher_Pass()
        {
            int wantedKeySize = 4096;
            Random rdm = new Random();
            byte[] message = new byte[256];
            byte[] signedMessage;
            byte[] signedMessageNative;
            string nativeXmlString;
            rdm.NextBytes(message);

            using (var rsacsp = new RSACryptoServiceProvider(wantedKeySize))
            {
                byte[] native = new SHA1Cng().ComputeHash(message);
                signedMessageNative = rsacsp.SignHash(native, "SHA1");
                nativeXmlString = rsacsp.ToXmlString(true);
            }

            using (var cipher = new RSACipher<RSACryptoServiceProvider>(nativeXmlString))
            {
                //byte[] hash = new SHA1Managed().ComputeHash(message);
                signedMessage = cipher.ComputeAndSignHash<SHA1Cng>(message);
            }

            CollectionAssert.AreEqual(signedMessage, signedMessageNative);
        }
예제 #15
0
파일: Program.cs 프로젝트: mkonoplev/chksum
 private static byte[] ComputeHash(string hashAlgorithmName, string fileName)
 {
     HashAlgorithm hashAlgorithm = null;
     switch (hashAlgorithmName)
     {
         case StrMd5:
             hashAlgorithm = new MD5Cng();
             break;
         case StrSha1:
             hashAlgorithm = new SHA1Cng();
             break;
         case StrSha256:
             hashAlgorithm = new SHA256Cng();
             break;
     }
     if (null != hashAlgorithm)
     {
         using (var stream = File.OpenRead(fileName))
         {
             return hashAlgorithm.ComputeHash(stream);
         }
     }
     var message = String.Format("Invalid hash algorithm name: {0}", hashAlgorithmName);
     throw new ApplicationException(message);
 }
예제 #16
0
        public static string ComputeSha1Hash(string fileName)
        {
            string sha1 = null;

            try
            {
                using (FileStream stream = File.OpenRead(fileName))
                {
                    using (var bufferedStream = new BufferedStream(stream, 1024 * 32))
                    {
                        using (var sha = new SHA1Cng())
                        {
                            byte[] checksum = sha.ComputeHash(bufferedStream);
                            sha1 = BitConverter.ToString(checksum).Replace("-", string.Empty);
                        }
                    }
                }
            }
            catch (IOException) { }
            catch (UnauthorizedAccessException) { }
            return sha1;
        }
        /// <summary>
        /// Gets the sha1 hash of the file.
        /// </summary>
        /// <returns></returns>
        public byte[] GetSHA1()
        {
            if (sha1 == null)
            {
                sha1 = new SHA1Cng().ComputeHash(FileStream);
                FileStream.Position = 0;
            }

            return sha1;
        }
예제 #18
0
 static Byte[] ComputeFIPS(Byte[] bytes)
 {
     SHA1Cng sha1 = new SHA1Cng();
     return sha1.ComputeHash(bytes);
 }
예제 #19
0
        private static byte[] CalculatePublicKeyToken(byte[] publicKeyBytes)
        {
            using (var sha1 = new SHA1Cng())
            {
                publicKeyBytes = sha1.ComputeHash(publicKeyBytes);
            }

            byte[] result = new byte[8];
            int length = publicKeyBytes.Length - 1;
            for (int i = 0; i < 8; i++)
            {
                result[i] = publicKeyBytes[length - i];
            }

            return result;
        }
예제 #20
0
		public override void SetUp ()
		{
			hash = new SHA1Cng ();
		}