Ejemplo n.º 1
0
        public void HashCombiner_Test_File()
        {
            using (var combiner1 = new HashGenerator())
                using (var combiner2 = new HashGenerator())
                    using (var combiner3 = new HashGenerator())
                    {
                        var dir       = PrepareFolder();
                        var file1Path = Path.Combine(dir.FullName, "hastest1.txt");
                        File.Delete(file1Path);
                        using (var file1 = File.CreateText(Path.Combine(dir.FullName, "hastest1.txt")))
                        {
                            file1.WriteLine("hello");
                        }
                        var file2Path = Path.Combine(dir.FullName, "hastest2.txt");
                        File.Delete(file2Path);
                        using (var file2 = File.CreateText(Path.Combine(dir.FullName, "hastest2.txt")))
                        {
                            //even though files are the same, the dates are different
                            file2.WriteLine("hello");
                        }

                        combiner1.AddFile(new FileInfo(file1Path));

                        combiner2.AddFile(new FileInfo(file1Path));

                        combiner3.AddFile(new FileInfo(file2Path));

                        Assert.AreEqual(combiner1.GenerateHash(), combiner2.GenerateHash());
                        Assert.AreNotEqual(combiner1.GenerateHash(), combiner3.GenerateHash());

                        combiner2.AddFile(new FileInfo(file2Path));

                        Assert.AreNotEqual(combiner1.GenerateHash(), combiner2.GenerateHash());
                    }
        }
Ejemplo n.º 2
0
        public void HashCombiner_Test_Folder()
        {
            using var combiner1 = new HashGenerator();
            using var combiner2 = new HashGenerator();
            using var combiner3 = new HashGenerator();
            DirectoryInfo dir       = PrepareFolder();
            var           file1Path = Path.Combine(dir.FullName, "hastest1.txt");

            File.Delete(file1Path);
            using (StreamWriter file1 = File.CreateText(Path.Combine(dir.FullName, "hastest1.txt")))
            {
                file1.WriteLine("hello");
            }

            // first test the whole folder
            combiner1.AddFolder(dir);

            combiner2.AddFolder(dir);

            Assert.AreEqual(combiner1.GenerateHash(), combiner2.GenerateHash());

            // now add a file to the folder
            var file2Path = Path.Combine(dir.FullName, "hastest2.txt");

            File.Delete(file2Path);
            using (StreamWriter file2 = File.CreateText(Path.Combine(dir.FullName, "hastest2.txt")))
            {
                // even though files are the same, the dates are different
                file2.WriteLine("hello");
            }

            combiner3.AddFolder(dir);

            Assert.AreNotEqual(combiner1.GenerateHash(), combiner3.GenerateHash());
        }
Ejemplo n.º 3
0
        public void Should_fail_authentication()
        {
            var hashGenerator = new HashGenerator(_kinlyConfiguration);
            var id            = Guid.NewGuid().ToString();
            var computedHash  = hashGenerator.GenerateHash(DateTime.UtcNow.AddMinutes(20), id);

            var id2            = Guid.NewGuid().ToString();
            var reComputedHash = hashGenerator.GenerateHash(DateTime.UtcNow.AddMinutes(-20), id2);

            reComputedHash.Should().NotBe(computedHash);
        }
Ejemplo n.º 4
0
        public void Should_fail_authentication()
        {
            var hashGenerator = new HashGenerator(_customTokenSettings.Secret);
            var id            = Guid.NewGuid().ToString();
            var computedHash  = hashGenerator.GenerateHash(GetExpiryOn(), id);

            var id2            = Guid.NewGuid().ToString();
            var reComputedHash = hashGenerator.GenerateHash(GetExpiryOn(), id2);

            reComputedHash.Should().NotBe(computedHash);
        }
Ejemplo n.º 5
0
 public void HashCombiner_Test_String()
 {
     using (var combiner1 = new HashGenerator())
         using (var combiner2 = new HashGenerator())
         {
             combiner1.AddCaseInsensitiveString("Hello");
             combiner2.AddCaseInsensitiveString("hello");
             Assert.AreEqual(combiner1.GenerateHash(), combiner2.GenerateHash());
             combiner2.AddCaseInsensitiveString("world");
             Assert.AreNotEqual(combiner1.GenerateHash(), combiner2.GenerateHash());
         }
 }
