示例#1
0
        public static PathDefinition FromDto(PathDto dto)
        {
            var pathDefinition = CreateInstance <PathDefinition>();

            pathDefinition.Name          = dto.Name;
            pathDefinition.SpawnPosition = dto.SpawnPosition;
            pathDefinition.WayPoints     = dto.WayPoints;
            return(pathDefinition);
        }
示例#2
0
        public async Task UpdatePath(PathDto path)
        {
            var pathEntity = _mapper.Map <PathDto, Path>(path);

            _context.Cities.Attach(pathEntity.InitialCity);
            _context.Cities.Attach(pathEntity.EndCity);
            _context.Update(pathEntity);
            await _context.SaveChangesAsync();
        }
示例#3
0
 public static PathEntity ToEntity(this PathDto dto)
 {
     return(new PathEntity
     {
         Id = dto.Id,
         Value = dto.Value,
         Path = dto.Path,
         Moves = dto.Moves
     });
 }
示例#4
0
 public static PathEntity ToEntity(this PathDto dto, int[] array)
 {
     return(new PathEntity
     {
         Id = dto.Id,
         Value = array,
         Path = dto.Path,
         Moves = dto.Moves
     });
 }
示例#5
0
        public ActionResult Encrypt([FromBody] PathDto path, [FromHeader] string authorization)
        {
            try
            {
                var isAuthorized = _context.ApiKeys.Any(a => a.Key == authorization);
                if (String.IsNullOrEmpty(authorization) || !isAuthorized)
                {
                    return(Unauthorized("Authorization mismatch."));
                }

                if (String.IsNullOrEmpty(path.InputPath) || !System.IO.File.Exists(path.InputPath))
                {
                    return(BadRequest("Invaild input path."));
                }

                if (String.IsNullOrEmpty(path.OutputPath) || !Directory.Exists(Path.GetDirectoryName(path.OutputPath)))
                {
                    return(BadRequest("Invaild output path."));
                }
                Console.WriteLine("Encryption started...");

                var startTime = DateTime.Now;

                var res = Services.Crypto.Encrypt(path);

                //foreach (string file in Directory.EnumerateFiles(path.InputPath, "*.xml"))
                //{
                //    string contents = System.IO.File.ReadAllText(file);
                //}


                var endTime = DateTime.Now;

                Console.WriteLine("Encryption successful...");

                //save the history
                History history = new History();
                history.FileTitle      = Path.GetFileName(path.InputPath);
                history.InputFilePath  = path.InputPath;
                history.OutputFilePath = path.OutputPath;
                history.StartTime      = startTime;
                history.OperationType  = 1;
                history.EndTime        = endTime;
                _context.Add(history);
                _context.SaveChanges();

                //var kdj = Image.Jpeg(path.InputPath);
                return(Ok("Successful"));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        internal IPath[][] ConvertPathDtos(PathDto[][] pathDtos)
        {
            var paths = new IPath[pathDtos.Length][];
            var i = 0;

            foreach ( PathDto[] dtos in pathDtos )
            {
                paths [ i++ ] = dtos.Select(ConvertPathDto).ToArray();
            }

            return paths;
        }
示例#7
0
        public static PathDto FindPath(int[] arr)
        {
            int n = arr.Length;

            var moves = CalculateMoves(arr, n);

            if (moves == null)
            {
                return(null);
            }

            var moveCount = moves.ToList();

            var indexes = new List <int>();

            var path = new List <int>();

            moveCount.RemoveAt(0);

            for (int i = 0; i < moveCount.Count; i++)
            {
                if (i < moveCount.Count - 1 && moveCount[i] != moveCount[i + 1])
                {
                    if (moveCount[i] == 1)
                    {
                        indexes.Add(i);
                        indexes.Add(i + 1);
                    }
                    else
                    {
                        indexes.Add(i + 1);
                    }
                }
            }

            foreach (var index in indexes)
            {
                path.Add(arr[index]);
            }

            path.Add(arr[arr.Length - 1]);

            var result = new PathDto
            {
                Moves = moveCount[moveCount.Count - 1],
                Path  = path
            };

            return(result);
        }
示例#8
0
        public async Task <PathDto> InsertPath(PathDto path)
        {
            var pathEntity = _mapper.Map <PathDto, Path>(path);

            _context.Cities.Attach(pathEntity.InitialCity);
            _context.Cities.Attach(pathEntity.EndCity);
            await _context.Paths.AddAsync(pathEntity);

            await _context.SaveChangesAsync();

            var pathDto = _mapper.Map <Path, PathDto>(pathEntity);

            return(pathDto);
        }
示例#9
0
文件: Crypto.cs 项目: rkxbytes/crypto
        private static string Decrypt(PathDto path,
                                      string passPhrase,
                                      string saltValue,
                                      string hashAlgorithm,
                                      int passwordIterations,
                                      string initVector,
                                      int keySize)
        {
            // Convert strings defining encryption key characteristics into byte
            // arrays. Let us assume that strings only contain ASCII codes.
            // If strings include Unicode characters, use Unicode, UTF7, or UTF8
            // encoding.
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes  = Encoding.ASCII.GetBytes(saltValue);

            string text = System.IO.File.ReadAllText(path.InputPath);

            // Convert our ciphertext into a byte array.
            byte[] cipherTextBytes = Convert.FromBase64String(text);

            // First, we must create a password, from which the key will be
            // derived. This password will be generated from the specified
            // passphrase and salt value. The password will be created using
            // the specified hash algorithm. Password creation can be done in
            // several iterations.
            PasswordDeriveBytes password = new PasswordDeriveBytes(
                passPhrase,
                saltValueBytes,
                hashAlgorithm,
                passwordIterations);

            // Use the password to generate pseudo-random bytes for the encryption
            // key. Specify the size of the key in bytes (instead of bits).
            byte[] keyBytes = password.GetBytes(keySize / 8);

            // Create uninitialized Rijndael encryption object.
            RijndaelManaged symmetricKey = new RijndaelManaged();

            // It is reasonable to set encryption mode to Cipher Block Chaining
            // (CBC). Use default options for other symmetric key parameters.
            symmetricKey.Mode = CipherMode.CBC;

            // Generate decryptor from the existing key bytes and initialization
            // vector. Key size will be defined based on the number of the key
            // bytes.
            ICryptoTransform decryptor = symmetricKey.CreateDecryptor(
                keyBytes,
                initVectorBytes);

            // Define memory stream which will be used to hold encrypted data.
            MemoryStream memoryStream = new MemoryStream(cipherTextBytes);

            // Define cryptographic stream (always use Read mode for encryption).
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                         decryptor,
                                                         CryptoStreamMode.Read);

            // Since at this point we don't know what the size of decrypted data
            // will be, allocate the buffer long enough to hold ciphertext;
            // plaintext is never longer than ciphertext.
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];

            // Start decrypting.
            int decryptedByteCount = cryptoStream.Read(plainTextBytes,
                                                       0,
                                                       plainTextBytes.Length);

            // Close both streams.
            memoryStream.Close();
            cryptoStream.Close();

            // Convert decrypted data into a string.
            // Let us assume that the original plaintext string was UTF8-encoded.
            string plainText = Encoding.UTF8.GetString(plainTextBytes,
                                                       0,
                                                       decryptedByteCount);

            File.WriteAllBytes(path.OutputPath, plainTextBytes);
            // Return decrypted string.
            return(plainText);
        }
        public void ConvertPathDto_SetsDto_WhenCalled()
        {
            // Arrange
            var dto = new PathDto();
            RacetracksDtoToRacetracksConverter sut = CreateSut();

            // Act
            sut.ConvertPathDto(dto);

            // Assert
            Assert.AreEqual(dto,
                            m_PathDtoToPath.Dto);
        }
