3

I need to hash passwords with C# in a way that another software understands it . Originally php's crypt function is doing that. It has the following output

$6$rounds=1000$1f$yeKGQo0b8MqqMpocFla8uKLE6GOpEygSQUH4qMi4msJZsD50Eh00bU4GwoGGPEeLMdG6C17ehl/l8SrcOABdC0 

I guess it is SHA512. . How can I achieve php's crypt functionality with C#

original php

$salt = '$6$rounds=1000$'.dechex(rand(0,15)).dechex(rand(0,15)).'$'; $crypted = crypt($password, $salt); 
5
  • Do you have the php code that produces that output? If so, can you show it? Commented Sep 8, 2015 at 10:01
  • According to the docs crypt does something different depending on what is available on the machine. So to answer this we would need to know what its doing on your machine. Commented Sep 8, 2015 at 10:04
  • looks like SHA-512 to me - looking through the php docs Commented Sep 8, 2015 at 10:05
  • 2
    A C# implementation of the PHP crypt function can be found here: gist.github.com/otac0n/1092558, see this post for futher details stackoverflow.com/questions/855103/…, Commented Sep 8, 2015 at 10:10
  • I edited my question (php source) Commented Sep 8, 2015 at 10:21

1 Answer 1

1

CryptSharp computes this for all common crypt variations.

Generating a SHA-512 salted hash (the default number of rounds for this algorithm is 5000):

using CryptSharp; string hash = Crypter.SHA512.Crypt(password); 

Using a custom number of rounds:

var saltOptions = new CrypterOptions() { { CrypterOption.Rounds, 10000 } }; string salt = Crypter.SHA512.GenerateSalt(saltOptions); string hash = Crypter.SHA512.Crypt(password, saltOptions); 

Verifying a hash:

bool matched = Crypter.CheckPassword(testPassword, hash); 

On another note, that original PHP should really be secured. The salt is only 8 bit and generated with rand (use openssl_random_pseudo_bytes instead). The hash specifically chooses a fifth of the default number of rounds.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.