Beispiel #1
0
 /// <summary>
 /// Decrypts the specified data.
 /// </summary>
 /// <param name="data">The data.</param>
 /// <param name="key">The key.</param>
 /// <param name="salt">The salt.</param>
 /// <param name="hashingAlgorithm">The hashing algorithm.</param>
 /// <param name="passwordIterations">The password iterations.</param>
 /// <param name="initialVector">The initial vector with a length of 16 bytes.</param>
 /// <param name="keySize">Size of the key. (64, 128, 192, 256, etc.)</param>
 /// <returns>The decrypted data.</returns>
 public static byte[] Decrypt(this byte[] data,
                              byte[] key,
                              byte[] salt,
                              HashingAlgorithms hashingAlgorithm,
                              int passwordIterations,
                              byte[] initialVector,
                              int keySize,
                              SymmetricAlgorithms algorithm)
 {
     if (data is null || string.IsNullOrEmpty(algorithm) || key is null)
     {
         return(Array.Empty <byte>());
     }
     initialVector ??= Array.Empty <byte>();
     salt ??= Array.Empty <byte>();
     return(Canister.Builder.Bootstrapper?.Resolve <CryptoManager>()?.Decrypt(
                data,
                key,
                salt,
                hashingAlgorithm,
                passwordIterations,
                initialVector,
                keySize,
                algorithm) ?? Array.Empty <byte>());
 }
Beispiel #2
0
 /// <summary>
 /// Creates a PasswordDeriveBytes key.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="salt">The salt.</param>
 /// <param name="hashingAlgorithm">The hashing algorithm.</param>
 /// <param name="passwordIterations">The password iterations.</param>
 /// <returns>The resulting key.</returns>
 public static PasswordDeriveBytes CreateKey(byte[] key,
                                             byte[] salt,
                                             HashingAlgorithms hashingAlgorithm,
                                             int passwordIterations)
 {
     return(new PasswordDeriveBytes(key, salt, hashingAlgorithm, passwordIterations));
 }
        private Student GetStudentAccount(string userEmail, string userPrefFirstName, string userLastName, string userFaculty, string userHomePhone, string userMobile, string userBestContactNumber, string userDob, int userGenderType, int userAccountType, string userPass, string studentCourseType, int studentDegreeType, int studentDegreeYearType, int studentStatusType, string studentLanguage, string studentCountry, bool studentPermissionToUseData, string studentOtherEducationalBackground)
        {
            Student studentModel = new Student()
            {
                UserEmail             = userEmail,
                UserPrefFirstName     = userPrefFirstName,
                UserLastName          = userLastName,
                UserFaculty           = userFaculty,
                UserHomePhone         = userHomePhone,
                UserMobile            = userMobile,
                UserBestContactNumber = userBestContactNumber,
                UserDob                           = Convert.ToDateTime(userDob),
                UserGenderType                    = (UserGenderType)userGenderType,
                UserAccountType                   = (UserAccountType)userAccountType,
                UserHasLoggedIn                   = false,
                UserPass                          = HashingAlgorithms.ComputeMd5Hash(userPass),
                UserName                          = $"{userPrefFirstName} {userLastName}",
                StudentCourseType                 = studentCourseType,
                StudentDegreeType                 = (StudentDegreeType)studentDegreeType,
                StudentDegreeYearType             = (StudentDegreeYearType)studentDegreeYearType,
                StudentStatusType                 = (StudentStatusType)studentStatusType,
                StudentLanguage                   = studentLanguage,
                StudentCountry                    = studentCountry,
                StudentPermissionToUseData        = studentPermissionToUseData,
                StudentOtherEducationalBackground = studentOtherEducationalBackground
            };

            return(studentModel);
        }
Beispiel #4
0
 /// <summary>
 /// Hashes the specified algorithm.
 /// </summary>
 /// <param name="data">The data.</param>
 /// <param name="algorithm">The algorithm.</param>
 /// <param name="encoding">The encoding of the text (defaults to UTF8).</param>
 /// <returns>The hashed result.</returns>
 public static byte[] Hash(this string data, HashingAlgorithms algorithm, Encoding?encoding = null)
 {
     if (string.IsNullOrEmpty(data) || string.IsNullOrEmpty(algorithm))
     {
         return(Array.Empty <byte>());
     }
     return(Canister.Builder.Bootstrapper?.Resolve <CryptoManager>()?.Hash(data.ToByteArray(encoding), algorithm) ?? Array.Empty <byte>());
 }