Ejemplo n.º 6
0
 public void HashCombiner_Test_Int()
 {
     using (var combiner1 = new HashGenerator())
         using (var combiner2 = new HashGenerator())
         {
             combiner1.AddInt(1234);
             combiner2.AddInt(1234);
             Assert.AreEqual(combiner1.GenerateHash(), combiner2.GenerateHash());
             combiner2.AddInt(1);
             Assert.AreNotEqual(combiner1.GenerateHash(), combiner2.GenerateHash());
         }
 }
Ejemplo n.º 7
0
        public void HashCombiner_Test_DateTime()
        {
            using var combiner1 = new HashGenerator();
            using var combiner2 = new HashGenerator();
            DateTime dt = DateTime.Now;

            combiner1.AddDateTime(dt);
            combiner2.AddDateTime(dt);
            Assert.AreEqual(combiner1.GenerateHash(), combiner2.GenerateHash());
            combiner2.AddDateTime(DateTime.Now);
            Assert.AreNotEqual(combiner1.GenerateHash(), combiner2.GenerateHash());
        }
Ejemplo n.º 8
0
        public IActionResult Register(RegisterDTO dto)
        {
            string userId = Guid.NewGuid().ToString();
            ApplicationUserRole userRole = new ApplicationUserRole()
            {
                RoleId = ApplicationRolesConst.ApplicationUserRoleId,
                UserId = userId
            };
            List <ApplicationUserRole> LST = new List <ApplicationUserRole>();

            LST.Add(userRole);
            ApplicationUser identityUser = new ApplicationUser()
            {
                Id           = userId,
                UserName     = dto.UserName,
                PasswordHash = HashGenerator.GenerateHash(dto.Password),
                UserRoles    = LST
            };
            var user = _userManager.CreateAsync(identityUser).Result;

            if (user != null)
            {
                return(Ok());
            }
            else
            {
                return(BadRequest());
            }
        }
Ejemplo n.º 9
0
        public Data.Model.User Update(int Id, UserUpsertRequest request)
        {
            var entity = _context.User.Find(Id);

            _context.User.Attach(entity);
            _context.User.Update(entity);

            if (request.Password != request.PasswordConfirm)
            {
                throw new Exception("Password and password confirm not matched");
            }

            if (request.Password != null)
            {
                entity.PasswordSalt = HashGenerator.GenerateSalt();
                entity.PasswordHash = HashGenerator.GenerateHash(entity.PasswordSalt, request.Password);
            }
            else
            {
                _mapper.Map(request, entity);
            }

            _context.SaveChanges();

            return(_mapper.Map <Data.Model.User>(entity));
        }
Ejemplo n.º 10
0
        public Model.Users Register(UserVM request, string password)
        {
            //byte[] passwordHash, passwordSalt;
            //var user = _mapper.Map<Database.Users>(req);

            //CreatePasswordHash(password, out passwordHash, out passwordSalt);

            //user.PasswordHash = passwordHash;
            //user.PasswordSalt = passwordSalt;

            // _context.Users.Add(user);
            // _context.SaveChanges();


            var entity = _mapper.Map <Users>(request);

            if (request.Password != request.PasswordConfirm)
            {
                throw new Exception("Password and password confirm not matched !");
            }
            entity.PasswordSalt = HashGenerator.GenerateSalt();
            entity.PasswordHash = HashGenerator.GenerateHash(entity.PasswordSalt, request.Password);

            _context.Add(entity);
            _context.SaveChanges();

            return(_mapper.Map <Model.Users>(entity));
        }
Ejemplo n.º 11
0
        private static string TryCharacter(int depth, string stub, ulong targetHash)
        {
            foreach (char character in characters)
            //for (byte character = 32; character <= 122; character++)
            {
                string newStub = (stub + (char)character).TrimStart(' ');

                if (depth == MaxDepth)
                {
                    ulong newHash = HashGenerator.GenerateHash(newStub);
                    if (newHash == targetHash)
                    {
                        return(newStub);
                    }
                }
                else
                {
                    string attempt = TryCharacter(depth + 1, newStub, targetHash);
                    if (attempt != null)
                    {
                        return(attempt);
                    }
                }
            }
            return(null);
        }
