Example #1
0
        // Make async
        public User Authenticate(string username, string password)
        {
            var fetchedUser = _context.AdminUsers.Where(x => x.Username == username).FirstOrDefault();

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

            var hashedIncomingPassword = HashingFunction.HashPassword(password, fetchedUser.Salt);


            if (hashedIncomingPassword != fetchedUser.HashedPassword)
            {
                return(null);
            }

            var user = new User
            {
                Username = fetchedUser.Username,
                Id       = fetchedUser.Id
            };

            var tokenHandler    = new JwtSecurityTokenHandler();
            var key             = Encoding.ASCII.GetBytes(_appSettings.Secret);
            var tokenDescriptor = new SecurityTokenDescriptor
            {
                Subject = new ClaimsIdentity(new Claim[]
                {
                    new Claim(ClaimTypes.Name, user.Id.ToString())
                }),
                Expires            = DateTime.UtcNow.AddHours(1),
                SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
            };
            var token = tokenHandler.CreateToken(tokenDescriptor);

            user.Token = tokenHandler.WriteToken(token);

            user.Password = null;

            return(user);
        }
Example #2
0
        /// <summary>
        /// Gets the checksum hash value from the FTP server for the file specified.  Use this value to compare a local checksum to determine file integrity.
        /// </summary>
        /// <param name="hash">Hashing function to use.</param>
        /// <param name="path">Path to the file on the remote FTP server.</param>
        /// <param name="startPosition">Byte position of where the server should begin computing the hash.</param>
        /// <param name="endPosition">Byte position of where the server should end computing the hash.</param>
        /// <returns>Checksum hash value in a string format.</returns>
        /// <seealso cref="ComputeChecksum(HashingFunction, string)"/>
        public string GetChecksum(HashingFunction hash, string path, long startPosition, long endPosition)
        {
            if (hash == HashingFunction.None)
                throw new ArgumentOutOfRangeException("hash", "must contain a value other than 'Unknown'");

            if (startPosition < 0)
                throw new ArgumentOutOfRangeException("startPosition", "must contain a value greater than or equal to 0");

            if (endPosition < 0)
                throw new ArgumentOutOfRangeException("startPosition", "must contain a value greater than or equal to 0");

            if (startPosition > endPosition)
                throw new ArgumentOutOfRangeException("startPosition", "must contain a value less than or equal to endPosition");

            FtpCmd command = FtpCmd.Unknown;

            switch (hash)
            {
                case HashingFunction.Crc32:
                    command = FtpCmd.Xcrc;
                    break;

                case HashingFunction.Md5:
                    command = FtpCmd.Xmd5;
                    break;
                case HashingFunction.Sha1:
                    command = FtpCmd.Xsha1;
                    break;
            }

            // send request to server to get the hash value for the file
            // if the restartposition is > 0 then computer the hash on the segment that we resent
            if (startPosition > 0)
                SendRequest(new FtpRequest(_encoding, command, path, startPosition.ToString(), endPosition.ToString()));
            else
                SendRequest(new FtpRequest(_encoding, command, path));

            return _response.Text;
        }
Example #3
0
 /// <summary>
 /// Gets the checksum value from the FTP server for the file specified.  Use this value to compare a local checksum to determine file integrity.
 /// </summary>
 /// <param name="hash">Hashing function to use.</param>
 /// <param name="path">Path to the file ont the remote FTP server.</param>
 /// <returns>Hash value in a string format.</returns>
 /// <seealso cref="ComputeChecksum(HashingFunction, string)"/>
 public string GetChecksum(HashingFunction hash, string path)
 {
     return GetChecksum(hash, path, 0, 0);
 }
Example #4
0
 /// <summary>
 /// Computes a checksum for a Stream object.
 /// </summary>
 /// <param name="hash">Hashing function to use.</param>
 /// <param name="inputStream">Any System.IO.Stream object.</param>
 /// <returns>Hash value in a string format.</returns>
 /// <remarks>
 /// The Stream object must allow reads and must allow seeking.
 /// </remarks>
 /// <seealso cref="GetChecksum(HashingFunction, string)"/>
 public string ComputeChecksum(HashingFunction hash, Stream inputStream)
 {
     return ComputeChecksum(hash, inputStream, 0);
 }
