Ejemplo n.º 1
0
        public async Task <ActionResult> PostUser(string tenantId, [FromBody] UserDTO userDTO)
        {
            if (await _tenantRepository.GetById(Guid.Parse(tenantId)) == null)
            {
                return(BadRequest("Tenant id is not valid"));
            }

            if (ModelState.IsValid)
            {
                User userToBeAdded = await _userRepository.FirstOrDefault(user => user.Email == userDTO.Email);

                if (userToBeAdded == null)
                {
                    await _userRepository.Add(new User {
                        Id       = new Guid(),
                        Username = userDTO.Username,
                        Email    = userDTO.Email,
                        Role     = userDTO.UserRole,
                        Password = _encryptorDecryptor.Encrypt(userDTO.Password),
                        TenantId = Guid.Parse(tenantId)
                    });

                    return(Ok("User added successfully"));
                }
                else
                {
                    return(Ok("User account already exists"));
                }
            }
            else
            {
                return(BadRequest("Please check if all the field values are provided"));
            }
        }
Ejemplo n.º 2
0
        void PasswordEncryptorDecryptor()
        {
            Console.WriteLine("Please enter a passphrase to use:");
            string password = Console.ReadLine();

            Console.WriteLine("Please enter your string to encrypt:");
            string plaintext = Console.ReadLine();

            Console.WriteLine("");

            Console.WriteLine("Your encrypted string is:");
            string encryptedstring = EncryptorDecryptor.Encrypt(plaintext, password);

            Console.WriteLine(encryptedstring);
            Console.WriteLine("");

            Console.WriteLine("Your decrypted string is:");
            string decryptedstring = EncryptorDecryptor.Decrypt(encryptedstring, password);

            Console.WriteLine(decryptedstring);
            Console.WriteLine("");

            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }
Ejemplo n.º 3
0
        public void TestEncryptionOfString()
        {
            string testSample = "this text is decrypted ok";
            string result     = EncryptorDecryptor.Encrypt(testSample);

            result = EncryptorDecryptor.Decrypt(result);
            Assert.True(result.Equals(testSample), "encryption/decryption failed");
        }
        internal static byte[] EncryptRepository(NoteRepositoryModel repository, string transferCode, ICryptoRandomService randomService, string encryptionAlgorithm)
        {
            byte[]             binaryRepository = XmlUtils.SerializeToXmlBytes(repository);
            EncryptorDecryptor encryptor        = new EncryptorDecryptor("SilentNotes");

            // The key derivation cost is set to low, because we can be sure that the transferCode
            // is a very strong password, and to not overload slow mobile devices.
            return(encryptor.Encrypt(binaryRepository, transferCode, Crypto.KeyDerivation.KeyDerivationCostType.Low, randomService, encryptionAlgorithm));
        }
        private static byte[] CreateEncryptedRepository(string password, NoteRepositoryModel repository = null)
        {
            if (repository == null)
            {
                repository = new NoteRepositoryModel();
            }
            byte[]             serializedRepository = XmlUtils.SerializeToXmlBytes(repository);
            EncryptorDecryptor encryptor            = new EncryptorDecryptor("SilentNotes");

            return(encryptor.Encrypt(serializedRepository, password, SilentNotes.Crypto.KeyDerivation.KeyDerivationCostType.Low, CommonMocksAndStubs.CryptoRandomService(), BouncyCastleTwofishGcm.CryptoAlgorithmName));
        }
Ejemplo n.º 6
0
        public void CryptoTestEncryptor()
        {
            EncryptorDecryptor   encryptor       = new EncryptorDecryptor("sugus");
            ICryptoRandomService randomGenerator = CommonMocksAndStubs.CryptoRandomService();
            string message = "Der schnelle Fuchs stolpert über den faulen Hund.";

            byte[] binaryMessage = CryptoUtils.StringToBytes(message);
            string password      = "******";

            byte[] cipher           = encryptor.Encrypt(binaryMessage, password, KeyDerivationCostType.Low, randomGenerator, BouncyCastleTwofishGcm.CryptoAlgorithmName);
            byte[] decryptedMessage = encryptor.Decrypt(cipher, password);
            Assert.AreEqual(binaryMessage, decryptedMessage);
        }
        public async Task <ActionResult> PostLoginUser([FromBody] UserDTO userDTO)
        {
            if (ModelState.IsValid)
            {
                var userAccount = await _userRepository.FirstOrDefault(user => user.Username == userDTO.Username && user.Password == _encryptorDecryptor.Encrypt(userDTO.Password));

                if (userAccount == null)
                {
                    return(BadRequest("User account does not exist"));
                }

                return(Ok(userAccount));
            }
            else
            {
                return(BadRequest("Please make sure credentials are correct"));
            }
        }