Solved

PHP to C# conversion

Posted on 2014-04-10
5
537 Views
Last Modified: 2014-04-19
I have this PHP code to compute a ticket# based on someone's ID and the current time.
I need a C# version of this. Can anyone help out?

$Cust_ID = "1158341";
$reftime = mktime(0,0,0,12,31,1973);
$preticket = floor((time()-$reftime)/(3600*24*7))*$Cust_ID;
$ticket = 'f' . dechex($Cust_ID) . md5($preticket);
echo $ticket;

Open in new window

0
Comment
Question by:EddieShipman
  • 4
5 Comments
 
LVL 26

Author Comment

by:EddieShipman
ID: 39992148
For reference, this produces the same output...

static void Main(string[] args)
{
    string Cust_ID = "1158341";
    int reftime = ConvertToUnixTimestamp(new DateTime(1973, 12, 31));
    int now = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
    string preticket = (Math.Floor(Convert.ToDouble(now - reftime) / Convert.ToDouble(3600 * 24 * 7)) * Int32.Parse(Cust_ID)).ToString();
    string hexval = Int32.Parse(Cust_ID).ToString("X");
    string ticket = string.Concat("f", hexval, MD5Hash(preticket)).ToLower();
    Console.WriteLine(ticket);
    Console.ReadKey();

}
static int ConvertToUnixTimestamp(DateTime date)
{
    DateTime origin = new DateTime(1970, 1, 1, 0, 0, 0, 0);
    TimeSpan diff = date - origin;
    return (int)diff.TotalSeconds;
}
public static string MD5Hash(string text)
{
    System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
    return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.Default.GetBytes(text))), "-", "");
}

Open in new window

0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 100 total points
ID: 39992162
Untested, but it should be along these lines:

using System;
using System.Security.Cryptography;
using System.Text;

...

MD5 md5 = MD5.Create();
DateTime reftime = new DateTime(1973, 12, 31);
Double preticket = Math.Floor((DateTime.Now - reftime) / (3600*24*7).TotalSeconds) * Cust_ID;
string preticketStr = preticket.ToString();
byte[] preticketBytes = Encoding.ASCII.GetBytes(preticketStr);
byte[] hashBytes = md5.ComputeHash(preticketBytes);
string ticket = "f" + Convert.ToString(Cust_ID, 16) + Convert.ToBase64String(hashBytes);

Open in new window

0
 
LVL 26

Author Comment

by:EddieShipman
ID: 39992281
Looks OK, kaufmed but I get these in VS2010:
Error	1	'int' does not contain a definition for 'TotalSeconds' 
  and no extension method 'TotalSeconds' accepting a first argument of type 'int' could be found 
  (are you missing a using directive or an assembly reference?)
Error	2	The best overloaded method match for 'System.Convert.ToString(object, System.IFormatProvider)' 
  has some invalid arguments	
Error	3	Argument 2: cannot convert from 'int' to 'System.IFormatProvider'

Open in new window

0
 
LVL 26

Accepted Solution

by:
EddieShipman earned 0 total points
ID: 39999114
Here is final code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Security.Cryptography;
    .
    .
    .
    public static void Main()
    {
         var ticket = getLogonTicket("1007945");
         Console.WriteLine(ticket);                   
    }
    static string getLogonTicket(string Cust_ID)
    {
        int iCust_ID;
        if (!int.TryParse(Cust_ID,out iCust_ID))
            return "";
        
        var weeks = (DateTime.Now - new DateTime(1973, 12, 31)).Days/7;
        var pre_ticket = weeks * iCust_ID;
        return ("f" + iCust_ID.ToString("X") + DoubleToMD5(pre_ticket)).ToLower();
    }
    static string DoubleToMD5(double Value)
    {
        var md5 = new MD5CryptoServiceProvider();
        var bytehash = md5.ComputeHash(ASCIIEncoding.Default.GetBytes(Value.ToString()));
        return BitConverter.ToString(bytehash).Replace("-","");
    }

Open in new window

0
 
LVL 26

Author Closing Comment

by:EddieShipman
ID: 40010157
Self Answered
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question