示例#11
0
        public async Task <PathDto> Create(PathDto path)
        {
            var result = await _pathRepository.InsertPath(path);

            return(result);
        }
示例#12
0
 public async Task Update(PathDto path)
 {
     await _pathRepository.UpdatePath(path);
 }
示例#13
0
文件: Crypto.cs 项目: rkxbytes/crypto
 public static string Decrypt(PathDto path)
 {
     return(Decrypt(path, _passPhrase, _saltValue, _hashAlgorithm, _passwordIterations, _initVector, _keySize));
 }
        public void ConvertPathDto_ReturnsPath_WhenCalled()
        {
            // Arrange
            var path = Substitute.For <IPath>();
            m_PathDtoToPath.Path.Returns(path);
            var dto = new PathDto();
            RacetracksDtoToRacetracksConverter sut = CreateSut();

            // Act
            IPath actual = sut.ConvertPathDto(dto);

            // Assert
            Assert.AreEqual(path,
                            actual);
        }
        public void ConvertPathDto_CallsConvert_WhenCalled()
        {
            // Arrange
            var dto = new PathDto();
            RacetracksDtoToRacetracksConverter sut = CreateSut();

            // Act
            sut.ConvertPathDto(dto);

            // Assert
            m_PathDtoToPath.Received().Convert();
        }
        public void ConvertPathDtos_ReturnsEmptyArray_ForEmptyDtoArray()
        {
            // Arrange
            var dtos = new PathDto[0][];
            RacetracksDtoToRacetracksConverter sut = CreateSut();

            // Act
            IPath[][] actual = sut.ConvertPathDtos(dtos);

            // Assert
            Assert.False(actual.Any());
        }
