2

I'm trying to replicate a php login in c# - but my php is failing me, i'm just not good enough to work out how to do the equivalent in c#.

My php classes are:

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; 
}

I had a good crack at the first with:

public static string randomKey(string amount)
    {
        string keyset = "abcdefghijklmABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        string randKey = "";
          for (int i=0; i < amount; i++)
                {
                    randKey = randKey + 
                }

    }

But I can't work out what the equivalent functions are. Any help really will be appreciated.

Edit: You guys have been absolutely amazing. I have one more if you don't mind. Sorry if i'm asking too much:

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);
    }
MissCoder87
  • 2,669
  • 10
  • 47
  • 82
  • in your C# function you are comparing int to string (i < amount). It should be amount.Length, right ? – Kamen Stoykov Jun 27 '13 at 13:52
  • [this](http://msdn.microsoft.com/en-us/library/system.string.substring.aspx), [this](http://msdn.microsoft.com/en-us/library/system.string.length.aspx), and [this](http://msdn.microsoft.com/en-us/library/zd1bc8e5.aspx) – Sayse Jun 27 '13 at 13:52
  • You'll also need [`Random`](http://msdn.microsoft.com/en-us/library/system.random.aspx) and [`SHA1`](http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha1.aspx). [Oh, and keep instantiating `Random` outside of loop.](http://stackoverflow.com/questions/10598000/generated-random-numbers-are-always-equal) – Leri Jun 27 '13 at 13:53
  • Define a variable of type Random then you can do `randKey += keyset.ElementAt(random.Next(0, keyset.Length - 1));` to get a random character from keyset. – Pierre-Luc Pineault Jun 27 '13 at 13:55

2 Answers2

1
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();
}
Denis
  • 5,894
  • 3
  • 17
  • 23
  • Thanks so much for that, really appreciated. Could you help with the other one if you don't mind too please? Thanks a lot mate – MissCoder87 Jun 27 '13 at 13:59
  • Thank you so much. I hope i'm not taking the mick, i've added one more function above. Would it be possible for you to do that one for me too? I really appreciate it! SaltLen = 4 sha1len = 40 – MissCoder87 Jun 27 '13 at 14:12
0
    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;
    }
Yengibar
  • 31
  • 1
  • 3