18 de junio de 2015

Uso de BCrypt y Salt C#

Para mostrar el uso de se utilizara este formulario




el código sera el siguiente
private void button1_Click(object sender, EventArgs e){
 string passwordAverificar = Convert.ToString(textBox1.Text.Trim());
  if (string.IsNullOrEmpty(passwordAverificar))
   {
    MessageBox.Show("Especificar password");
   }
   else
   {
    string salt= createSalt();
  if (string.IsNullOrEmpty(salt))
   {
    MessageBox.Show("No pudo crearse la SALT");
   }
   else
   {
    string passwordHash = createHash(passwordAverificar, salt);
    label1.Text = passwordHash;
   }
  }
 }

private string createSalt() { 
 string mySalt = BCryptHelper.GenerateSalt(8);
 return mySalt;
       }

private string createHash(string password, string salt){
 string passwordV = password+"yxy";
 string myHash = BCryptHelper.HashPassword(passwordV, salt);
 return myHash;
       }

private void button2_Click(object sender, EventArgs e){
 string password2 = textBox2.Text.Trim();
 string passwordV2 = password2+"yxy";
 bool verificar = DoesPasswordMatch(label1.Text, passwordV2);
  if (verificar)
  {
  label2.Text = "exito";
  }
  else {
  label2.Text = "Fail";
       }
  }

private bool DoesPasswordMatch(string hashedPwdFromDatabase, string userEnteredPassword){
 return BCryptHelper.CheckPassword(userEnteredPassword, hashedPwdFromDatabase);
     }