Esempio n. 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();
        }
Esempio n. 2
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;
        }