/// <summary>
        /// In case for some some crazed reason you want a client to validate the specified token rather than just letting the creator be authoritative.
        /// </summary>
        /// <returns>Validated?</returns>
        /// <param name="token">The token to be validated.</param>
        public static bool Validate(string token, string secret = null)
        {
            byte[]           secretBytes      = EncodingHelper.GetBytes(secret);
            AlgorithmFactory algorithmFactory = new AlgorithmFactory();

            var tokenDecoded = DecodeToken(token);

            bool secretValid     = string.IsNullOrEmpty(secret);
            bool expirationValid = JsonConvert.DeserializeObject <JwtExpiration>(tokenDecoded.Payload).Expiration == null;

            // actually check the secret
            if (secret != null)
            {
                var alg = algorithmFactory.Create(tokenDecoded.Header.Algorithm);

                var bytesToSign = EncodingHelper.GetBytes(String.Concat(JsonConvert.SerializeObject(tokenDecoded.Header), ".", tokenDecoded.Payload));

                var testSignature        = alg.Sign(secretBytes, bytesToSign);
                var decodedTestSignature = Convert.ToBase64String(testSignature);

                secretValid = decodedTestSignature == tokenDecoded.Verification;
            }

            //actually check the expiration
            var expiration = JsonConvert.DeserializeObject <JwtExpiration>(tokenDecoded.Payload).Expiration;

            if (expiration != null)
            {
                expirationValid = DateTimeHelpers.FromUnixTime((long)expiration) < DateTime.Now;
            }

            return(secretValid && expirationValid);
        }
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += UnhandledException;

            Console.WriteLine("Reading configuration");

            string algorithmConfigurationFileName = "configuration.json";
            string algorithmConfigurationFilePath = Path.Combine(Directory.GetCurrentDirectory(), algorithmConfigurationFileName);

            if (File.Exists(algorithmConfigurationFilePath) == false)
            {
                throw new FileNotFoundException($"{algorithmConfigurationFileName} not found at {Directory.GetCurrentDirectory()}");
            }

            string outputDirectory = "Output";

            if (Directory.Exists(outputDirectory))
            {
                Directory.Delete(outputDirectory, true);
            }

            var algorithm = AlgorithmFactory.Create(algorithmConfigurationFilePath);

            Console.WriteLine($"{algorithm.Name} algorithm is running....");
            outputDirectory = algorithm.Run();
            Console.WriteLine("Algorithm has completed");

            WaitKeyPress();

            Process.Start(Path.Combine(Directory.GetCurrentDirectory(), outputDirectory));
        }
Ejemplo n.º 3
0
        public double ResultAlgorithm(int numberOfAlg)
        {
            AlgorithmFactory fuctory   = new AlgorithmFactory(numberOfAlg, CompliteCodeMain, CompliteCodeChild);
            IAlgorithm       algorithm = fuctory.Create();

            algorithm?.CompareRes();
            return(algorithm.Result);
        }
