Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

closed as off topic by Wyatt Barnett, Walter, Telastyn, Glenn Nelson, MichaelT Feb 24 '13 at 14:48

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

    
there is a codereview.SE for this –  ratchet freak Feb 24 '13 at 14:14
1  
@ratchetfreak, note that to be on topic for us over on Code Review you'd really need to show actual code, not just an outline. –  Winston Ewert Feb 24 '13 at 17:10
add comment

1 Answer

up vote 4 down vote accepted

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.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.