Exemplo n.º 1
0
        /*
         * · Simular la web php: http://sandbox.onlinephpfunctions.com
         *  <?php
         *  if (function_exists("gzinflate"))
         *       echo "gzinflate OK \n";
         *  else echo "gzinflate NO";
         *
         *  $a = gzinflate(base64_decode("cy0qyi9SyC2tBOOC1KJ8hcLSVCAHCCoV0osSy1IB"));
         *  echo $a;
         *  ?>
         * · Simular la logica del app: https://dotnetfiddle.net
         *  using System;
         *  using System.IO;
         *  using System.IO.Compression;
         *  using System.Text;
         *
         *  public class Program
         *  {
         *      public static void Main()
         *      {
         *          using ( var outStream = new MemoryStream () )
         *          {
         *              string input = "Error muy muy pero que muuuuy grave";
         *
         *              using ( DeflateStream stream = new DeflateStream ( outStream, CompressionMode.Compress ) )
         *                  using (var ms = new MemoryStream ( Encoding.UTF8.GetBytes ( input ) ) )
         *                      ms.CopyTo ( stream );
         *
         *              string converted = Convert.ToBase64String ( outStream.ToArray() );
         *
         *              Console.WriteLine ( converted );
         *          }
         *      }
         *  }
         * · Convertir a url: https://www.url-encode-decode.com
         */

        private static string Compress_Logic(string input, ALGORITHM algorithm)
        {
            if (algorithm == ALGORITHM.NOTHING)
            {
                return(Convert.ToBase64String(Encoding.UTF8.GetBytes(input)));
            }

            using (var outStream = new MemoryStream())
            {
                dynamic stream = null;
                switch (algorithm)
                {
                case ALGORITHM.DEFLATE:
                    stream = new DeflateStream(outStream, CompressionMode.Compress);
                    break;

                case ALGORITHM.GZIP:
                    stream = new GZipStream(outStream, CompressionMode.Compress);
                    break;
                }

                using (stream)
                    using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(input)))
                        ms.CopyTo(stream);

                return(Convert.ToBase64String(outStream.ToArray()));
            }
        }
Exemplo n.º 2
0
        private static string Decompress_Logic(string input, ALGORITHM algorithm)
        {
            if (algorithm == ALGORITHM.NOTHING)
            {
                return(Encoding.UTF8.GetString(Convert.FromBase64String(input)));
            }

            using (var inStream = new MemoryStream(Convert.FromBase64String(input)))
            {
                dynamic stream = null;
                switch (algorithm)
                {
                case ALGORITHM.DEFLATE:
                    stream = new DeflateStream(inStream, CompressionMode.Decompress);
                    break;

                case ALGORITHM.GZIP:
                    stream = new GZipStream(inStream, CompressionMode.Decompress);
                    break;
                }

                using ( stream )
                {
                    using (var ms = new MemoryStream())
                    {
                        stream.CopyTo(ms);
                        return(Encoding.UTF8.GetString(ms.ToArray()));
                    }
                }
            }
        }
Exemplo n.º 3
0
 public PasswordHashing(
     byte saltSize       = DEFAULT_SALT_SIZE,
     byte complexity     = DEFAULT_COMPLEXITY,
     ALGORITHM algorithm = ALGORITHM.DEFAULT)
 {
     _saltSize   = saltSize;
     _algorithm  = algorithm;
     _complexity = complexity;
 }
Exemplo n.º 4
0
        public static string Decompress(string input, ALGORITHM algorithm = ALGORITHM.DEFLATE, int times = 1)
        {
            string result = Decompress_Logic(input, algorithm);

            if (--times > 0)
            {
                return(Decompress(result, algorithm, times));
            }
            return(result);
        }
