Ebus_capital.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace ConsoleApp1.OCPPAuth
  9. {
  10. internal static class Ebus_capital
  11. {
  12. public static void Test()
  13. {
  14. //var url = "https://buscharge.zerovatech.com/service/api/v1/cpo/station";
  15. //var _body = "";
  16. //var timestamp = "";
  17. //string signature = GetSignature(GetUnencodeText(url, _body, timestamp, headers["PartnerId"], saltkey));
  18. var result = IsExpiryTime("1712031689");
  19. Console.WriteLine(result);
  20. }
  21. private static bool IsExpiryTime(string timestamp)
  22. {
  23. bool result = true;
  24. long minTime = DateTimeOffset.UtcNow.AddSeconds(-300).ToUnixTimeSeconds();
  25. long maxTime = DateTimeOffset.UtcNow.AddSeconds(300).ToUnixTimeSeconds();
  26. long requestTime = 0;
  27. if (long.TryParse(timestamp, out requestTime))
  28. {
  29. if (minTime < requestTime && maxTime > requestTime)
  30. {
  31. result = false;
  32. }
  33. }
  34. DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
  35. dateTime = dateTime.AddSeconds(requestTime).ToLocalTime();
  36. return result;
  37. }
  38. private static string GetUnencodeText(string url, string body, string timestamp, string partnerId, string saltkey)
  39. {
  40. string tempText = url.Substring(url.IndexOf('?') + 1).ToLower();
  41. tempText = tempText.StartsWith("http") ? string.Empty : tempText;
  42. body = tempText + body;
  43. string unencodeText = string.Format("{0}{1}{2}{3}", body, timestamp, partnerId, saltkey).ToLower();
  44. return unencodeText;
  45. }
  46. private static string GetSignature(string unencodeText)
  47. {
  48. if ((unencodeText == null) || (unencodeText.Length == 0))
  49. {
  50. return String.Empty;
  51. }
  52. unencodeText = unencodeText.ToLower();
  53. MD5 md5 = MD5.Create();// new MD5CryptoServiceProvider();
  54. byte[] textToHash = Encoding.UTF8.GetBytes(unencodeText);
  55. byte[] result = md5.ComputeHash(textToHash);
  56. return BitConverter.ToString(result).Replace("-", "").ToLower();
  57. }
  58. }
  59. }