gpt4 book ai didi

javascript - 尝试将 HashPassword c#(Microsoft 示例)转换为 javascript

转载 作者:行者123 更新时间:2023-11-28 04:59:36 24 4
gpt4 key购买 nike

我想将 c# 中的 byte.Parse 转换为 javascript ..但我不知道如何。

byte[] binarySaltValue = new byte[SaltValueSize];

binarySaltValue[0] = byte.Parse(saltValue.Substring(0, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);

我需要获取子字符串(0,2)的ascII代码...我是对的吗?

编辑问题

我真正想做的是将 Microsoft 的 HashPassword 方法转换为 Javascript。

private static string HashPassword(string clearData, string saltValue, HashAlgorithm hash)
{
UnicodeEncoding encoding = new UnicodeEncoding();

if (clearData != null && hash != null && encoding != null)
{
// If the salt string is null or the length is invalid then
// create a new valid salt value.



// Convert the salt string and the password string to a single
// array of bytes. Note that the password string is Unicode and
// therefore may or may not have a zero in every other byte.

byte[] binarySaltValue = new byte[SaltValueSize];

binarySaltValue[0] = byte.Parse(saltValue.Substring(0, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[1] = byte.Parse(saltValue.Substring(2, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[2] = byte.Parse(saltValue.Substring(4, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);
binarySaltValue[3] = byte.Parse(saltValue.Substring(6, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture.NumberFormat);

byte[] valueToHash = new byte[SaltValueSize + encoding.GetByteCount(clearData)];
byte[] binaryPassword = encoding.GetBytes(clearData);

// Copy the salt value and the password to the hash buffer.

binarySaltValue.CopyTo(valueToHash, 0);
binaryPassword.CopyTo(valueToHash, SaltValueSize);

byte[] hashValue = hash.ComputeHash(valueToHash);

// The hashed password is the salt plus the hash value (as a string).

string hashedPassword = saltValue;

foreach (byte hexdigit in hashValue)
{
hashedPassword += hexdigit.ToString("X2", CultureInfo.InvariantCulture.NumberFormat);
}

// Return the hashed password as a string.

return hashedPassword;
}

return null;
}

最佳答案

好的,这就是我的解决方案,也许它没有优化,但工作正常!

 function Salt(plainText, salt) {


if (plainText != undefined && plainText != "" && salt != null) {
// If the salt string is null or the length is invalid then
// create a new valid salt value.

// Convert the salt string and the password string to a single
// array of bytes. Note that the password string is Unicode and
// therefore may or may not have a zero in every other byte.

var binarySaltValue = new Array();
binarySaltValue[0] = parseInt(salt.substring(0, 2), 16);
binarySaltValue[1] = parseInt(salt.substring(2, 4), 16);
binarySaltValue[2] = parseInt(salt.substring(4, 6), 16);
binarySaltValue[3] = parseInt(salt.substring(6, 8), 16);
var binaryPassword = cryptoHelper.stringToBytes(plainText);
var valueToHash = new Array();
// Copy the salt value and the password to the hash buffer.
// binarySaltValue.concat(valueToHash);
// binarySaltValue.CopyTo(valueToHash, 0);
// binaryPassword.CopyTo(valueToHash, SaltValueSize);
valueToHash = valueToHash.concat(binarySaltValue);
// Détermine la longeur utilisé par le array - 2048
var newLengthArray = 2048 - valueToHash.length;
var tampon = new Array(newLengthArray);
for (var i = 0; i < tampon.length; i++) {
tampon[i] = 0;
}
valueToHash = valueToHash.concat(tampon);
valueToHash = valueToHash.concat(binaryPassword);
var hashValue = CryptoJS.MD5(CryptoJS.lib.Base.ByteArray(valueToHash));
var arr = wordArrayToByteArray(hashValue);
var hashedPassword = salt;
for (var i = 0; i < arr.length; i++) {
hexdigit = arr[i];
var hexValue = hexdigit.toString(16);
if (hexValue.length < 2) hexValue = "0" + hexValue;
hashedPassword += hexValue;
}
return hashedPassword;
}
return null;
}

关于javascript - 尝试将 HashPassword c#(Microsoft 示例)转换为 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42239528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com