?
Solved

PHP to C# conversion

Posted on 2014-04-10
5
Medium Priority
?
569 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

770 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