2013-01-14 40 views
3

我在我的web应用程序(Symfony2与FOSUserBundle)上创建了一个帐户,并使用密码“lolwut”(不带引号)进行了注册。Symfony2(FOSUserBundle)SHA512哈希不匹配C#SHA512哈希

这些人是我security.yml配置的设置:

security: 
    encoders: 
     FOS\UserBundle\Model\UserInterface: 
      algorithm: sha512 
      iterations: 1 
      encode_as_base64: false 

得到的数据:

Hashed password: 
f57470574dbf29026821519b19539d1c2237e4315d881fa412da978af554740c6b284062a9a4af7d0295d18ebea8ef2e152cf674b283f792fe0b568a93f969cf 
Salt: 
kuc5bixg5u88s4k8ggss4osoksko0g8 

现在,由于迭代1集,我假设编码“lolwut “在C#中的SHA512将给我同样的结果,这是我的逻辑:

string salt = "kuc5bixg5u88s4k8ggss4osoksko0g8"; 
string input = "lolwut"; 
string passAndSalt = String.Concat(input, salt); 

System.String Hashed = System.BitConverter.ToString(((System.Security.Cryptography.SHA512)new System.Security.Cryptography.SHA512Managed()).ComputeHash(System.Text.Encoding.ASCII.GetBytes(passAndSalt))).Replace("-", ""); 
return passAndSalt + "<br>" + Hashed; 

但是,这返回th E采用值不匹配FOSUserBundle哈希密码都:

82E8CA0408B23DB50EB654EDB50A7926AC73613184054DB82FB6D67CD4186B7A045D265AEDE6E3852CD85B981F15F6615C1C0C6FBF443B1672DF59DE23557BD9 

我知道我必须在某个地方做错了什么,但我不能为我的生活弄清楚它是什么,它的使我抓狂。有谁能帮我出来吗?

+0

我认为C#是基地64编码密码,但我可能是错 – JamesHalsall

+1

为什么你完全合格的所有你的类型名称?它使代码更难以阅读 - 就像把它全部放在一个声明中一样...... –

+0

@JonSkeet这是因为它不是我自己的代码,我从示例中复制它以检查它是否实际工作。你是对的,但它是不可读的,一旦我有这个问题分类,将被清理。 :) – thomastuts

回答

3

Symfony的合并密码和盐password{salt},所以这段代码将返回相同的哈希:

string salt = "kuc5bixg5u88s4k8ggss4osoksko0g8"; 
    string input = "lolwut"; 
    string passAndSalt = String.Format("{0}{{{1}}}", input, salt); 

    System.String Hashed = System.BitConverter.ToString(((System.Security.Cryptography.SHA512)new System.Security.Cryptography.SHA512Managed()).ComputeHash(System.Text.Encoding.ASCII.GetBytes(passAndSalt))).Replace("-", ""); 
    // Hashed.ToLower() 
+0

非常感谢,这就是诀窍! C#将它作为大写字符串返回,所以我没有'返回Hashed.ToLower()'来完美匹配它们。 – thomastuts