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();
        }
        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();
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
 private string ComputeHash(FileInfo filePath)
 {
     byte[] hash;
     using (FileStream fs = filePath.Open(FileMode.Open))
     {
         MD5 md5 = new MD5Cng();
         hash = md5.ComputeHash(fs);
     }
     return BitConverter.ToString(hash);
 }
Ejemplo n.º 5
0
        public static string ComputeHash(string data)
        {
            if (string.IsNullOrEmpty(data))
                throw new ArgumentNullException("data");

            MD5 md5 = new MD5Cng();
            byte[] rawdata = Encoding.UTF8.GetBytes(data);
            byte[] hash = md5.ComputeHash(rawdata);

            return HttpUtility.UrlEncode(hash);
        }
Ejemplo n.º 6
0
 public static CodeId FromCode(string code)
 {
     code = code.Replace("\r\n", "\n").Trim();
     var md5bin = new MD5Cng().ComputeHash(Encoding.UTF8.GetBytes(code));
     var id = new CodeId
     {
         PartitionKey = string.Format("{0:X2}", md5bin.First()).Substring(0, 1),
         RowKey = Convert.ToBase64String(md5bin).Replace('+', '-').Replace('/', '_').TrimEnd('=')
     };
     return id;
 }
Ejemplo n.º 7
0
 protected virtual string Encrypt(string input)
 {
     MD5 md5 = new MD5Cng();
     byte[] buffer = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
     StringBuilder sb = new StringBuilder();
     foreach (var b in buffer)
     {
         sb.Append(b.ToString("x2"));
     }
     return sb.ToString();
 }
Ejemplo n.º 8
0
 static void Main(string[] args)
 {
     rnd = new Random(12445324);
     chunks = RandomSequence(rnd, 6).Take(1000).ToList();
     var flag = "mars0_" + chunks[42] + chunks[13] + chunks[210];
     var md5 = new MD5Cng();
     md5Hash = md5.ComputeHash(Encoding.ASCII.GetBytes(flag)).Select(b => b.ToString("x2")).Aggregate("", (s, c) => s + c);
     Console.WriteLine(flag);
     MakeImage(s => s).Save(@"..\..\english.png", ImageFormat.Png);
     MakeImage(TranslateExtensions.ToMars).Save(@"..\..\mars.png", ImageFormat.Png);
 }
Ejemplo n.º 9
0
		public static String MD5HashString(this byte[] data)
		{
			if (data == null)
			{
				throw new ArgumentNullException("data");
			}

			using (var md5 = new MD5Cng())
			{
			    var hash = md5.ComputeHash(data);
			    return BitConverter.ToString(hash).Replace("-", "");
			}
		}
Ejemplo n.º 10
0
        public async Task<Models.File> Upload()
        {
            // Set the file stream postion to the start of the stream
            File.InputStream.Position = 0;

            // Create extension, unique key and file hash variables, and reset position after reading stream
            var extension = Path.GetExtension(File.FileName);
            var key = string.Format("File-{0}{1}", Guid.NewGuid(), extension);
            var hash = new MD5Cng().ComputeHash(File.InputStream);
            File.InputStream.Position = 0;

            // Check if the file already exists in the Database using the hash; Return the existing file object if the file exists already
            if (_ctx.Files.Any(m => m.Md5 == hash && m.Container == Container))
            {
                return _ctx.Files.Single(m => m.Md5 == hash && m.Container == Container);
            }

            // Get / create the blob container reference
            _container = _blobClient.GetContainerReference(Container);
            _container.CreateIfNotExists();
            _container.SetPermissions(
                new BlobContainerPermissions
                {
                    PublicAccess = BlobContainerPublicAccessType.Blob
                });

            // Create new database object to reference the blob from
            var fileEntity = new Models.File
            {
                Name = File.FileName.Replace(extension ?? "", ""),
                ContentLength = File.ContentLength,
                ContentType = File.ContentType,
                Key = key,
                Container = Container,
                Md5 = hash  
            };

            _ctx.Files.Add(fileEntity);
            await _ctx.SaveChangesAsync();

            // Upload original file
            var block = _container.GetBlockBlobReference(fileEntity.Key);
            block.Properties.ContentType = fileEntity.ContentType;
            await block.UploadFromStreamAsync(File.InputStream);

            // Upload resized image versions
            await UploadImageResized(fileEntity);

            return fileEntity;
        }
