Esempio n. 1
0
        public List <WordCountDto> List()
        {
            var wordCounts = _dbContext.Set <WordCount>().ToList();
            var rsa        = CryptographicFunctions.GetKeyFromContainer(WordCounterConstants.KeyContainerName);

            return(wordCounts.Select(wordCount => new WordCountDto
            {
                Word = CryptographicFunctions.Decrypt(rsa.ToXMLString(true), wordCount.EncryptedWord),
                Count = wordCount.Count
            })
                   .ToList());
        }
Esempio n. 2
0
        public void StoreWordCounts(List <KeyValuePair <string, int> > countedWords, int numberOfItemsToStore)
        {
            if (countedWords == null || !countedWords.Any())
            {
                return;
            }

            // Create an instance of the RSA algorithm class
            var rsa = CryptographicFunctions.GetKeyFromContainer(WordCounterConstants.KeyContainerName);
            // Get the public keyy
            var publicKey = rsa.ToXMLString(false); // false to get the public key
            var someSalt  = "somesalt";

            if (numberOfItemsToStore > countedWords.Count)
            {
                numberOfItemsToStore = countedWords.Count;
            }

            for (var i = 0; i < numberOfItemsToStore; i++)
            {
                var countedWord   = countedWords[i];
                var keyAsBytes    = Encoding.UTF32.GetBytes(countedWord.Key);
                var saltAsBytes   = Encoding.UTF32.GetBytes(someSalt);
                var hashedWord    = CryptographicFunctions.GenerateSaltedHash(keyAsBytes, saltAsBytes);
                var encryptedWord = CryptographicFunctions.Encrypt(publicKey, countedWord.Key);

                var wordCount = new WordCount
                {
                    Id            = hashedWord,
                    EncryptedWord = encryptedWord,
                    Count         = countedWord.Value
                };

                try
                {
                    AddOrUpdateWordCount(wordCount);
                }
                catch (Exception e)
                {
                    _logger.LogError(e.Message, null);
                }
            }
        }