Example #1
0
        public static HashContrasena GetHash(string password)
        {
            // Hasheamos la contraseña
            string contrasenaHasheada = "";
            string sal = "";

            using (var Sha256 = SHA256.Create())
            {
                // Generamos la sal
                RandomNumberGenerator rng = RNGCryptoServiceProvider.Create();
                byte[] randomBytes        = new byte[16];
                rng.GetBytes(randomBytes);
                sal = HashContrasena.bytes2string(randomBytes);
                rng.Dispose();

                // Hasheamos la contraseña
                byte[] hash = new byte[32];
                System.Text.Encoding enc = Encoding.UTF8;
                hash = Sha256.ComputeHash(enc.GetBytes(sal + password));
                contrasenaHasheada = HashContrasena.bytes2string(hash);
            }

            return(new HashContrasena()
            {
                Contrasena = contrasenaHasheada,
                Sal = sal
            });
        }
Example #2
0
        public static bool CheckPassword(string password, string hash)
        {
            using (var Sha256 = SHA256.Create())
            {
                // Hasheamos la contraseña
                byte[] hashBytes         = new byte[32];
                System.Text.Encoding enc = Encoding.UTF8;
                hashBytes = Sha256.ComputeHash(enc.GetBytes(password));
                string contrasenaHasheada = HashContrasena.bytes2string(hashBytes);

                return(hash.Equals(contrasenaHasheada));
            }
        }