Ejemplo n.º 11
0
 public void FindSolution()
 {
     // То что распознал OneNote2013:
     var chunks = new[] { 111410, 341917, 137859, 113920, 119827, 266241, 160397, 512761, 101522, 186211, 484741, 772834, 781347, 473633, 535009, 921713, 183358, 553933, 172883, 162839, 100596, 193372, 135023, 100102, 594017, 314908, 137820, 631989, 318188, 535193, 449865, 109065, 771288, 112489, 172121, 136240, 157843, 163956, 192449, 114467, 189369, 989983, 233773, 194102, 159592, 108460, 146484, 680203, 610662, 231601, 203711, 148638, 109458, 183115, 592895, 437058, 189600, 194466, 582587, 194548, 886543, 199610, 153574, 103681, 170805, 904959, 604197, 276558, 580140, 105004, 931407, 153431, 856152, 698504, 874531, 192780, 202709, 220101, 169099, 120159, 235744, 119364, 117852, 206279, 116243, 225340, 142729, 137909, 883960, 188613, 854975, 132099, 345823, 160736, 856880, 977750, 167645, 636614, 204505, 898228, 157849, 169289, 125505, 850470, 204287, 550100, 134812, 115886, 133608, 166766, 130686, 179950, 803727, 191415, 119332, 948348, 124739, 145574, 147973, 147454, 115570, 101408, 149971, 113995, 837238, 162286, 192773, 102534, 384096, 733051, 152603, 814296, 188524, 355787, 940840, 168355, 649190, 500507, 648007, 148362, 195234, 346618, 106223, 847820, 171864, 120591, 184682, 696370, 790680, 148152, 213321, 307636, 130956, 659392, 164283, 171196, 306780, 177710, 517587, 144801, 377385, 173885, 192647, 889169, 148838, 180462, 194804, 208854, 107520, 148903, 134762, 205682, 108560, 935446, 167852, 818096, 170559, 107442, 222055, 107165, 746684, 209599, 341405, 127631, 114392, 199113, 130116, 166057, 142722, 377337, 104795, 928191, 117941, 189954, 103742, 784623, 188241, 149887, 210030, 185731, 131396, 149789, 164470, 118454, 996297, 135246, 667747, 899133, 555058, 178782, 149014, 110413, 394364, 528649, 420392, 725041, 198569, 191140, 365722, 701898, 117603, 100393, 468046, 653245, 991023, 129580, 230800, 161612, 185802, 822295, 108251, 196682, 211485, 855586, 184286, 872061, 147941, 166441, 450164, 179468, 199220, 236956, 196597, 916584, 589345, 238892, 359300, 464187, 194977, 170775, 171739, 121085, 169754, 143096, 214235, 903245, 183244, 777946, 983276, 943813, 192152, 367164, 209826, 110352, 111605, 764148, 271255, 151425, 242752, 533411, 200064, 115023, 138975, 636093, 188860, 168510, 127207, 494241, 369574, 533831, 169091, 176687, 135669, 194131, 169897, 423759, 100490, 121100, 262640, 479947 };
     foreach (var c1 in chunks)
         foreach (var c2 in chunks)
             foreach (var c3 in chunks)
             {
                 var s = "mars0_" + c1 + c2 + c3;
                 var hash = new MD5Cng().ComputeHash(Encoding.ASCII.GetBytes(s)).Select(b => b.ToString("x2")).Aggregate("", (ss, c) => ss + c);
                 if (hash.StartsWith("73f48ba"))
                     Console.WriteLine(s + " " + hash);
             }
 }
Ejemplo n.º 12
0
        /// <summary>
        /// compute the hash value of a file associated with the specified filePath
        /// </summary>
        /// <returns>null, if the file does not exist</returns>
        public static string GetFileHash(string filePath)
        {
            Contract.Requires(!PathHelper.IsInvalidPath(filePath));
            if (!File.Exists(filePath))
                return null;

            byte[] hash;
            using (var cng = new MD5Cng())//for performance
            {
                using (var stream = new FileStream(filePath, FileMode.Open))
                {
                    hash = cng.ComputeHash(stream);
                }
            }
            return Convert.ToBase64String(hash);
        }
Ejemplo n.º 13
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;
        }
Ejemplo n.º 14
0
        public static string ComputeMD5Hash(string fileName)
        {
            string md5 = null;

            try
            {
                using (FileStream stream = File.OpenRead(fileName))
                {
                    using (var bufferedStream = new BufferedStream(stream, 1024 * 32))
                    {
                        using (var sha = new MD5Cng())
                        {
                            byte[] checksum = sha.ComputeHash(bufferedStream);
                            md5 = BitConverter.ToString(checksum).Replace("-", string.Empty);
                        }
                    }
                }
            }
            catch (IOException) { }
            catch (UnauthorizedAccessException) { }
            return md5;
        }