Beispiel #5
0
 /// <summary>
 /// Hashes the specified data.
 /// </summary>
 /// <param name="data">The data.</param>
 /// <param name="algorithm">The algorithm.</param>
 /// <returns>The hashed result.</returns>
 public static byte[] Hash(this byte[] data, HashingAlgorithms algorithm)
 {
     if (data is null || string.IsNullOrEmpty(algorithm))
     {
         return(Array.Empty <byte>());
     }
     return(Canister.Builder.Bootstrapper?.Resolve <CryptoManager>()?.Hash(data, algorithm) ?? Array.Empty <byte>());
 }
Beispiel #6
0
 /// <summary>
 /// Decrypts the specified data.
 /// </summary>
 /// <param name="data">The data.</param>
 /// <param name="key">The key.</param>
 /// <param name="salt">The salt.</param>
 /// <param name="hashingAlgorithm">The hashing algorithm.</param>
 /// <param name="passwordIterations">The password iterations.</param>
 /// <param name="initialVector">The initial vector. 16 ASCII characters long.</param>
 /// <param name="keySize">
 /// Size of the key. Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
 /// </param>
 /// <returns>The decrypted data.</returns>
 public byte[] Decrypt(byte[] data, byte[] key, byte[] salt, HashingAlgorithms hashingAlgorithm, int passwordIterations, byte[] initialVector, int keySize)
 {
     if (data == null || key == null || salt == null || initialVector == null)
     {
         return(Array.Empty <byte>());
     }
     using PasswordDeriveBytes TempKey = new PasswordDeriveBytes(key, salt, hashingAlgorithm.ToString(), passwordIterations);
     return(Decrypt(data, TempKey, initialVector, keySize));
 }
Beispiel #7
0
        /// <summary>
        /// Hashes the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="algorithm">The algorithm to use.</param>
        /// <returns>The hash of the data.</returns>
        public byte[] Hash(byte[] data, HashingAlgorithms algorithm)
        {
            var HashingAlgorithm = Hashes.FirstOrDefault(x => string.Equals(x.Name, algorithm.ToString(), StringComparison.OrdinalIgnoreCase));

            if (HashingAlgorithm == null)
            {
                return(Array.Empty <byte>());
            }
            return(HashingAlgorithm.Hash(data));
        }
Beispiel #8
0
 /// <summary>
 /// Encrypts the specified data.
 /// </summary>
 /// <param name="data">The data.</param>
 /// <param name="key">The key.</param>
 /// <param name="salt">The salt.</param>
 /// <param name="hashingAlgorithm">The hashing algorithm.</param>
 /// <param name="passwordIterations">The password iterations.</param>
 /// <param name="initialVector">The initial vector with a length of 16 bytes.</param>
 /// <param name="keySize">Size of the key. (64, 128, 192, 256, etc.)</param>
 /// <param name="algorithm">The algorithm.</param>
 /// <param name="encoding">The encoding of the string (defaults to UTF8).</param>
 /// <returns>The encrypted data.</returns>
 public static byte[] Encrypt(this string data,
                              byte[] key,
                              byte[] salt,
                              HashingAlgorithms hashingAlgorithm,
                              int passwordIterations,
                              byte[] initialVector,
                              int keySize,
                              SymmetricAlgorithms algorithm,
                              Encoding?encoding = null)
 {
     return(data.ToByteArray(encoding)
            .Encrypt(key, salt, hashingAlgorithm, passwordIterations, initialVector, keySize, algorithm));
 }
