Example #1
0
 public static byte[] Hash(char[] password)
 {
     if (password.Length == 0)
     {
         return(null);
     }
     byte[] passwordBytes = GetPasswordBytes(password);
     return(Blake2.Hash(passwordBytes));
 }
Example #2
0
 private static byte[] GetFileBytes(string filePath, bool preHash)
 {
     if (!preHash)
     {
         return(File.ReadAllBytes(filePath));
     }
     using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.FileStreamBufferSize, FileOptions.SequentialScan);
     return(Blake2.Hash(fileStream));
 }
Example #3
0
 public static byte[] ReadKeyfile(string keyfilePath)
 {
     using var keyfile = new FileStream(keyfilePath, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.FileStreamBufferSize, FileOptions.SequentialScan);
     if (keyfile.Length == Constants.KeyfileLength)
     {
         byte[] keyfileBytes = new byte[Constants.KeyfileLength];
         keyfile.Read(keyfileBytes, offset: 0, keyfileBytes.Length);
         return(keyfileBytes);
     }
     return(Blake2.Hash(keyfile));
 }
Example #4
0
        private static byte[] GetFileBytes(string filePath, bool preHashed)
        {
            int  oneGibibyte = 1024 * Constants.Mebibyte;
            long fileSize    = FileHandling.GetFileLength(filePath);

            if (fileSize >= oneGibibyte || preHashed)
            {
                using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read, Constants.FileStreamBufferSize, FileOptions.SequentialScan);
                return(Blake2.Hash(fileStream));
            }
            return(File.ReadAllBytes(filePath));
        }
Example #5
0
        public static bool Compare(char[] a, char[] b)
        {
            // Constant time comparison
            byte[] aBytes = Encoding.UTF8.GetBytes(a);
            byte[] aHash  = Blake2.Hash(aBytes);
            CryptographicOperations.ZeroMemory(aBytes);
            byte[] bBytes = Encoding.UTF8.GetBytes(b);
            byte[] bHash  = Blake2.Hash(bBytes);
            CryptographicOperations.ZeroMemory(bBytes);
            bool equal = Utilities.Compare(aHash, bHash);

            CryptographicOperations.ZeroMemory(aHash);
            CryptographicOperations.ZeroMemory(bHash);
            return(equal);
        }
Example #6
0
 public static byte[] ReadKeyfile(string keyfilePath)
 {
     using var keyfile = new FileStream(keyfilePath, FileMode.Open, FileAccess.Read, FileShare.Read, Constants.FileStreamBufferSize, FileOptions.SequentialScan);
     return Blake2.Hash(keyfile);
 }