SHA256
GetSha256Hash
// Generate the SHA256 hash
string hash = SecurityUtils.GetSha256Hash("Hello there!");
// Outputs "89b8b8e486421463d7e0f5caf60fb9cb35ce169b76e657ab21fc4d1d6b093603"
Console.WriteLine(hash);
With encoding:
// Generate the SHA256 hash
string hash = SecurityUtils.GetSha256Hash("Hello there!", Encoding.UTF8);
// Outputs "89b8b8e486421463d7e0f5caf60fb9cb35ce169b76e657ab21fc4d1d6b093603"
Console.WriteLine(hash);
With HEX format:
// Generate the SHA256 hash
string hash = SecurityUtils.GetSha256Hash("Hello there!", HexFormat.UpperCase));
// Outputs "89B8B8E486421463D7E0F5CAF60FB9CB35CE169B76E657AB21FC4D1D6B093603"
Console.WriteLine(hash);
With HEX format and encoding:
// Generate the SHA256 hash
string hash = SecurityUtils.GetSha256Hash("Hello there!", HexFormat.UpperCase, Encoding.UTF8));
// Outputs "89B8B8E486421463D7E0F5CAF60FB9CB35CE169B76E657AB21FC4D1D6B093603"
Console.WriteLine(hash);
GetSha256HashFromFile
// Generate the SHA256 hash
Guid hash = SecurityUtils.GetSha256HashFromFile("c:/obi-wan.txt");
// Outputs "89b8b8e486421463d7e0f5caf60fb9cb35ce169b76e657ab21fc4d1d6b093603"
Console.WriteLine(hash);
The GetSha256HashFromFile
method currently doesn't allow specifying the desired HEX format. As the file is read from disk, a method overload taking an encoding also isn't available.