/*
         * private string MD5ComputeHash(byte[] data)
         * {
         *
         *  if (data.Length != 0)
         *  {
         *      wifi.CreateRamFile("fileToHash", data);
         *      return Encoding.UTF8.GetString(wifi.ComputeHash("3", "fileToHash")).Substring(4);
         *  }
         *  else
         *  {
         *      return "D41D8CD98F00B204E9800998ECF8427E";
         *  }
         *
         *
         *  //byte[] hash= xBrainLab.Security.Cryptography.MD5.GetHash(StringData);
         *  //string hashString = xBrainLab.Security.Cryptography.MD5.GetHashString(StringData);
         *
         *
         *  //using (HashAlgorithm csp = new HashAlgorithm(PervasiveDigital.Security.ManagedProviders.HashAlgorithmType.MD5))
         *  //using (HashAlgorithm csp = new HashAlgorithm(xBrainLab.Security.Cryptography.MD5)
         *
         *  //using (HashAlgorithm csp = HashAlgorithm.Create("Md5"))
         *
         *  //using (HashAlgorithm csp = HashAlgorithm.Create("MD5"))
         *  //{
         *
         *     // hash = csp.ComputeHash(data);
         *  //}
         *
         *  //string hashString = ByteExtensions.ToHexString(hash, "");
         *
         *  //return hashString;
         * }
         */
        #endregion

        #region CreateTableAuthorizationHeader
        protected string CreateTableAuthorizationHeader(byte[] content, string canonicalResource, string ptimeStamp, string pHttpVerb, ContType pContentType, out string pMD5Hash, out byte[] pHash, bool useSharedKeyLite = false)
        {
            string contentType = getContentTypeString(pContentType);

            pMD5Hash = string.Empty;
            pHash    = null;

            if (!useSharedKeyLite)
            {
                // long startTime = DateTime.Now.Ticks;

                pHash    = xBrainLab.Security.Cryptography.MD5.GetHash(content);
                pMD5Hash = BitConverter.ToString(pHash);
                pMD5Hash = pMD5Hash.Replace("-", string.Empty);

                // long endTime = DateTime.Now.Ticks;
                // Debug.WriteLine("Needed for MD5-hash (1): " + ((endTime - startTime) / TimeSpan.TicksPerMillisecond).ToString());  // about 80 ms
            }

            string toSign = string.Empty;

            if (useSharedKeyLite)
            {
                toSign = String.Format("{0}\n{1}", ptimeStamp, canonicalResource);
            }
            else
            {
                toSign = String.Format("{0}\n{4}\n{1}\n{2}\n{3}", pHttpVerb, contentType, ptimeStamp, canonicalResource, pMD5Hash);
            }

            string signature;

            #region Region: Tests to use SPWF04SA for SHA256 encoding (not used)
            //toSign = @"POST\n56487EFE04B9981AB97DE7D20353F298\napplication/atom+xml\nTue, 29 Jan 2019 22:38:48 GMT\n/roschmi01/Tables()";

            /*
             * wifi.CreateRamFile("fileSHA256", Convert.FromBase64String(_account.AccountKey));
             * string strSHA256Hash = Encoding.UTF8.GetString(Program.wifi.ComputeHash("2", "fileSHA256")).Substring(7);
             * byte[] theHmac = Convert.FromBase64String(strSHA256Hash);
             */
            //var theHmac = wifi.ComputeHash("2", "fileSHA256");
            #endregion

            //long startTime = DateTime.Now.Ticks;

            //RoSchmi debugging
            //byte[] decodedKey = Convert.FromBase64String(_account.AccountKey);

            var hmac      = new PervasiveDigital.Security.ManagedProviders.HMACSHA256(Convert.FromBase64String(_account.AccountKey));
            var hmacBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(toSign));
            signature = Convert.ToBase64String(hmacBytes).Replace("!", "+").Replace("*", "/");

            /*
             * var hmac = new PervasiveDigital.Security.ManagedProviders.HMACSHA256(Convert.FromBase64String(_account.AccountKey));
             * var hmacBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes("Roland"));
             * signature = Convert.ToBase64String(hmacBytes).Replace("!", "+").Replace("*", "/");
             */


            // long endTime = DateTime.Now.Ticks;
            // Debug.WriteLine("Needed for MD5SHA256-hash: " + ((endTime - startTime) / TimeSpan.TicksPerMillisecond).ToString());  // about 160 ms



            if (useSharedKeyLite)
            {
                return("SharedKeyLite " + _account.AccountName + ":" + signature);
            }
            else
            {
                return("SharedKey " + _account.AccountName + ":" + signature);
            }
        }
Exemple #2
0
        //protected byte[] GetPackageFileBytesAndLength(string fileName, out int contentLength)
        //{
        //    byte[] ms = null;
        //    contentLength = 0;
        //    if (fileName != null)
        //    {
        //        using (StreamReader sr = new StreamReader(File.Open(fileName, FileMode.Open)))
        //        {
        //            string data = sr.ReadToEnd();
        //            ms = Encoding.UTF8.GetBytes(data);
        //            contentLength = ms.Length;
        //        }
        //    }
        //    return ms;
        //}
        protected string CreateAuthorizationHeader(string canResource, string options = "", int contentLength = 0)
        {
            string toSign = StringUtilities.Format("{0}\n\n\n{1}\n\n\n\n\n\n\n\n{5}\nx-ms-date:{2}\nx-ms-version:{3}\n{4}",
                                          HttpVerb, contentLength, GetDateHeader(), VersionHeader, canResource, options);

            var hmac = new HMACSHA256(Convert.FromBase64String(_account.AccountKey));
            var hmacBytes = hmac.ComputeHash(Encoding.UTF8.GetBytes(toSign));
            string signature = Convert.ToBase64String(hmacBytes).Replace("!", "+").Replace("*", "/"); ;
            return "SharedKey " + _account.AccountName + ":" + signature;
        }