Microsoft.CodeAnalysis.Sarif.HashUtilities.ComputeHashes C# (CSharp) Method

ComputeHashes() public static method

public static ComputeHashes ( string fileName ) : HashData
fileName string
return HashData
        public static HashData ComputeHashes(string fileName)
        {
            try
            {
                using (FileStream stream = File.OpenRead(fileName))
                {
                    using (var bufferedStream = new BufferedStream(stream, 1024 * 32))
                    {
                        string md5, sha1, sha256;
                        byte[] checksum;

                        using (var md5Cng = new MD5Cng())
                        {
                            checksum = md5Cng.ComputeHash(bufferedStream);
                            md5 = BitConverter.ToString(checksum).Replace("-", string.Empty);
                        }

                        stream.Seek(0, SeekOrigin.Begin);
                        bufferedStream.Seek(0, SeekOrigin.Begin);

                        using (var sha1Cng = new SHA1Cng())
                        {
                            checksum = sha1Cng.ComputeHash(bufferedStream);
                            sha1 = BitConverter.ToString(checksum).Replace("-", string.Empty);
                        }

                        stream.Seek(0, SeekOrigin.Begin);
                        bufferedStream.Seek(0, SeekOrigin.Begin);

                        using (var sha256Cng = new SHA256Cng())
                        {
                            checksum = sha256Cng.ComputeHash(bufferedStream);
                            sha256 = BitConverter.ToString(checksum).Replace("-", string.Empty);
                        }

                        return new HashData(md5, sha1, sha256);
                    }
                }
            }
            catch (IOException) { }
            catch (UnauthorizedAccessException) { }
            return null;
        }

Usage Example

Esempio n. 1
0
        public static IDictionary <string, HashData> MultithreadedComputeTargetFileHashes(IEnumerable <string> analysisTargets)
        {
            if (analysisTargets == null)
            {
                return(null);
            }

            Console.WriteLine("Computing file hashes...");
            var fileToHashDataMap = new ConcurrentDictionary <string, HashData>();

            var queue = new ConcurrentQueue <string>(analysisTargets);

            var tasks = new List <Task>();

            while (queue.Count > 0 && tasks.Count < Environment.ProcessorCount)
            {
                tasks.Add(Task.Factory.StartNew(() =>
                {
                    while (queue.Count > 0)
                    {
                        if (queue.TryDequeue(out string filePath))
                        {
                            fileToHashDataMap[filePath] = HashUtilities.ComputeHashes(filePath);
                        }
                    }
                }));
            }
            Task.WaitAll(tasks.ToArray());
            Console.WriteLine("Hash computation complete.");
            return(fileToHashDataMap);
        }
All Usage Examples Of Microsoft.CodeAnalysis.Sarif.HashUtilities::ComputeHashes