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);
    }
4
  • in your C# function you are comparing int to string (i < amount). It should be amount.Length, right ? Commented Jun 27, 2013 at 13:52
  • this, this, and this Commented Jun 27, 2013 at 13:52
  • You'll also need Random and SHA1. Oh, and keep instantiating Random outside of loop. Commented Jun 27, 2013 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. Commented Jun 27, 2013 at 13:55

2 Answers 2

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();
}
Sign up to request clarification or add additional context in comments.

2 Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.