Beispiel #1
0
        public static void Main(string[] args)
        {
            /*
            string user_salt = "NyfLZ6RJXWE1aHrrM5JRefMlipdBV0bCp";
            string password = "******";
            var pbkdf2 = new PBKDF2<HMACSHA512>(Encoding.ASCII.GetBytes(password), Encoding.ASCII.GetBytes(user_salt), 50);
            var bytes = pbkdf2.GetBytes(128);

            Console.WriteLine(String.Join("", bytes.Select(x => x.ToString("X2"))));
            return;
             */

            Console.WriteLine(PasswordHasher.GenerateSalt());

            int iterations = 0;
            int memoryBits = 22;

            var chasher = new PasswordHasher("Qq48KGoFOXbZcBXDHZuqyjTP5oBfUy4N2iEHmL2NkIw=", memoryBits, iterations);

            var startTime = DateTime.UtcNow;
            string hash = chasher.Hash("foo1", "NyfLZ6RJXWE1aHrrM5JRefMlipdBV0bCp");
            Console.WriteLine((DateTime.UtcNow - startTime).TotalMilliseconds);
            Console.WriteLine(hash);
            Console.WriteLine(chasher.MemoryUsage);

            var hasher = new TestPasswordHasher("Qq48KGoFOXbZcBXDHZuqyjTP5oBfUy4N2iEHmL2NkIw=", memoryBits, iterations);

            startTime = DateTime.UtcNow;
            hash = hasher.Hash("foo1", "NyfLZ6RJXWE1aHrrM5JRefMlipdBV0bCp");
            Console.WriteLine((DateTime.UtcNow - startTime).TotalMilliseconds);
            Console.WriteLine(hash);
            Console.WriteLine(hasher.MemoryUsage);
            #if DEBUG
            /* cell frequency */
            if (!Directory.Exists("stats"))
                Directory.CreateDirectory("stats");

            CellFrequency(String.Format("cellfreq-{0}-{1}.txt", memoryBits, iterations), hasher.__hashArray);

            /* xored cell frequency */
            byte[] xored = new byte[hasher.MemoryUsage];

            for (int i = 0; i < xored.Length; i++)
                xored[i] = (byte)(hasher.__hashArray[i] ^ hasher._originalArray[i]);

            Console.WriteLine("0 xor bytes: " + xored.Count(x => x == 0));

            CellFrequency(String.Format("xor-cellfreq-{0}-{1}.txt", memoryBits, iterations), xored);

            /* visit counts */
            var chars = "0123456789abcdefghijklmnopqrstuvwxyz".ToCharArray();
            System.IO.File.WriteAllText(String.Format(@"stats\hash-{0}-{1}.txt", memoryBits, iterations), String.Join("", hasher._visitCounts.Select(v => chars[v])));

            /* depth, count, iterations */
            _mixingHashes = hasher._hashes.Skip(1);

            string prevHash = hasher._hashes[0];

            _info[prevHash] = new HashTreeInfo
            {
                depth = 1,
                count = 0,
                iterations = 1
            };

            foreach(string nextHash in _mixingHashes)
            {
                _prevHash[nextHash] = prevHash;
                prevHash = nextHash;
            }

            var depths = _mixingHashes.Select(h => GetTreeInfo(hasher, h).ToString());
            System.IO.File.WriteAllLines(String.Format(@"stats\depths-{0}-{1}.txt", memoryBits, iterations), depths);
            #endif
            Console.ReadLine();
        }
Beispiel #2
0
        private static void VisitCounts()
        {
            var chars = "0123456789abcdefghijklmnopqrstuvwxyz".ToCharArray();

            var hasher = new TestPasswordHasher("Qq48KGoFOXbZcBXDHZuqyjTP5oBfUy4N2iEHmL2NkIw=", 12288, 4);

            DateTime startTime = DateTime.UtcNow;
            hasher.Hash("foo", "");
            Console.WriteLine((DateTime.UtcNow - startTime).TotalMilliseconds);

            System.IO.File.WriteAllText(@"stats\hash-12288-16.txt", String.Join("", hasher._visitCounts.Select(v => chars[v])));
        }