Example #5
0
        /// <summary>
        /// Computes a checksum for a local file.
        /// </summary>
        /// <param name="hash">Hashing function to use.</param>
        /// <param name="localPath">Path to file to perform checksum operation on.</param>
        /// <returns>Hash value in a string format.</returns>
        /// <seealso cref="GetChecksum(HashingFunction, string)"/>
        public string ComputeChecksum(HashingFunction hash, string localPath)
        {
            if (!File.Exists(localPath))
                throw new ArgumentException("file does not exist.", "localPath");

            using (FileStream fileStream = File.OpenRead(localPath))
            {
                return ComputeChecksum(hash, fileStream);
            }
        }
Example #6
0
        /// <summary>
        /// Computes a checksum value for a Stream object.
        /// </summary>
        /// <param name="hash">Hashing function to use.</param>
        /// <param name="inputStream">Any System.IO.Stream object.</param>
        /// <param name="startPosition">Byte position of where the hash computation should begin.</param>
        /// <returns>Hash value in a string format.</returns>
        /// <remarks>
        /// The Stream object must allow reads and must allow seeking.
        /// </remarks>
        /// <seealso cref="GetChecksum(HashingFunction, string)"/>
        public static string ComputeChecksum(HashingFunction hash, Stream inputStream, long startPosition)
        {
            if (hash == HashingFunction.None)
                throw new ArgumentOutOfRangeException("hash", "must contain a value other than 'Unknown'");

            if (inputStream == null)
                throw new ArgumentNullException("inputStream");

            if (!inputStream.CanRead)
                throw new ArgumentException("must be readable.  The CanRead property must return a value of 'true'.", "inputStream");

            if (!inputStream.CanSeek)
                throw new ArgumentException("must be seekable.  The CanSeek property must return a value of 'true'.", "inputStream");

            if (startPosition < 0)
                throw new ArgumentOutOfRangeException("startPosition", "must contain a value greater than or equal to 0");

            HashAlgorithm hashAlgo = null;

            switch (hash)
            {
                case HashingFunction.Crc32:
                    hashAlgo = new Starksoft.Hashing.Crc32();
                    break;
                case HashingFunction.Md5:
                    hashAlgo = new MD5CryptoServiceProvider();
                    break;
                case HashingFunction.Sha1:
                    hashAlgo = new SHA1CryptoServiceProvider();
                    break;
            }

            if (startPosition > 0)
                inputStream.Position = startPosition;
            else
                inputStream.Position = 0;

            byte[] hashArray = hashAlgo.ComputeHash(inputStream);

            // convert byte array to a string
            StringBuilder buffer = new StringBuilder(hashArray.Length);
            foreach (byte hashByte in hashArray)
            {
                buffer.Append(hashByte.ToString("x2"));
            }

            return buffer.ToString();
        }
Example #7
0
 public HashStorage()
 {
     _calculateHash    = CheckTypeAndGetHashingFunction();
     _storage          = new LinkedList <T> [_minSize];
     _numberOfElements = 0;
 }
Example #8
0
        public string GetChecksum(HashingFunction hash, string path, long startPosition, long endPosition)
        {
            if (hash == HashingFunction.None)
            {
                throw new ArgumentOutOfRangeException("hash", "must contain a value other than 'Unknown'");
            }

            if (startPosition < 0)
            {
                throw new ArgumentOutOfRangeException("startPosition", "must contain a value greater than or equal to 0");
            }

            if (endPosition < 0)
            {
                throw new ArgumentOutOfRangeException("startPosition", "must contain a value greater than or equal to 0");
            }

            if (startPosition > endPosition)
            {
                throw new ArgumentOutOfRangeException("startPosition", "must contain a value less than or equal to endPosition");
            }

            FtpCmd command = FtpCmd.Unknown;

            switch (hash)
            {
                case HashingFunction.Crc32:
                    command = FtpCmd.Xcrc;
                    break;
                case HashingFunction.Md5:
                    command = FtpCmd.Xmd5;
                    break;
                case HashingFunction.Sha1:
                    command = FtpCmd.Xsha1;
                    break;
            }

            if (startPosition > 0)
            {
                SendRequest(new FtpRequest(_encoding, command, path, startPosition.ToString(), endPosition.ToString()));
            }
            else
            {
                SendRequest(new FtpRequest(_encoding, command, path));
            }

            return _response.Text;
        }