Ejemplo n.º 4
0
        private static void RunAlgorithm()
        {
            IFigure           startingContainer;
            IAlgorithm        algorithm;
            Stopwatch         stopwatch        = new Stopwatch();
            IContainerFactory containerFactory = new ContainerFactory();
            IAlgorithmFactory factory          = new AlgorithmFactory();



            if (Properties.Dimensionality == AlgorithmDimensionality.TwoDimensional)
            {
                startingContainer = containerFactory.Create(Properties, ContainerWidth, ContainerHeight);
                algorithm         = factory.Create(Properties, startingContainer);
            }
            else
            {
                startingContainer = containerFactory.Create(Properties, ContainerWidth, ContainerHeight, ContainerDepth);
                algorithm         = factory.Create(Properties, startingContainer);
            }

            ObjectSet objectsToPack = LoadObjectSet();

            if (!CheckContainerSize(objectsToPack))
            {
                throw new InvalidContainerSizeException("Container is not big enough to contain biggest object. Enlarge the container.");
            }


            stopwatch.Reset();

            var sortedObjects = SortingHelper.Sort(objectsToPack, Ordering);

            stopwatch.Start();
            algorithm.Execute(sortedObjects);
            stopwatch.Stop();

            var endResults = algorithm.CreateResults();

            endResults.ExecutionTime = stopwatch.ElapsedMilliseconds;

            WriteResiltsToCsv(endResults, startingContainer);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Decodes secred data from Image
        /// </summary>
        /// <param name="algorithm">Algorithm used in decoding</param>
        /// <exception cref="System.NullReferenceException">Thrown if the <paramref name="algorithm"/> is is not known algorithm type.</exception>
        /// <exception cref="System.InvalidOperationException">TThrown if the <paramref name="algorithm"/> does not inherit from StegoAlgorithm.</exception>
        /// <exception cref="StegoCore.Exceptions.DecodeException">Thrown if error while decoding occurs</exception>
        /// <returns>Bytes of decoded secred data</returns>
        public byte[] Decode(AlgorithmEnum algorithm)
        {
            if (this.image == null)
            {
                throw new System.NullReferenceException("Image cannot be null");
            }
            var alg = AlgorithmFactory.Create(algorithm);

            return(alg.Decode(this.image, this.settings));
        }
Ejemplo n.º 6
0
        public void LSB_Embed_And_Decode_SecretDataLength()
        {
            var lsb = AlgorithmFactory.Create(AlgorithmEnum.Lsb);

            byte[] secretDataBytes = System.IO.File.ReadAllBytes(FileHelper.GetPathToSecretData());
            var    secretData      = new SecretData(secretDataBytes);
            var    imageWithSecret = lsb.Embed(Image.Load(FileHelper.GetPathToImage()), secretData, null);
            int    readedLength    = lsb.ReadSecretLength(imageWithSecret, null);

            Assert.Equal(secretDataBytes.Length, readedLength);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Embed secret data in Image
        /// </summary>
        /// <param name="algorithm">Algorithm used in embending</param>
        /// <exception cref="System.NullReferenceException">Thrown if the <paramref name="algorithm"/> is is not known algorithm type.</exception>
        /// <exception cref="System.InvalidOperationException">TThrown if the <paramref name="algorithm"/> does not inherit from StegoAlgorithm.</exception>
        /// <exception cref="StegoCore.Exceptions.DataToBigException">Thrown if the secred data is to big for embending</exception>
        /// <returns>Image with embeded secret data</returns>
        public Image <Rgba32> Embed(AlgorithmEnum algorithm)
        {
            if (this.image == null)
            {
                throw new System.NullReferenceException("Image cannot be null");
            }
            if (this.secretData == null)
            {
                throw new System.NullReferenceException("Secret data cannot be null");
            }
            var alg = AlgorithmFactory.Create(algorithm);

            return(alg.Embed(this.image, this.secretData, this.settings));
        }
Ejemplo n.º 8
0
        private void Encrypt_Decrypt(AlgorithmEnum alg, IImageFormat imageFormat, string outFileName)
        {
            Configuration.Default.AddImageFormat(imageFormat);

            var algorithm = AlgorithmFactory.Create(alg);

            byte[] secretDataBytes = System.IO.File.ReadAllBytes(FileHelper.GetPathToSecretData());

            var notSavedStego = EncryptAndSave(algorithm, secretDataBytes, outFileName);

            var stegoImage = Image.Load(outFileName);

            byte[] resultSecret = algorithm.Decode(notSavedStego, null);

            Assert.Equal(secretDataBytes, resultSecret);
        }
Ejemplo n.º 9
0
        public void AlgorithmFactory_CreateLsb_Type_Equals()
        {
            var created = AlgorithmFactory.Create(AlgorithmEnum.Lsb);

            Assert.Equal(typeof(Lsb), created.GetType());
        }