我在C#中有一个应用程序,在SQLite中有一个数据库。在数据库中,我有一个有几列的表。在一个列中,我有一个值,该值是用SHA1 from查询加密的。但我需要像这样在我的C#应用程序中使用它:
cmd.CommandText = "Select * from accounts where (username=@username and password=sha1(@password));";我需要选择字符串值,以便登录到app。我收到错误:no such function sha1。
从其他帖子(如:这一个 )中,我知道我必须创建另一个函数来使用sha1进行散列?但我真的不明白怎么做this..Can,有人帮我吗?对不起,如果是重复的话,但我没有找到具体的答案。
发布于 2015-03-22 14:55:32
由于SQLite默认不实现任何sha1函数,因此必须将密码哈希从SQL查询移到代码中。
这意味着您的查询应该是:
cmd.CommandText = "Select * from accounts where (username=@username and password=@password);";你应该像这样传递密码:
cmd.Parameters.AddWithValue("@password", sha1(password));并且您应该实现自己的sha1函数
using System.Security.Cryptography;
...
string sha1(string input) {
byte[] byteArray = Encoding.UTF8.GetBytes(input);
string result="";
using (HashAlgorithm hash = SHA1.Create()) {
result=Convert.ToBase64String(hash.ComputeHash(byteArray));
}
return result;
}重要
使用哈希函数被认为是存储密码的非常不安全的,您应该考虑了解密钥推导函数,阅读维基百科页面将引导您实现这些函数。
https://stackoverflow.com/questions/29195488
复制相似问题