Exemplo n.º 5
0
        public static string CompressToUrlUsingGlobal(string input, int times = 1)
        {
            string paramGlobal = Singleton.Get.Configuration.Global.Compression.ToLower();

            ALGORITHM algorithm = ALGORITHM.NOTHING;

            switch (paramGlobal)
            {
            case DEFLATE: algorithm = ALGORITHM.DEFLATE; break;

            case GZIP: algorithm = ALGORITHM.GZIP;    break;
            }

            return(HttpUtility.UrlEncode(Compress(input, algorithm, times)));
        }
Exemplo n.º 6
0
        private TimeSpan RunTest(string file, IWeightedGraph <int, double> graph, ALGORITHM algorithm)
        {
            Stopwatch sw = new Stopwatch();

            switch (algorithm)
            {
            case ALGORITHM.KRUSKAL:
                sw.Start();
                Algorithms.MinimumSpanningTree.Kruskal.FindMinimumSpanningTree(graph);
                sw.Stop();
                break;

            case ALGORITHM.PRIM:
                sw.Start();
                Algorithms.MinimumSpanningTree.Prim.FindMinimumSpanningTree(graph, 0, double.MaxValue);
                sw.Stop();
                break;
            }

            Logger.LogDebug($" - Computed MST for '{file}' using {algorithm}'s algorithm in {sw.Elapsed}.");

            return(sw.Elapsed);
        }
Exemplo n.º 7
0
 public static string CompressToUrl(string input, ALGORITHM algorithm = ALGORITHM.DEFLATE, int times = 1)
 {
     return(HttpUtility.UrlEncode(Compress(input, algorithm, times)));
 }
Exemplo n.º 8
0
        private HashedPassword Compute(string password, byte[] salt, byte complexity, ALGORITHM algorithm)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentNullException("Plain text password cannot be null, empty or whitespace");
            }

            HashedPassword hashedPassword = new HashedPassword(salt, complexity, algorithm);

            using (DeriveBytes provider = GetHashingProvider(password,
                                                             hashedPassword.Salt, hashedPassword.Iterations, hashedPassword.Algorithm))
            {
                hashedPassword.Content = provider.GetBytes(hashedPassword.Content.Length);
            }

            return(hashedPassword);
        }
Exemplo n.º 9
0
        private DeriveBytes GetHashingProvider(string password, byte[] salt, int iterations, ALGORITHM algorithm)
        {
            DeriveBytes provider = null;

            switch (algorithm)
            {
            case ALGORITHM.PBKDF1:
                byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
                provider = new PasswordDeriveBytes(passwordBytes, salt, "SHA1", iterations);
                break;

            default:
            case ALGORITHM.PBKDF2:
                provider = new Rfc2898DeriveBytes(password, salt, iterations);
                break;
            }

            return(provider);
        }
Exemplo n.º 10
0
        private void TestMstAlgorithm(IWeightedGraph <int, double> graph, double expectedMstWeight, double precision, ALGORITHM algorithm)
        {
            // Build minimum spanning tree with algorithm to test
            IWeightedGraph <int, double> msp;

            switch (algorithm)
            {
            case ALGORITHM.KRUSKAL:
                msp = Kruskal.FindMinimumSpanningTree(graph);
                break;

            case ALGORITHM.PRIM:
                msp = Prim.FindMinimumSpanningTree(graph, 0, double.MaxValue);
                break;

            default:
                throw new NotImplementedException($"No minimum spanning tree test implemented for algorithm {algorithm.ToString()}.");
            }

            // Check that all verteces are included
            Assert.AreEqual(graph.VertexCount, msp.VertexCount);

            // Count edges and compute total weight
            IEnumerable <IWeightedEdge <int, double> > edges = msp.GetAllEdges();
            int    edgeCount = edges.Count();
            double mspWeight = edges.Sum(e => e.Weight);

            // Check edge count
            Assert.AreEqual(graph.VertexCount - 1, edgeCount);

            // Check that the total weight is as expected
            AssertDoublesNearlyEqual(expectedMstWeight, mspWeight, precision);
        }