I am looking for advice on the neatest way to structure my code.
class Password
{
private string cipher;
Password(string cipher)
{
this.cipher = cipher;
}
public string HashPassword()
{
}
public bool VerifyPassword()
{
}
}
The idea behind the class is to abstract away the password hashing process. The class will present two functions to the user, HashPassword()
and VerifyPassword()
, which will perform completely different hashing algorithms depending on the value of the cipher
variable.
Currently, I am using a set of if/else
conditional statements within the two functions itself to determine which hashing algorithm to perform. However, it felt quite inelegant to me.
Is there a better way of structuring my code?
2
Have a base class – PasswordHasher
or such that will be abstract (or, if you find that implementation vary too much for implementations to share anything, use an interface).
From it, inherit a class for each hash algorithm you are going to use.
Using dependency injection, you can use either algorithm by simply passing in the relevant class.