no

C# Converting a date to timestamp and how to create an Md5 function

A always use these 2 methods so I'm gonna paste them here. I don't remember if I change some lines in these code but I'm sure I ...

A always use these 2 methods so I'm gonna paste them here. I don't remember if I change some lines in these code but I'm sure I got them from Microsoft hmmm already forget about it.

DateTime -> TimeStamp
/// 
/// method for converting a System.DateTime value to a UNIX Timestamp
/// 
/// date to convert/// 
private static double ConvertToTimestamp(DateTime value)
{
  //create Timespan by subtracting the value provided from
  //the Unix Epoch
  TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());

  //return the total seconds (which is a UNIX timestamp)
  return (double)span.TotalSeconds;
} 

Md5
// Hash an input string and return the hash as
// a 32 character hexadecimal string.
private static string Md5(string input)
{
  // Create a new instance of the MD5CryptoServiceProvider object.
  MD5 md5Hasher = MD5.Create();

  // Convert the input string to a byte array and compute the hash.
  byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

  // Create a new Stringbuilder to collect the bytes
  // and create a string.
  StringBuilder sBuilder = new StringBuilder();

  // Loop through each byte of the hashed data
  // and format each one as a hexadecimal string.
  for (int i = 0; i < data.Length; i++)
  {
    sBuilder.Append(data[i].ToString("x2"));
  }

  // Return the hexadecimal string.
  return sBuilder.ToString();
}

Related

.net 1085864997328842145

Post a Comment

item