[英]Password hashing in UWP
I have the following code in .net framework. 我在.net框架中有以下代码。
public string GetHashedPassword(string password, string salt)
{
byte[] saltArray = Convert.FromBase64String(salt);
byte[] passArray = Convert.FromBase64String(password);
byte[] salted = new byte[saltArray.Length + passArray.Length];
byte[] hashed = null;
saltArray.CopyTo(salted, 0);
passArray.CopyTo(salted, saltArray.Length);
using (var hash = new SHA256Managed())
{
hashed = hash.ComputeHash(salted);
}
return Convert.ToBase64String(hashed);
}
I'm trying to create an equivalent in .net core for a UWP application. 我正在尝试为UWP应用程序在.net核心中创建等效项。 Here's what I have so far.
到目前为止,这就是我所拥有的。
public string GetHashedPassword(string password, string salt)
{
IBuffer input = CryptographicBuffer.ConvertStringToBinary(password + salt, BinaryStringEncoding.Utf8);
var hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var hash = hashAlgorithm.HashData(input);
//return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, hash);
}
The last line, converting the buffer back to a string doesn't work. 最后一行,将缓冲区转换回字符串不起作用。 I get this exception:
我得到这个例外:
No mapping for the Unicode character exists in the target multi-byte code page. 目标多字节代码页中不存在Unicode字符的映射。
How can I convert the buffer back into a string? 如何将缓冲区转换回字符串?
I am assuming, that you want to get the hashed password in a base64-format, because you did that in your .net example. 我假设您想以base64格式获取哈希密码,因为您是在.net示例中这样做的。
To get this, change: 要获取此信息,请更改:
CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, hash);
to: 至:
CryptographicBuffer.EncodeToBase64String(hash);
So the complete method looks like this: 因此,完整的方法如下所示:
public string GetHashedPassword(string password, string salt)
{
IBuffer input = CryptographicBuffer.ConvertStringToBinary(password + salt, BinaryStringEncoding.Utf8);
var hashAlgorithm = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha256);
var hash = hashAlgorithm.HashData(input);
return CryptographicBuffer.EncodeToBase64String(hash);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.