{"id":61,"date":"2024-05-29T23:39:49","date_gmt":"2024-05-29T23:39:49","guid":{"rendered":"https:\/\/mystifying-liskov.77-68-127-141.plesk.page\/?p=61"},"modified":"2024-05-29T23:39:49","modified_gmt":"2024-05-29T23:39:49","slug":"common-cs","status":"publish","type":"post","link":"https:\/\/mystifying-liskov.77-68-127-141.plesk.page\/?p=61","title":{"rendered":"Common.cs"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>using System;\nusing System.Security.Cryptography;\nusing System.Text;\n\nnamespace WooCommerceNET.Base\n{\n    public static class Common\n    {\n        static Common()\n        {\n            DebugInfo = new StringBuilder();\n        }\n\n        public static StringBuilder DebugInfo { get; set; }\n\n        public static string GetUnixTime(bool micro)\n        {\n            long unixtime = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0)).Ticks;\n\n            if (!micro)\n            {\n                DebugInfo.AppendLine(unixtime.ToString().Substring(0, 10));\n                return unixtime.ToString().Substring(0, 10);\n            }\n\n            DebugInfo.AppendLine(\"0.\" + unixtime.ToString().Substring(10, 6) + \"00 \" + unixtime.ToString().Substring(0, 10));\n\n            return \"0.\" + unixtime.ToString().Substring(10, 6) + \"00 \" + unixtime.ToString().Substring(0, 10);\n        }\n\n        public static string GetSHA1(string key, string message)\n        {\n            var encoding = new ASCIIEncoding();\n\n            byte&#91;] keyBytes = encoding.GetBytes(key);\n            byte&#91;] messageBytes = encoding.GetBytes(message);\n\n            string Sha1Result = string.Empty;\n\n            using (HMACSHA1 SHA1 = new HMACSHA1(keyBytes))\n            {\n                var Hashed = SHA1.ComputeHash(messageBytes);\n                Sha1Result = Convert.ToBase64String(Hashed);\n            }\n\n            return Sha1Result;\n        }\n\n        public static string GetSHA256(string key, string message)\n        {\n            return HMAC_SHA256.HMAC(Encoding.UTF8.GetBytes(message), Encoding.UTF8.GetBytes(key));\n            \/\/MacAlgorithmProvider sha256 = MacAlgorithmProvider.OpenAlgorithm(\"HMAC_SHA256\");\n            \/\/IBuffer contentBuffer = CryptographicBuffer.ConvertStringToBinary(message, BinaryStringEncoding.Utf8);\n\n            \/\/IBuffer keyBuffer = CryptographicBuffer.ConvertStringToBinary(key, BinaryStringEncoding.Utf8);\n            \/\/var signatureKey = sha256.CreateKey(keyBuffer);\n\n            \/\/IBuffer digest = CryptographicEngine.Sign(signatureKey, contentBuffer);\n\n            \/\/return CryptographicBuffer.EncodeToBase64String(digest);\n        }\n\n        public static string GetMD5(string content)\n        {\n            using (MD5 md5Hash = MD5.Create())\n            {\n                byte&#91;] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(content));\n\n                \/\/ Create a new Stringbuilder to collect the bytes\n                \/\/ and create a string.\n                StringBuilder sBuilder = new StringBuilder();\n\n                \/\/ Loop through each byte of the hashed data \n                \/\/ and format each one as a hexadecimal string.\n                for (int i = 0; i &lt; data.Length; i++)\n                {\n                    sBuilder.Append(data&#91;i].ToString(\"x2\"));\n                }\n\n                \/\/ Return the hexadecimal string.\n                return sBuilder.ToString();\n            }\n        }\n    }\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-61","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/mystifying-liskov.77-68-127-141.plesk.page\/index.php?rest_route=\/wp\/v2\/posts\/61","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mystifying-liskov.77-68-127-141.plesk.page\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mystifying-liskov.77-68-127-141.plesk.page\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mystifying-liskov.77-68-127-141.plesk.page\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mystifying-liskov.77-68-127-141.plesk.page\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=61"}],"version-history":[{"count":1,"href":"https:\/\/mystifying-liskov.77-68-127-141.plesk.page\/index.php?rest_route=\/wp\/v2\/posts\/61\/revisions"}],"predecessor-version":[{"id":62,"href":"https:\/\/mystifying-liskov.77-68-127-141.plesk.page\/index.php?rest_route=\/wp\/v2\/posts\/61\/revisions\/62"}],"wp:attachment":[{"href":"https:\/\/mystifying-liskov.77-68-127-141.plesk.page\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=61"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mystifying-liskov.77-68-127-141.plesk.page\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=61"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mystifying-liskov.77-68-127-141.plesk.page\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=61"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}