Solved

PHP to C# conversion

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

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

762 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

24 Experts available now in Live!

Get 1:1 Help Now