Beispiel #9
0
    public static void HashEachInput(string[] inputs, bool text, HashFunction hashFunction)
    {
        if (inputs == null)
        {
            DisplayMessage.Error(!text ? "Please specify a file/directory to hash." : "Please specify text to hash.");
            return;
        }
        foreach (string input in inputs)
        {
            try
            {
                switch (text)
                {
                case false when Directory.Exists(input):
                {
                    string[] filePaths  = Directory.GetFiles(input, searchPattern: "*", SearchOption.AllDirectories);
                    int      arrayIndex = Array.IndexOf(inputs, input);
                    if (arrayIndex > 0)
                    {
                        Console.WriteLine();
                    }
                    DisplayMessage.Message(input, "Hashing each file in the directory...");
                    HashEachInput(filePaths, text: false, hashFunction);
                    if (arrayIndex != inputs.Length - 1)
                    {
                        Console.WriteLine();
                    }
                    continue;
                }

                case false when !File.Exists(input):
                    DisplayMessage.NamedError(input, "This file/directory path doesn't exist.");
                    continue;
                }
                using Stream stream = !text ? new FileStream(input, FileMode.Open, FileAccess.Read, FileShare.Read, HashingAlgorithms.BufferSize, FileOptions.SequentialScan) : new MemoryStream(Encoding.UTF8.GetBytes(input));
                byte[] hash = HashingAlgorithms.GetHash(stream, hashFunction);
                DisplayMessage.Message(input, BitConverter.ToString(hash).Replace("-", "").ToLower());
            }
            catch (Exception ex) when(ex is IOException or UnauthorizedAccessException or ArgumentException or SecurityException or NotSupportedException)
            {
                DisplayMessage.NamedError(input, ex.GetType().ToString());
            }
        }
    }
Beispiel #10
0
        /// <summary>
        /// Encrypts the specified data.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <param name="key">The key.</param>
        /// <param name="salt">The salt.</param>
        /// <param name="hashingAlgorithm">The hashing algorithm.</param>
        /// <param name="passwordIterations">The password iterations.</param>
        /// <param name="initialVector">The initial vector. 16 ASCII characters long.</param>
        /// <param name="keySize">
        /// Size of the key. Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
        /// </param>
        /// <returns>The encrypted data.</returns>
        public byte[] Encrypt(
            byte[] data,
            byte[] key,
            byte[] salt,
            HashingAlgorithms hashingAlgorithm,
            int passwordIterations,
            byte[] initialVector,
            int keySize,
            SymmetricAlgorithms algorithm)
        {
            var SymmetricAlgorithm = Symmetrics.FirstOrDefault(x => string.Equals(x.Name, algorithm.ToString(), StringComparison.OrdinalIgnoreCase));

            if (SymmetricAlgorithm == null)
            {
                return(Array.Empty <byte>());
            }
            key = (byte[])key.Clone();
            return(SymmetricAlgorithm.Encrypt(data, key, salt, hashingAlgorithm, passwordIterations, initialVector, keySize));
        }
        private Admin GetAdminAccount(string userEmail, string userPrefFirstName, string userLastName, string userFaculty, string userHomePhone, string userMobile, string userBestContactNumber, string userDob, int userGenderType, int userAccountType, string userPass)
        {
            Admin adminModel = new Admin()
            {
                UserEmail             = userEmail,
                UserPrefFirstName     = userPrefFirstName,
                UserLastName          = userLastName,
                UserFaculty           = userFaculty,
                UserHomePhone         = userHomePhone,
                UserMobile            = userMobile,
                UserBestContactNumber = userBestContactNumber,
                UserDob         = Convert.ToDateTime(userDob),
                UserGenderType  = (UserGenderType)userGenderType,
                UserAccountType = (UserAccountType)userAccountType,
                UserHasLoggedIn = false,
                UserPass        = HashingAlgorithms.ComputeMd5Hash(userPass),
                UserName        = $"{userPrefFirstName} {userLastName}"
            };

            return(adminModel);
        }
Beispiel #12
0
        public string SignInUser(string userEmail, string password)
        {
            var user = _context.UserValues.Where(x => x.UserEmail == userEmail).First <User>();

            if (user != null)
            {
                var userAccountStatus = _userManager.GetUserConfirmationStatus(user);
                if (HashingAlgorithms.VerifyPassword(user.UserPass, password))
                {
                    if (userAccountStatus != null)
                    {
                        if (userAccountStatus.UserAccountConfirmed)
                        {
                            return(_tokenManager.AssignToken(user.UserId));
                        }
                        return("UnconfirmedEmail"); // If the user account has not been confirmed this string will be returned to indicate that an uncofirmed email has attempted to sign in
                    }
                }
                return(null);
            }
            return(null);
        }
 public void Hash(HashingAlgorithms algorithms)
 {
     Assert.NotNull(TestObject2.Hash(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }, algorithms));
 }
 public void Hash(HashingAlgorithms algorithms)
 {
     Assert.NotNull("Test String".Hash(algorithms));
 }