예제 #1
0
        public bool IsInStore(string password, StoreType storeType)
        {
            if (storeType == StoreType.Word)
            {
                password = StringNormalizer.Normalize(password);
            }

            return(this.IsInStore(this.ComputeHash(password), storeType));
        }
예제 #2
0
        public void AddToStore(string password, StoreType storeType)
        {
            if (storeType == StoreType.Word)
            {
                password = StringNormalizer.Normalize(password);
            }

            this.AddToStore(this.ComputeHash(password), storeType);
        }
예제 #3
0
        public static void ImportPasswordsFromFile(Store store, StoreType storeType, string sourceFile, CancellationToken ct, int batchSize = DefaultBatchSize, OperationProgress progress = null)
        {
            bool batched = false;

            if (progress == null)
            {
                progress = new OperationProgress();
            }

            if (batchSize < 0)
            {
                batchSize = DefaultBatchSize;
            }

            progress.Status = $"Loading plain-text passwords from {sourceFile}";

            Dictionary <string, HashSet <byte[]> > importData = new Dictionary <string, HashSet <byte[]> >(store.HashOffset ^ 16);

            int currentCount = 0;

            foreach (string line in GetLinesFromFile(sourceFile, progress))
            {
                ct.ThrowIfCancellationRequested();

                if (line.Length <= 0)
                {
                    continue;
                }
                progress.Status = "Reading lines from file";

                byte[] hash = null;
                progress?.IncrementTotalProcessed();

                if (storeType == StoreType.Word)
                {
                    hash = store.ComputeHash(StringNormalizer.Normalize(line));
                }
                else
                {
                    hash = store.ComputeHash(line);
                }

                string range = store.GetRangeFromHash(hash);

                if (!importData.TryGetValue(range, out HashSet <byte[]> rangeStore))
                {
                    rangeStore = new HashSet <byte[]>(ByteArrayComparer.Comparer);
                    importData.Add(range, rangeStore);
                }

                if (!rangeStore.Add(hash))
                {
                    progress?.IncrementHashesDiscarded();
                }
                else
                {
                    currentCount++;
                }

                if (batchSize > 0 && currentCount >= batchSize)
                {
                    if (!batched)
                    {
                        batched = true;
                        store.StartBatch(storeType);
                    }

                    progress.Status = "Flushing batch to store";
                    store.AddToStore(importData, storeType, ct, true, progress);
                    currentCount = 0;
                }
            }

            if (currentCount > 0)
            {
                progress.Status = "Flushing batch to store";
                store.AddToStore(importData, storeType, ct, true, progress);
            }

            if (batched)
            {
                progress.Status = "Sorting hashes into store";
                store.EndBatch(storeType, ct, progress);
            }

            progress.Status = "Done";
        }