Question

I know that this question is already asked many times but actually till now I can't find the right one for me so please help. I want to insert new users in moodle's database by a script written in c#. What i want to do is to insert the username and a password to that user from this script. So i need to insert the hashed password. All my hashed passwords begin with "$2y$10$". In c# I tried several ways and it didn't give me the same password. For ex. I tried :

string cryptedPassword = Crypter.Blowfish.Crypt(password);

It gives me

$2a&06&bQuFKC8fVPYQsGbQj3sL7uoZByn7tRc4enxBeiN2k1wu5iyubTIyi

although the expected one is

$2y$10$SNUjOqCuwKodg0E126mHUO2NzMBnIHZl/fD/Q2wEv4hg.y5y2j4VC

. Also the following is giving true so the crypter can hash my password in the same way as moodle script does:

bool matches = Crypter.CheckPassword(password, "$2y$10$SNUjOqCuwKodg0E126mHUO2NzMBnIHZl/fD/Q2wEv4hg.y5y2j4VC");

How can i specify the hash-algorithm = 2y = BCrypt and cost-factor = 10 = 2^10 iterations in 'Crypter.Blowfish' in a way to have the same hashed password as expected? Any help is appreciated. Thanks in advance

Was it helpful?

Solution

Function Crypter.Blowfish.Crypt set the second parameter Salt, try to set it, because when you don't set Salt parameter module used random salt for each running try this

string cryptedPassword = Crypter.Blowfish.Crypt(password,"some_SALT");

OTHER TIPS

Just to show as an answer and not as a comment (more readable):

as @y.h commented:

string result = Crypter.Blowfish.Crypt(cadena, 
                new CrypterOptions()
                {
                    { CrypterOption.Variant, BlowfishCrypterVariant.Corrected }, 
                    { CrypterOption.Rounds, 6 }
                });

This will produce something like:

$2y$06$xkdTy5YwKmMCDP6vQGC5A.YgZkKxv17xQQfMyIUNfNWazd4pqqibK

Which is a valid 2y version (plus round factor 6 )

Cheers!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top