C# retrieve file CheckSum sha512

using System.Security.Cryptography;
using System.Text.Unicode;

namespace ConsoleApp4
{
    internal class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"C:\Users\fred\Downloads\dotnet-sdk-9.0.100-win-x64.exe";
            string sha512Str= RetrieveFileSHA512(filePath);
            Console.WriteLine($"File:{filePath}\nSHA512:{sha512Str}");
        }

        static string RetrieveFileSHA512(string filePath)
        {
            string sha512Str = string.Empty;
            if (!File.Exists(filePath))
            {
                return null;
            }
            using(FileStream fs=new FileStream(filePath,FileMode.Open,FileAccess.Read))
            {
                using(SHA512 sha512=SHA512.Create())
                {
                    var shaBytes = sha512.ComputeHash(fs);
                    if(shaBytes!=null && shaBytes.Any())
                    {
                        sha512Str = BitConverter.ToString(shaBytes).Replace("-", "").ToLowerInvariant();                        
                    }
                }
            }
            return sha512Str;
        }
    }
}

 

 

 

 

 

 

 

 

posted @ 2024-11-16 19:07  FredGrit  阅读(2)  评论(0编辑  收藏  举报