mirror of
https://github.com/aaru-dps/Aaru.Checksums.git
synced 2025-12-16 19:24:29 +00:00
Code restyling.
This commit is contained in:
@@ -37,114 +37,90 @@ using Aaru.CommonTypes.Interfaces;
|
||||
|
||||
namespace Aaru.Checksums
|
||||
{
|
||||
/// <summary>
|
||||
/// Wraps up .NET SHA384 implementation to a Init(), Update(), Final() context.
|
||||
/// </summary>
|
||||
/// <summary>Wraps up .NET SHA384 implementation to a Init(), Update(), Final() context.</summary>
|
||||
public class Sha384Context : IChecksum
|
||||
{
|
||||
SHA384 sha384Provider;
|
||||
readonly SHA384 sha384Provider;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the SHA384 hash provider
|
||||
/// </summary>
|
||||
public Sha384Context()
|
||||
{
|
||||
sha384Provider = SHA384.Create();
|
||||
}
|
||||
/// <summary>Initializes the SHA384 hash provider</summary>
|
||||
public Sha384Context() => sha384Provider = SHA384.Create();
|
||||
|
||||
/// <summary>
|
||||
/// Updates the hash with data.
|
||||
/// </summary>
|
||||
/// <summary>Updates the hash with data.</summary>
|
||||
/// <param name="data">Data buffer.</param>
|
||||
/// <param name="len">Length of buffer to hash.</param>
|
||||
public void Update(byte[] data, uint len)
|
||||
{
|
||||
sha384Provider.TransformBlock(data, 0, (int)len, data, 0);
|
||||
}
|
||||
public void Update(byte[] data, uint len) => sha384Provider.TransformBlock(data, 0, (int)len, data, 0);
|
||||
|
||||
/// <summary>
|
||||
/// Updates the hash with data.
|
||||
/// </summary>
|
||||
/// <summary>Updates the hash with data.</summary>
|
||||
/// <param name="data">Data buffer.</param>
|
||||
public void Update(byte[] data)
|
||||
{
|
||||
Update(data, (uint)data.Length);
|
||||
}
|
||||
public void Update(byte[] data) => Update(data, (uint)data.Length);
|
||||
|
||||
/// <summary>
|
||||
/// Returns a byte array of the hash value.
|
||||
/// </summary>
|
||||
/// <summary>Returns a byte array of the hash value.</summary>
|
||||
public byte[] Final()
|
||||
{
|
||||
sha384Provider.TransformFinalBlock(new byte[0], 0, 0);
|
||||
|
||||
return sha384Provider.Hash;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a hexadecimal representation of the hash value.
|
||||
/// </summary>
|
||||
/// <summary>Returns a hexadecimal representation of the hash value.</summary>
|
||||
public string End()
|
||||
{
|
||||
sha384Provider.TransformFinalBlock(new byte[0], 0, 0);
|
||||
StringBuilder sha384Output = new StringBuilder();
|
||||
var sha384Output = new StringBuilder();
|
||||
|
||||
foreach(byte h in sha384Provider.Hash) sha384Output.Append(h.ToString("x2"));
|
||||
foreach(byte h in sha384Provider.Hash)
|
||||
sha384Output.Append(h.ToString("x2"));
|
||||
|
||||
return sha384Output.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the hash of a file
|
||||
/// </summary>
|
||||
/// <summary>Gets the hash of a file</summary>
|
||||
/// <param name="filename">File path.</param>
|
||||
public static byte[] File(string filename)
|
||||
{
|
||||
SHA384 localSha384Provider = SHA384.Create();
|
||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||
byte[] result = localSha384Provider.ComputeHash(fileStream);
|
||||
var localSha384Provider = SHA384.Create();
|
||||
var fileStream = new FileStream(filename, FileMode.Open);
|
||||
byte[] result = localSha384Provider.ComputeHash(fileStream);
|
||||
fileStream.Close();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the hash of a file in hexadecimal and as a byte array.
|
||||
/// </summary>
|
||||
/// <summary>Gets the hash of a file in hexadecimal and as a byte array.</summary>
|
||||
/// <param name="filename">File path.</param>
|
||||
/// <param name="hash">Byte array of the hash value.</param>
|
||||
public static string File(string filename, out byte[] hash)
|
||||
{
|
||||
SHA384 localSha384Provider = SHA384.Create();
|
||||
FileStream fileStream = new FileStream(filename, FileMode.Open);
|
||||
var localSha384Provider = SHA384.Create();
|
||||
var fileStream = new FileStream(filename, FileMode.Open);
|
||||
hash = localSha384Provider.ComputeHash(fileStream);
|
||||
StringBuilder sha384Output = new StringBuilder();
|
||||
var sha384Output = new StringBuilder();
|
||||
|
||||
foreach(byte h in hash) sha384Output.Append(h.ToString("x2"));
|
||||
foreach(byte h in hash)
|
||||
sha384Output.Append(h.ToString("x2"));
|
||||
|
||||
fileStream.Close();
|
||||
|
||||
return sha384Output.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the hash of the specified data buffer.
|
||||
/// </summary>
|
||||
/// <summary>Gets the hash of the specified data buffer.</summary>
|
||||
/// <param name="data">Data buffer.</param>
|
||||
/// <param name="len">Length of the data buffer to hash.</param>
|
||||
/// <param name="hash">Byte array of the hash value.</param>
|
||||
public static string Data(byte[] data, uint len, out byte[] hash)
|
||||
{
|
||||
SHA384 localSha384Provider = SHA384.Create();
|
||||
var localSha384Provider = SHA384.Create();
|
||||
hash = localSha384Provider.ComputeHash(data, 0, (int)len);
|
||||
StringBuilder sha384Output = new StringBuilder();
|
||||
var sha384Output = new StringBuilder();
|
||||
|
||||
foreach(byte h in hash) sha384Output.Append(h.ToString("x2"));
|
||||
foreach(byte h in hash)
|
||||
sha384Output.Append(h.ToString("x2"));
|
||||
|
||||
return sha384Output.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the hash of the specified data buffer.
|
||||
/// </summary>
|
||||
/// <summary>Gets the hash of the specified data buffer.</summary>
|
||||
/// <param name="data">Data buffer.</param>
|
||||
/// <param name="hash">Byte array of the hash value.</param>
|
||||
public static string Data(byte[] data, out byte[] hash) => Data(data, (uint)data.Length, out hash);
|
||||
|
||||
Reference in New Issue
Block a user