示例#17
0
文件: Crypto.cs 项目: rkxbytes/crypto
        private static string Encrypt(PathDto path,
                                      string passPhrase,
                                      string saltValue,
                                      string hashAlgorithm,
                                      int passwordIterations,
                                      string initVector,
                                      int keySize)
        {
            byte[] bytes = File.ReadAllBytes(path.InputPath);


            //Image image1 = Image.FromFile("c:\\FakePhoto1.jpg");

            // Convert strings into byte arrays.
            // Let us assume that strings only contain ASCII codes.
            // If strings include Unicode characters, use Unicode, UTF7, or UTF8
            // encoding.
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes  = Encoding.ASCII.GetBytes(saltValue);

            // Convert our plaintext into a byte array.
            // Let us assume that plaintext contains UTF8-encoded characters.
            //byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

            // First, we must create a password, from which the key will be derived.
            // This password will be generated from the specified passphrase and
            // salt value. The password will be created using the specified hash
            // algorithm. Password creation can be done in several iterations.
            PasswordDeriveBytes password = new PasswordDeriveBytes(
                passPhrase,
                saltValueBytes,
                hashAlgorithm,
                passwordIterations);

            // Use the password to generate pseudo-random bytes for the encryption
            // key. Specify the size of the key in bytes (instead of bits).
            byte[] keyBytes = password.GetBytes(keySize / 8);

            // Create uninitialized Rijndael encryption object.
            RijndaelManaged symmetricKey = new RijndaelManaged();

            // It is reasonable to set encryption mode to Cipher Block Chaining
            // (CBC). Use default options for other symmetric key parameters.
            symmetricKey.Mode = CipherMode.CBC;

            // Generate encryptor from the existing key bytes and initialization
            // vector. Key size will be defined based on the number of the key
            // bytes.
            ICryptoTransform encryptor = symmetricKey.CreateEncryptor(
                keyBytes,
                initVectorBytes);

            // Define memory stream which will be used to hold encrypted data.
            MemoryStream memoryStream = new MemoryStream();

            // Define cryptographic stream (always use Write mode for encryption).
            CryptoStream cryptoStream = new CryptoStream(memoryStream,
                                                         encryptor,
                                                         CryptoStreamMode.Write);

            // Start encrypting.
            cryptoStream.Write(bytes, 0, bytes.Length);

            // Finish encrypting.
            cryptoStream.FlushFinalBlock();

            // Convert our encrypted data from a memory stream into a byte array.
            byte[] cipherTextBytes = memoryStream.ToArray();

            // Close both streams.
            memoryStream.Close();
            cryptoStream.Close();

            // Convert encrypted data into a base64-encoded string.
            string cipherText = Convert.ToBase64String(cipherTextBytes);


            System.IO.File.WriteAllText(path.OutputPath, cipherText);
            // Return encrypted string.
            return(cipherText);
        }