Repository: PowerShell/PowerShell
File: src/System.Management.Automation/security/CatalogHelper.cs (C#)

Code before:
            string hashValue = string.Empty;

            // To get handle to the hash algorithm to be used to calculate hashes
            SafeCATAdminHandle catAdmin;
            try
            {
                catAdmin = WinTrustMethods.CryptCATAdminAcquireContext2(hashAlgorithm);
            }
            catch (Win32Exception e)
            {
                ErrorRecord errorRecord = new ErrorRecord(
                    new InvalidOperationException(StringUtil.Format(CatalogStrings.UnableToAcquireHashAlgorithmContext, hashAlgorithm), e),
                    "UnableToAcquireHashAlgorithmContext",
                    ErrorCategory.InvalidOperation,
                    null);
                _cmdlet.ThrowTerminatingError(errorRecord);

                // The method returns an empty string on a failure.
                return hashValue;
            }

            // Open the file that is to be hashed for reading and get its handle
            FileStream fileStream;
            try
            {
                fileStream = File.OpenRead(filePath);
            }
            catch (Exception e)
            {
                // If we are not able to open file that is to be hashed we can not continue with catalog validation
                ErrorRecord errorRecord = new ErrorRecord(
                    new InvalidOperationException(StringUtil.Format(CatalogStrings.UnableToReadFileToHash, filePath), e),
                    "UnableToReadFileToHash",
                    ErrorCategory.InvalidOperation,
                    null);
                _cmdlet.ThrowTerminatingError(errorRecord);

                // The method returns an empty string on a failure.
                return hashValue;
            }

            using (catAdmin)
            using (fileStream)
            {
                byte[] hashBytes = Array.Empty<byte>();
                try
                {
                    hashBytes = WinTrustMethods.CryptCATAdminCalcHashFromFileHandle2(catAdmin, fileStream.SafeFileHandle);
                }
                catch (Win32Exception e)
                {

Review comment (suggestion):
Taking into account a nature of the issue we fix I'd prefer explicit parameters.
```suggestion
                fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
```

Code after:
            string hashValue = string.Empty;

            // To get handle to the hash algorithm to be used to calculate hashes
            SafeCATAdminHandle catAdmin;
            try
            {
                catAdmin = WinTrustMethods.CryptCATAdminAcquireContext2(hashAlgorithm);
            }
            catch (Win32Exception e)
            {
                ErrorRecord errorRecord = new ErrorRecord(
                    new InvalidOperationException(StringUtil.Format(CatalogStrings.UnableToAcquireHashAlgorithmContext, hashAlgorithm), e),
                    "UnableToAcquireHashAlgorithmContext",
                    ErrorCategory.InvalidOperation,
                    null);
                _cmdlet.ThrowTerminatingError(errorRecord);

                // The method returns an empty string on a failure.
                return hashValue;
            }

            // Open the file that is to be hashed for reading and get its handle
            FileStream fileStream;
            try
            {
                fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            catch (Exception e)
            {
                // If we are not able to open file that is to be hashed we can not continue with catalog validation
                ErrorRecord errorRecord = new ErrorRecord(
                    new InvalidOperationException(StringUtil.Format(CatalogStrings.UnableToReadFileToHash, filePath), e),
                    "UnableToReadFileToHash",
                    ErrorCategory.InvalidOperation,
                    null);
                _cmdlet.ThrowTerminatingError(errorRecord);

                // The method returns an empty string on a failure.
                return hashValue;
            }

            using (catAdmin)
            using (fileStream)
            {
                byte[] hashBytes = Array.Empty<byte>();
                try
                {
                    hashBytes = WinTrustMethods.CryptCATAdminCalcHashFromFileHandle2(catAdmin, fileStream.SafeFileHandle);
                }
                catch (Win32Exception e)
                {