Beispiel #3
0
        private static void ByteFrequency()
        {
            var hasher = new TestPasswordHasher("Qq48KGoFOXbZcBXDHZuqyjTP5oBfUy4N2iEHmL2NkIw=", 65536 + 32768, 4);
            Console.WriteLine(hasher.MemoryUsage);
            //Console.ReadLine();

            using (var rng = new RNGCryptoServiceProvider())
            {
                var randomBytes = new byte[4096 * 16 * 512 / 8];
                rng.GetBytes(randomBytes);
                ByteFrequency("randomfreq.txt", randomBytes);
                ByteFrequency("hashfreq.txt", hasher.__hashArray);
            }
        }
Beispiel #4
0
        private static HashTreeInfo GetTreeInfo(TestPasswordHasher hasher, string hash)
        {
            if (_info.ContainsKey(hash))
                return _info[hash];

            HashTreeInfo info = new HashTreeInfo { depth = 0, count = 0, iterations = _info[_prevHash[hash]].iterations + 1 };

            foreach (int index in hasher._hashToCells[hash])
            {
                IList<string> cellHashes = hasher._hashesPerCell[index];

                for (int i = 0; i < cellHashes.Count && cellHashes[i] != hash; i++ )
                {
                    HashTreeInfo childInfo = GetTreeInfo(hasher, cellHashes[i]);

                    if (childInfo.depth > info.depth)
                        info.depth = childInfo.depth;

                    info.count += childInfo.count + 1;
                    info.iterations += childInfo.iterations;
                }
            }

            info.depth++;
            _info[hash] = info;
            return info;
        }
Beispiel #5
0
        private static void Test()
        {
            var hasher1 = new TestPasswordHasher("Qq48KGoFOXbZcBXDHZuqyjTP5oBfUy4N2iEHmL2NkIw=");
            var hasher2 = new TestPasswordHasher("Qq48KGoFOXbZcBXDHZuqyjTP5oafUy4N2iEHmL2NkIw=");
            var hasher3 = new TestPasswordHasher("Qq48KGoFOXbZcBXDHZuqyjTP5oBfUy4N2iEHmL2NkIw=", 17001);
            //Console.WriteLine(PasswordHasher.GenerateSalt());

            var startTime = DateTime.UtcNow;
            var result1a = hasher1.Hash("password1", "NyfLZ6RJXWE1aHrrM5JRefMlipdBV0bCp");
            Console.WriteLine((DateTime.UtcNow - startTime).TotalMilliseconds);

            var result1b = hasher1.Hash("password1", "NyfLZ6RJXWE1aHrrM5JRefMlipdBV0bCp");
            var result1c = hasher1.Hash("password1", "NyfLZ6RJXWE1aHrrM5JRefMlipdBV0bCq");
            var result1d = hasher1.Hash("password2", "NyfLZ6RJXWE1aHrrM5JRefMlipdBV0bCp");
            var result2  = hasher2.Hash("password1", "NyfLZ6RJXWE1aHrrM5JRefMlipdBV0bCp");
            var result3  = hasher3.Hash("password1", "NyfLZ6RJXWE1aHrrM5JRefMlipdBV0bCp");

            Console.WriteLine(result1a);
            Console.WriteLine(result1b);
            Console.WriteLine();
            Console.WriteLine(result1a);
            Console.WriteLine(result1c);
            Console.WriteLine();
            Console.WriteLine(result1a);
            Console.WriteLine(result1d);
            Console.WriteLine();
            Console.WriteLine(result1a);
            Console.WriteLine(result2);
            Console.WriteLine();
            Console.WriteLine(result1a);
            Console.WriteLine(result3);
            Console.WriteLine();
        }