Click here to Skip to main content
15,887,083 members
Home / Discussions / C#
   

C#

 
QuestionC# text to speech Pin
tsi clinero22-Jun-18 14:22
tsi clinero22-Jun-18 14:22 
AnswerRe: C# text to speech Pin
OriginalGriff22-Jun-18 18:10
mveOriginalGriff22-Jun-18 18:10 
GeneralRe: C# text to speech Pin
Eric Lynch22-Jun-18 23:55
Eric Lynch22-Jun-18 23:55 
GeneralRe: C# text to speech Pin
OriginalGriff23-Jun-18 0:11
mveOriginalGriff23-Jun-18 0:11 
QuestionHow do Xamarin forms connect to SQL Server ? Pin
Member 245846721-Jun-18 21:57
Member 245846721-Jun-18 21:57 
AnswerRe: How do Xamarin forms connect to SQL Server ? Pin
Richard MacCutchan21-Jun-18 22:33
mveRichard MacCutchan21-Jun-18 22:33 
AnswerRe: How do Xamarin forms connect to SQL Server ? Pin
Pete O'Hanlon22-Jun-18 1:26
mvePete O'Hanlon22-Jun-18 1:26 
QuestionComparing hashed Passwords Pin
Kevin Marois21-Jun-18 12:29
professionalKevin Marois21-Jun-18 12:29 
I'm using C# 4.5.2 & SQL Server Express 2012 RTM..

When I create a new user in my app I hash the password using
public static string Hash(string password, int iterations)
{
    //create salt
    byte[] salt;
    new RNGCryptoServiceProvider().GetBytes(salt = new byte[SaltSize]);

    //create hash
    var pbkdf2 = new Rfc2898DeriveBytes(password, salt, iterations);
    var hash = pbkdf2.GetBytes(HashSize);

    //combine salt and hash
    var hashBytes = new byte[SaltSize + HashSize];
    Array.Copy(salt, 0, hashBytes, 0, SaltSize);
    Array.Copy(hash, 0, hashBytes, SaltSize, HashSize);

    //convert to base64
    var base64Hash = Convert.ToBase64String(hashBytes);

    //format hash with extra information
    return string.Format("$MYHASH$V1${0}${1}", iterations, base64Hash);
}

Then, when I do the Login, I prompt the user's for a password, hash it using the same function, then get the user's record from the DB and compare it using this:
public static bool Verify(string password, string hashedPassword)
{
    //check hash
    if (!IsHashSupported(hashedPassword))
    {
        throw new NotSupportedException("The hashtype is not supported");
    }

    //extract iteration and Base64 string
    var splittedHashString = hashedPassword.Replace("$MYHASH$V1$", "").Split('$');
    var iterations = int.Parse(splittedHashString[0]);
    var base64Hash = splittedHashString[1];

    //get hashbytes
    var hashBytes = Convert.FromBase64String(base64Hash);

    //get salt
    var salt = new byte[SaltSize];
    Array.Copy(hashBytes, 0, salt, 0, SaltSize);

    //create hash with given salt
    var pbkdf2 = new Rfc2898DeriveBytes(password, salt, iterations);
    byte[] hash = pbkdf2.GetBytes(HashSize);

    //get result
    for (var i = 0; i < HashSize; i++)
    {
        if (hashBytes[i + SaltSize] != hash[i])
        {
            return false;
        }
    }
    return true;
}
When I try this in a console app it works fine. The hashed password is verified against what I type in.

When I retrieve the password from the DB and compare it using Verify it fails.

Anyone know what's wrong, or have a better way?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.

AnswerRe: Comparing hashed Passwords Pin
#realJSOP21-Jun-18 13:55
mve#realJSOP21-Jun-18 13:55 
GeneralRe: Comparing hashed Passwords Pin
OriginalGriff21-Jun-18 18:41
mveOriginalGriff21-Jun-18 18:41 
GeneralRe: Comparing hashed Passwords Pin
Kevin Marois22-Jun-18 5:58
professionalKevin Marois22-Jun-18 5:58 
GeneralRe: Comparing hashed Passwords Pin
Richard Deeming22-Jun-18 6:50
mveRichard Deeming22-Jun-18 6:50 
GeneralRe: Comparing hashed Passwords Pin
#realJSOP22-Jun-18 6:55
mve#realJSOP22-Jun-18 6:55 
GeneralRe: Comparing hashed Passwords Pin
Richard Deeming22-Jun-18 7:02
mveRichard Deeming22-Jun-18 7:02 
GeneralRe: Comparing hashed Passwords Pin
Dave Kreskowiak22-Jun-18 7:12
mveDave Kreskowiak22-Jun-18 7:12 
GeneralRe: Comparing hashed Passwords Pin
#realJSOP22-Jun-18 7:18
mve#realJSOP22-Jun-18 7:18 
GeneralRe: Comparing hashed Passwords Pin
Kevin Marois22-Jun-18 7:45
professionalKevin Marois22-Jun-18 7:45 
GeneralRe: Comparing hashed Passwords Pin
#realJSOP22-Jun-18 6:54
mve#realJSOP22-Jun-18 6:54 
AnswerRe: Comparing hashed Passwords Pin
OriginalGriff21-Jun-18 18:30
mveOriginalGriff21-Jun-18 18:30 
GeneralRe: Comparing hashed Passwords Pin
Richard Deeming21-Jun-18 23:54
mveRichard Deeming21-Jun-18 23:54 
AnswerRe: Comparing hashed Passwords Pin
Richard Deeming22-Jun-18 0:04
mveRichard Deeming22-Jun-18 0:04 
GeneralRe: Comparing hashed Passwords Pin
Kevin Marois22-Jun-18 5:59
professionalKevin Marois22-Jun-18 5:59 
GeneralRe: Comparing hashed Passwords Pin
Richard Deeming22-Jun-18 6:51
mveRichard Deeming22-Jun-18 6:51 
GeneralRe: Comparing hashed Passwords Pin
Kevin Marois22-Jun-18 7:55
professionalKevin Marois22-Jun-18 7:55 
QuestionC# web browser example Pin
tsi clinero21-Jun-18 12:06
tsi clinero21-Jun-18 12:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.