Ejemplo n.º 12
0
        public void Should_Generate_Hash()
        {
            string expectedHash  = "Cy84UuLZpfGhI7oaPD0Ckx1M0mo=";
            string generatedHash = HashGenerator.GenerateHash("apiKey", "secretKey", "random", new TestRequest());

            Assert.AreEqual(expectedHash, generatedHash);
        }
Ejemplo n.º 13
0
        public async Task <IActionResult> Register([FromBody] User userInfo)
        {
            if (userInfo == null || (userInfo != null && await accountsRepository.UserExistAsync(userInfo.Username)))
            {
                return(BadRequest());
            }

            User result = null;

            userInfo.Password = HashGenerator.GenerateHash(userInfo.Password);
            await accountsRepository.AddUserAsync(userInfo);

            try
            {
                result = await accountsRepository.GetUserAsync(userInfo.Username);
            }
            catch (Exception e)
            {
                var x = e.Message;
            }

            if (result == null)
            {
                return(BadRequest());
            }
            return(Ok(result));
        }
Ejemplo n.º 14
0
        public void Should_encrypt()
        {
            var hashGenerator = new HashGenerator(_kinlyConfiguration);
            var id            = Guid.NewGuid().ToString();
            var computedHash  = hashGenerator.GenerateHash(DateTime.UtcNow.AddMinutes(20), id);

            computedHash.Should().NotBeNullOrEmpty();
        }
Ejemplo n.º 15
0
        public void Should_encrypt()
        {
            var hashGenerator = new HashGenerator(_customTokenSettings.Secret);
            var id            = Guid.NewGuid().ToString();
            var computedHash  = hashGenerator.GenerateHash(GetExpiryOn(), id);

            computedHash.Should().NotBeNullOrEmpty();
        }
