77 lines
2.6 KiB
C#
77 lines
2.6 KiB
C#
using MfGames.Cryptography;
|
|
using MfGames.Cryptography.Extensions;
|
|
using MfGames.Cryptography.Hashes;
|
|
|
|
namespace MfGames.Nitride.Contents;
|
|
|
|
public static class CryptographyContentExtensions
|
|
{
|
|
/// <summary>
|
|
/// Hashes the given input and returns the results.
|
|
/// </summary>
|
|
/// <param name="content">The binary content or null.</param>
|
|
/// <param name="hash">The type of hash requested.</param>
|
|
/// <returns>Null if input is null, otherwise the hash value.</returns>
|
|
public static byte[]? ToHashBytes(this IBinaryContent? content, HashType hash = HashType.Sha512)
|
|
{
|
|
if (content == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using Stream stream = content.GetStream();
|
|
|
|
return hash.CreateHash().ComputeHash(stream);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hashes the given input and returns the results.
|
|
/// </summary>
|
|
/// <param name="content">The text content or null.</param>
|
|
/// <param name="hash">The type of hash requested.</param>
|
|
/// <returns>Null if input is null, otherwise the hash value.</returns>
|
|
public static byte[]? ToHashBytes(this ITextContent? content, HashType hash = HashType.Sha512)
|
|
{
|
|
if (content == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
string text = content.GetTextContentString();
|
|
|
|
return text.ToHashBytes(hash);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hashes the given input and returns the results as a string.
|
|
/// </summary>
|
|
/// <param name="content">The binary content or null.</param>
|
|
/// <param name="hash">The type of hash requested.</param>
|
|
/// <param name="format">The format of the requested string.</param>
|
|
/// <returns>Null if input is null, otherwise the hash value.</returns>
|
|
public static string? ToHashString(
|
|
this IBinaryContent? content,
|
|
HashType hash = HashType.Sha512,
|
|
ByteStringFormat format = ByteStringFormat.LowercaseHex
|
|
)
|
|
{
|
|
return content?.ToHashBytes(hash).ToByteString(format);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hashes the given input and returns the results as a string.
|
|
/// </summary>
|
|
/// <param name="content">The text content or null.</param>
|
|
/// <param name="hash">The type of hash requested.</param>
|
|
/// <param name="format">The format of the requested string.</param>
|
|
/// <returns>Null if input is null, otherwise the hash value.</returns>
|
|
public static string? ToHashString(
|
|
this ITextContent? content,
|
|
HashType hash = HashType.Sha512,
|
|
ByteStringFormat format = ByteStringFormat.LowercaseHex
|
|
)
|
|
{
|
|
return content?.ToHashBytes(hash).ToByteString(format);
|
|
}
|
|
}
|