我正在尝试在 c# 中复制 php 登录 - 但我的 php 失败了,我只是不够好,无法弄清楚如何在 c# 中执行等效操作。
我的 php 类是:
function randomKey($amount)
{
$keyset = "abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$randkey = "";
for ($i=0; $i<$amount; $i++)
$randkey .= substr($keyset, rand(0, strlen($keyset)-1), 1);
return $randkey;
}
public static function hashPassword($password)
{
$salt = self::randomKey(self::SALTLEN);
$site = new Sites();
$s = $site->get();
return self::hashSHA1($s->siteseed.$password.$salt.$s->siteseed).$salt;
}
一开始我有一个很好的破解:
public static string randomKey(string amount)
{
string keyset = "abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string randKey = "";
for (int i=0; i < amount; i++)
{
randKey = randKey +
}
}
但我不知道等效函数是什么。任何帮助真的将不胜感激。
编辑:你们真是太棒了。如果你不介意,我还有一个。对不起,如果我要求太多:
public static function checkPassword($password, $hash)
{
$salt = substr($hash, -self::SALTLEN);
$site = new Sites();
$s = $site->get();
return self::hashSHA1($s->siteseed.$password.$salt.$s->siteseed) === substr($hash, 0, self::SHA1LEN);
}
最佳答案
static string Sha1(string password)
{
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] data = sha1.ComputeHash(Encoding.UTF8.GetBytes(password));
StringBuilder sb = new StringBuilder();
foreach (byte b in data)
sb.Append(b.ToString("x2"));
return sb.ToString();
}
static string RandomKey(uint amaunt)
{
const string keyset = "abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuilder sb = new StringBuilder((int)amaunt, (int)amaunt);
Random rnd = new Random();
for (uint i = 0; i < amaunt; i++)
sb.Append(keyset[rnd.Next(keyset.Length)]);
return sb.ToString();
}
关于c# - 将两个 PHP 函数转换为 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17345019/