Ejemplo n.º 16
0
        private void RegisterUser(RegistrationModel model)
        {
            model.Password = HashGenerator.GenerateHash(model.Password);
            ApplicationUser appUser = mapper.Map <RegistrationModel, ApplicationUser>(model);

            appUser.RoleId = context.Roles.First(r => r.RoleName.Equals("User")).Id;
            context.Users.Add(appUser);
            context.SaveChanges();
        }
 public static string GetServerHash(string machineName, string appDomainAppId)
 {
     using (var generator = new HashGenerator())
     {
         generator.AddString(machineName);
         generator.AddString(appDomainAppId);
         return(generator.GenerateHash());
     }
 }
        public void GenerateHash_EmptyString()
        {
            string inputString = "";

            string expected = "a2582a3a0e66e6e86e3812dcb672a272";

            string actual = HashGenerator.GenerateHash(inputString);

            Assert.AreEqual(expected, actual);
        }
        public void GenerateHash_1_2_4()
        {
            string inputString = "1,2,4";

            string expected = "63960835bcdc130f0b66d7ff4f6a5a8e";

            string actual = HashGenerator.GenerateHash(inputString);

            Assert.AreEqual(expected, actual);
        }
        public void GenerateHash_1_2_3()
        {
            string inputString = "1,2,3";

            string expected = "3efbe78a8d82f29979031a4aa0b16a9d";

            string actual = HashGenerator.GenerateHash(inputString);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 21
0
        public ulong Add(string name)
        {
            ulong hash = HashGenerator.GenerateHash(name);

            if (!string.IsNullOrEmpty(name) && !ids.ContainsValue(name))
            {
                ids.Add(hash, name);
            }
            return(hash);
        }
        public void GenerateHash_AoC_2017()
        {
            string inputString = "AoC 2017";

            string expected = "33efeb34ea91902bb2f59c9920caa6cd";

            string actual = HashGenerator.GenerateHash(inputString);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 23
0
 protected override PECMailLog BeforeCreate(PECMailLog entity)
 {
     if (entity.PECMail != null)
     {
         entity.PECMail = _unitOfWork.Repository <PECMail>().Find(entity.PECMail.EntityId);
     }
     entity.LogDate        = DateTime.UtcNow;
     entity.SystemComputer = Environment.MachineName;
     entity.Hash           = HashGenerator.GenerateHash(string.Concat(entity.RegistrationUser, "|", entity.SystemComputer, "|", entity.LogType, "|", entity.Description, "|", entity.UniqueId, "|", entity.PECMail.EntityId, "|", entity.LogDate.ToString("yyyyMMddHHmmss")));
     return(base.BeforeCreate(entity));
 }
Ejemplo n.º 24
0
        private void TryToLogin(LoginModel model)
        {
            model.Password = HashGenerator.GenerateHash(model.Password);
            var appUser = context.Users.FirstOrDefault(u => u.Username.Equals(model.Username) && u.PasswordHash.Equals(model.Password));

            if (appUser == null)
            {
                MessageBox.Show("Invalid credentials");
                return;
            }
            HandleSuccessfullLogin(appUser);
        }
Ejemplo n.º 25
0
        public void Should_Generate_Hash_When_Request_Body_Null()
        {
            //given
            var expectedSignature = "/IoWMs1tQ329eEC4XoDwQAhYc4eHZKV8bdFYNhrL/BQ=";

            //when
            var signature = HashGenerator.GenerateHash("https://api.craftgate.io", "api-key", "secret-key",
                                                       "rand-2010",
                                                       null, "/onboarding/v1/members");

            //then
            Assert.AreEqual(expectedSignature, signature);
        }
        public Model.Users Login(string email, string password)
        {
            Database.Users user = _context.Users.FirstOrDefault(e => e.Email == email);
            if (user != null)
            {
                var newHash = HashGenerator.GenerateHash(user.PasswordSalt, password);

                if (newHash == user.PasswordHash)
                {
                    return(_mapper.Map <Model.Users>(user));
                }
            }
            return(null);
        }
Ejemplo n.º 27
0
        [HttpPost] // Login
        public IActionResult Login(UserDTO dto)
        {
            var user = _userManager.FindByNameAsync(dto.Username).Result;

            if (user.PasswordHash == HashGenerator.GenerateHash(dto.Password))
            {
                string token = TokenGenerator.GenerateEncodedToken(user);
                return(Ok(token));
            }
            else
            {
                return(BadRequest());
            }
        }
Ejemplo n.º 28
0
 public void CreatPECMailLog(PECMail pecMail, string logType, string logDescription)
 {
     PECMailLog pecMailLog = new PECMailLog()
     {
         LogDate = DateTime.UtcNow,
         LogType = logType,
         Description = logDescription,
         SystemComputer = Environment.MachineName,
         PECMail = pecMail,
         RegistrationUser = CurrentDomainUser.Account,
     };
     pecMailLog.Hash = HashGenerator.GenerateHash(string.Concat(pecMailLog.RegistrationUser, "|", pecMailLog.SystemComputer, "|", pecMailLog.LogType, "|", pecMailLog.Description, "|", pecMailLog.UniqueId, "|", pecMailLog.PECMail.EntityId, "|", pecMailLog.LogDate.ToString("yyyyMMddHHmmss")));
     _unitOfWork.Repository<PECMailLog>().Insert(pecMailLog);
 }
Ejemplo n.º 29
0
        public eDentalClinic.Model.User Authenticate(string username, string password)
        {
            var user = _context.Users.Include("UserRoles.Role").FirstOrDefault(x => x.Username == username);

            if (user != null)
            {
                var newHash = HashGenerator.GenerateHash(user.PasswordSalt, password);
                if (newHash == user.PasswordHash)
                {
                    return(_mapper.Map <eDentalClinic.Model.User>(user));
                }
            }
            return(null);
        }
Ejemplo n.º 30
0
        public void CanMatchStrings()
        {
            // arrange
            var stringToHash  = "StringToHash";
            var hashGenerator = new HashGenerator();

            // act
            var hash    = hashGenerator.GenerateHash(stringToHash);
            var isMatch = hashGenerator.CompareHash(hash, stringToHash);

            // assert
            Assert.AreNotEqual(hash, stringToHash);
            Assert.IsTrue(isMatch);
        }