Solved

PHP to C# conversion

Posted on 2014-04-10
5
514 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

815 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now