【发布时间】:2015-01-04 23:54:21
【问题描述】:
我在 MongoDB 中有一个按如下方式组织的集合:
{
"_id" : ObjectId("xxxxxx"),
"Username" : username
"Password" : encrypted_password
"Position" : position
.....
}
{
"_id" : ObjectId("yyyyyy"),
"Username" : username2
"Password" : encrypted_password2
"Position" : position2
.....
}
我想遍历集合并检查是否存在用户名+密码组合,但我似乎无法弄清楚:/
我正在使用带有 C# 的 MongoDB 驱动程序,这就是我目前所拥有的:
public bool DoesSaveDataExist(String database, String collection, string username, string password)
{
MongoClient client = new MongoClient(); // connect to localhost
MongoServer server = client.GetServer();
MongoDatabase test = server.GetDatabase(database);
var GetFromCollection = test.GetCollection<BsonDocument>(collection);
byte[] passwordToByte = System.Text.Encoding.ASCII.GetBytes(password);
passwordToByte = new System.Security.Cryptography.SHA256Managed().ComputeHash(passwordToByte);
String hash = System.Text.Encoding.ASCII.GetString(passwordToByte);
IMongoQuery query = new QueryDocument {
{ "Player Name", username },
{ "Password", hash}
};
return false;
}
有什么帮助吗?
【问题讨论】:
-
SHA 太快,无法存储密码。您需要使用加盐的迭代哈希。使用 PBKDFv2。此外,哈希不是 ASCII;使用 base63 或存储一个字节数组。