将两个PHP函数转换为c#
本文关键字:函数 转换 PHP 两个 | 更新日期: 2023-09-27 18:20:04
我正试图在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();
}
public static string randomKey(int amount)
{
string keyset = "abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
string randkey = string.Empty;
Random random = new Random();
for (int i = 0; i < amount; i++)
{
randkey += keyset.Substring(0, random.Next(2, keyset.Length - 2));
}
return randkey;
}