Ejemplo n.º 15
0
        public static bool IsMd5Valid(string messageBody, string expectedMd5)
        {
            if (messageBody == null)
            {
                throw new ArgumentNullException("messageBody");
            }

            // Define the hash algorithm to use
            byte[] hashBytes;
            using (var hash = new MD5Cng())
            {
                hashBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(messageBody));
                hash.Clear();
            }

            var messageBodyMd5 = hashBytes.ToHex();
            if (messageBodyMd5 == expectedMd5)
            {
                return true;
            }

            return false;
        }
Ejemplo n.º 16
0
        /// <summary>
        /// MD5Cng
        /// 此类型的任何公共static成员都是线程安全的。但不保证所有实例成员都是线程安全的
        /// </summary>
        private static string Encrypt_static(string clearText, Encoding encode)
        {
            MD5 md5 = null;
            try
            {
                byte[] originalBytes = encode.GetBytes(clearText); //Encoding.UTF8.GetBytes(clearText);

                md5 = new MD5Cng();
                byte[] data = md5.ComputeHash(originalBytes);

                StringBuilder builder = new StringBuilder();
                foreach (var item in data)
                {
                    builder.Append(item.ToString("X2"));
                }

                return builder.ToString();
            }
            catch (ArgumentNullException) { return clearText; }
            catch (EncoderFallbackException) { return clearText; }
            //catch (TargetInvocationException) { return clearText; }
            catch (ObjectDisposedException) { return clearText; }
            catch (InvalidOperationException) { return clearText; }

            catch { return clearText; }
            finally
            {
                if (md5 != null)
                {
                    md5.Clear();
                    md5.Dispose();
                }

                md5 = null;
            }
        }
Ejemplo n.º 17
0
 /// <summary>
 /// Encrypt the password.
 /// </summary>
 /// <param name="password">The password.</param>
 /// <returns>The encrypted password.</returns>
 private static string EncryptPassword(string password)
 {
     var passwordBytes = Encoding.Unicode.GetBytes(password);
     var md5Bytes = new MD5Cng().ComputeHash(passwordBytes);
     return Encoding.Unicode.GetString(md5Bytes);
 }
Ejemplo n.º 18
0
 public static string ToMD5(this string text)
 {
     var md5Bytes = new MD5Cng().ComputeHash(Encoding.UTF8.GetBytes(text));
     return BitConverter.ToString(md5Bytes).Replace("-", "").ToLower();
 }
Ejemplo n.º 19
0
        /// <summary>
        ///     Compute the MD5 from a file.
        /// </summary>
        /// <param name="file">Path to the file</param>
        /// <returns>MD5 as 32 characters long hex-string</returns>
        public static string MD5(string file)
        {
            byte[] hash;
            var sBuilder = new StringBuilder();

            using (var sr = new StreamReader(file))
            {
                var sha = new MD5Cng();
                hash = sha.ComputeHash(sr.BaseStream);
            }

            foreach (var t in hash)
                sBuilder.Append(t.ToString("x2"));

            return sBuilder.ToString();
        }
Ejemplo n.º 20
0
        /// <summary>
        ///     Compute the MD5 from a byte array.
        /// </summary>
        /// <param name="buff">Binary as a byte buffer.</param>
        /// <returns>MD5 as 32 characters long hex-string</returns>
        public static string MD5(byte[] buff)
        {
            var sBuilder = new StringBuilder();

            var sha = new MD5Cng();
            var hash = sha.ComputeHash(buff);

            foreach (var t in hash)
                sBuilder.Append(t.ToString("x2"));

            return sBuilder.ToString();
        }
Ejemplo n.º 21
0
            public string GetGravatarHash(string email)
            {
                Requires.NotNullOrEmpty(email, "email");

                MD5 m = new MD5Cng();
                return BitConverter.ToString(
                    m.ComputeHash(Encoding.UTF8.GetBytes(email.Trim().ToLower())))
                    .Replace("-", "")
                    .ToLower();
            }
Ejemplo n.º 22
0
 /// <summary>
 /// Gets the md5 hash of the file.
 /// </summary>
 /// <returns></returns>
 public byte[] GetMD5()
 {
     if (md5 == null)
     {
         md5 = new MD5Cng().ComputeHash(FileStream);
         FileStream.Position = 0;
     }
     
     return md5;
 }
Ejemplo n.º 23
0
 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);
 }