简体   繁体   English

vb.net,如何验证SHA1哈希密码?

[英]vb.net, how do I validate a SHA1 hashed password?

I inherited a program that I need to support and I was told that the passwords were stored in a MSSQL database using a SHA1 hash. 我继承了一个我需要支持的程序,并被告知密码是使用SHA1哈希存储在MSSQL数据库中的。 When I try to read the data from the database all I get is "System.Byte[]". 当我尝试从数据库中读取数据时,我得到的只是“ System.Byte []”。

The program asks the user for a password and I am able to create a SHA1 hash using the following: 该程序要求用户输入密码,并且我可以使用以下命令创建SHA1哈希:

Public Function GetSHA1HashData(data As String) As String

        Dim cBase64 As String
        Dim objSHA1 As New SHA1CryptoServiceProvider()
        Dim abBytesToHash() As Byte
        Dim cHash As String

        cBase64 = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(data))
        abBytesToHash = System.Text.Encoding.ASCII.GetBytes(cBase64)
        cHash = BitConverter.ToString(objSHA1.ComputeHash(abBytesToHash))
        cHash = Replace(cHash, "-", "")

        Return cHash

End Function

So my question is, how do I compare the newly created hash with the value I am pulling out of the database to see if they are the same? 所以我的问题是,如何将新创建的哈希值与要从数据库中拉出的值进行比较,以查看它们是否相同? What do I need to do with "System.Byte[]" to turn it into something I can read? 我需要对“ System.Byte []”进行些什么才能使其变成我可以阅读的内容?

Thanks. 谢谢。

You are able to create a SHA-1 hash using your function, but it is questionable if the person that filled the database with the values did use the same function. 您可以使用您的函数创建SHA-1哈希,但是用值填充数据库的人员是否确实使用了相同的函数,这令人怀疑。 Currently you are base 64 encoding the data, which is already a string, only to retrieve the character encoding. 当前,您正在使用base 64对数据(已经是字符串)进行编码,仅用于检索字符编码。 It is more likely that the person simply directly got the character encoding - I'm guessing UTF-8 here - and calculated the SHA-1 value. 这个人很可能直接得到了字符编码-我这里是UTF-8-并计算了SHA-1值。

Public Function GetSHA1HashData(data As String) As Byte()
    Dim objSHA1 As New SHA1CryptoServiceProvider()
    return objSHA1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(data));
End Function

Ok, so now you would have a byte array from the database and one calculated from the users password. 好的,现在您将从数据库中获得一个字节数组,并从用户密码中计算出一个字节数组。 You can directly compare these byte arrays. 您可以直接比较这些字节数组。 Fortunately you can rely on StackOverflow and Jon Skeet to already have an answer on how to do this . 幸运的是,您可以依靠StackOverflow和Jon Skeet获得有关如何执行此操作的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM