diff --git a/src/LibHac/FsSystem/IHash256Generator.cs b/src/LibHac/FsSystem/IHash256Generator.cs new file mode 100644 index 00000000..3f2e26dc --- /dev/null +++ b/src/LibHac/FsSystem/IHash256Generator.cs @@ -0,0 +1,79 @@ +// ReSharper disable InconsistentNaming +using System; +using LibHac.Common; +using LibHac.Diag; + +namespace LibHac.FsSystem; + +/// +/// Generates a hash for a stream of data. The data can be given to the +/// as multiple, smaller sequential blocks of data. +/// +/// Based on FS 13.1.0 (nnSdk 13.4.0) +public abstract class IHash256Generator : IDisposable +{ + public static readonly long HashSize = 256 / 8; + + public virtual void Dispose() { } + + public void Initialize() + { + DoInitialize(); + } + + public void Update(ReadOnlySpan data) + { + DoUpdate(data); + } + + public void GetHash(Span hashBuffer) + { + Assert.SdkRequiresEqual(HashSize, hashBuffer.Length); + + DoGetHash(hashBuffer); + } + + protected abstract void DoInitialize(); + protected abstract void DoUpdate(ReadOnlySpan data); + protected abstract void DoGetHash(Span hashBuffer); +} + +/// +/// Creates objects and can generate a hash for a single, in-memory block of data. +/// +/// Based on FS 13.1.0 (nnSdk 13.4.0) +public abstract class IHash256GeneratorFactory : IDisposable +{ + public virtual void Dispose() { } + + public UniqueRef Create() + { + return DoCreate(); + } + + public void GenerateHash(Span hashBuffer, ReadOnlySpan data) + { + Assert.SdkRequiresEqual(IHash256Generator.HashSize, hashBuffer.Length); + + DoGenerateHash(hashBuffer, data); + } + + protected abstract UniqueRef DoCreate(); + protected abstract void DoGenerateHash(Span hashBuffer, ReadOnlySpan data); +} + +/// +/// Creates objects. +/// +/// Based on FS 13.1.0 (nnSdk 13.4.0) +public abstract class IHash256GeneratorFactorySelector : IDisposable +{ + public virtual void Dispose() { } + + public IHash256GeneratorFactory GetFactory() + { + return DoGetFactory(); + } + + protected abstract IHash256GeneratorFactory DoGetFactory(); +} \ No newline at end of file