Skip to content

Commit 08ee093

Browse files
authored
Categorize Core and Add Revived HashValue Structure
Signed-off-by: Xen <lordofxen@deskasoft.com>
1 parent 718bfc1 commit 08ee093

13 files changed

+1547
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// *
2+
// *****************************************************************************
3+
// *
4+
// * Copyright (c) 2025 Deskasoft International
5+
// *
6+
// * Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// * of this software and associated documentation files (the ""Software""), to deal
8+
// * in the Software without restriction, including without limitation the rights
9+
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// * copies of the Software, and to permit persons to whom the Software is
11+
// * furnished to do so, subject to the following conditions:
12+
// *
13+
// * The above copyright notice and this permission notice shall be included in all
14+
// * copies or substantial portions of the Software.
15+
// *
16+
// * THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// * SOFTWARE.
23+
// *
24+
// *
25+
// * Please refer to LICENSE file.
26+
// *
27+
// ******************************************************************************
28+
// *
29+
30+
using HashifyNet.Core.Utilities;
31+
using System;
32+
33+
namespace HashifyNet.Core
34+
{
35+
/// <summary>
36+
/// Specifies that a class implements a hash algorithm.
37+
/// </summary>
38+
/// <remarks>This attribute is used to annotate classes that provide an implementation of a hash algorithm. It
39+
/// is intended for internal use and is not inherited by derived classes.</remarks>
40+
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)]
41+
internal class HashAlgorithmImplementationAttribute : Attribute
42+
{
43+
/// <summary>
44+
/// Gets the interface type that the current class implements.
45+
/// </summary>
46+
/// <remarks>This property is useful for scenarios where reflection or type analysis is required to determine
47+
/// the specific interface implemented by the class.</remarks>
48+
public Type ImplementedInterface { get; }
49+
50+
/// <summary>
51+
/// Gets the concrete configuration type associated with the current instance.
52+
/// </summary>
53+
public Type ConcreteConfig { get; }
54+
55+
/// <summary>
56+
/// Initializes a new instance of the <see cref="HashAlgorithmImplementationAttribute"/> class, specifying the
57+
/// interface implemented by the hash algorithm and the concrete configuration type.
58+
/// </summary>
59+
/// <param name="implementedInterface">The interface type that the hash algorithm implementation adheres to. This parameter must represent an interface
60+
/// type.</param>
61+
/// <param name="concreteConfig">The concrete class type that provides the configuration for the hash algorithm implementation. This parameter
62+
/// must represent a class type.</param>
63+
/// <exception cref="ArgumentNullException">Thrown if <paramref name="implementedInterface"/> or <paramref name="concreteConfig"/> is <see langword="null"/>.</exception>
64+
/// <exception cref="ArgumentException">Thrown if <paramref name="implementedInterface"/> is not an interface type, or if <paramref
65+
/// name="concreteConfig"/> is not a class type.</exception>
66+
public HashAlgorithmImplementationAttribute(Type implementedInterface, Type concreteConfig)
67+
{
68+
if (implementedInterface == null)
69+
{
70+
throw new ArgumentNullException(nameof(implementedInterface), $"{nameof(implementedInterface)} must not be null.");
71+
}
72+
73+
string memberName = implementedInterface.FullName;
74+
75+
if (!implementedInterface.IsInterface)
76+
{
77+
throw new ArgumentException($"{memberName}: The provided type must be an interface.", nameof(implementedInterface));
78+
}
79+
80+
if (concreteConfig == null)
81+
{
82+
throw new ArgumentNullException(nameof(concreteConfig), $"{memberName}: {nameof(concreteConfig)} must not be null.");
83+
}
84+
85+
if (!concreteConfig.IsClass)
86+
{
87+
throw new ArgumentException($"{memberName}: The provided type for concrete config must be a class.", nameof(concreteConfig));
88+
}
89+
90+
if (!implementedInterface.HasInterface(typeof(IHashFunction<>)))
91+
{
92+
throw new ArgumentException($"{memberName}: The provided interface type must implement IHashFunction<>.", nameof(implementedInterface));
93+
}
94+
95+
if (!concreteConfig.HasInterface(typeof(IHashConfig<>)))
96+
{
97+
throw new ArgumentException($"{memberName}: The provided concrete config type must implement IHashConfig<>.", nameof(concreteConfig));
98+
}
99+
100+
ImplementedInterface = implementedInterface;
101+
ConcreteConfig = concreteConfig;
102+
}
103+
}
104+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// *
2+
// *****************************************************************************
3+
// *
4+
// * Copyright (c) 2025 Deskasoft International
5+
// *
6+
// * Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// * of this software and associated documentation files (the ""Software""), to deal
8+
// * in the Software without restriction, including without limitation the rights
9+
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// * copies of the Software, and to permit persons to whom the Software is
11+
// * furnished to do so, subject to the following conditions:
12+
// *
13+
// * The above copyright notice and this permission notice shall be included in all
14+
// * copies or substantial portions of the Software.
15+
// *
16+
// * THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// * SOFTWARE.
23+
// *
24+
// *
25+
// * Please refer to LICENSE file.
26+
// *
27+
// ******************************************************************************
28+
// *
29+
30+
namespace HashifyNet
31+
{
32+
/// <summary>
33+
/// Defines the configuration for a hash algorithm, including the hash size and the ability to create a copy of the
34+
/// configuration.
35+
/// </summary>
36+
/// <typeparam name="CName">The type of the hash configuration, which must implement <see cref="IHashConfig{CName}"/>.</typeparam>
37+
public interface IHashConfig<CName> : IHashConfigBase where CName : IHashConfig<CName>
38+
{
39+
/// <summary>
40+
/// Creates a new instance of the <typeparamref name="CName"/> object that is a copy of the current instance.
41+
/// </summary>
42+
/// <returns>A new <typeparamref name="CName"/> object that is identical to the current instance.</returns>
43+
CName Clone();
44+
}
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// *
2+
// *****************************************************************************
3+
// *
4+
// * Copyright (c) 2025 Deskasoft International
5+
// *
6+
// * Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// * of this software and associated documentation files (the ""Software""), to deal
8+
// * in the Software without restriction, including without limitation the rights
9+
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// * copies of the Software, and to permit persons to whom the Software is
11+
// * furnished to do so, subject to the following conditions:
12+
// *
13+
// * The above copyright notice and this permission notice shall be included in all
14+
// * copies or substantial portions of the Software.
15+
// *
16+
// * THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// * SOFTWARE.
23+
// *
24+
// *
25+
// * Please refer to LICENSE file.
26+
// *
27+
// ******************************************************************************
28+
// *
29+
30+
namespace HashifyNet
31+
{
32+
/// <summary>
33+
/// Defines the base configuration for a hashing algorithm, including properties that specify the characteristics of
34+
/// the computed hash.
35+
/// </summary>
36+
/// <remarks>This interface is intended to be implemented by classes that configure hashing algorithms. It
37+
/// provides a common property to describe the size of the hash value produced by the algorithm.</remarks>
38+
public interface IHashConfigBase
39+
{
40+
/// <summary>
41+
/// Gets the size, in bits, of the hash value computed by the algorithm.
42+
/// </summary>
43+
int HashSizeInBits { get; }
44+
}
45+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// *
2+
// *****************************************************************************
3+
// *
4+
// * Copyright (c) 2025 Deskasoft International
5+
// *
6+
// * Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// * of this software and associated documentation files (the "Software"), to deal
8+
// * in the Software without restriction, including without limitation the rights
9+
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// * copies of the Software, and to permit persons to whom the Software is
11+
// * furnished to do so, subject to the following conditions:
12+
// *
13+
// * The above copyright notice and this permission notice shall be included in all
14+
// * copies or substantial portions of the Software.
15+
// *
16+
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// * SOFTWARE.
23+
// *
24+
// *
25+
// * Please refer to LICENSE file.
26+
// *
27+
// ******************************************************************************
28+
// *
29+
30+
namespace HashifyNet.Core
31+
{
32+
/// <summary>
33+
/// Abstract implementation of an <see cref="IHashFunction{FName}"/>.
34+
/// Provides convenience checks and ensures a default HashSize has been set at construction.
35+
/// </summary>
36+
public abstract class CryptographicHashFunctionBase<CName>
37+
: HashFunctionBase<CName>, ICryptographicHashFunction<CName> where CName : ICryptographicHashConfig<CName>
38+
{
39+
private bool disposedValue;
40+
41+
private void Dispose(bool disposing)
42+
{
43+
if (!disposedValue)
44+
{
45+
if (disposing)
46+
{
47+
Config?.Dispose();
48+
}
49+
50+
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
51+
// TODO: set large fields to null
52+
disposedValue = true;
53+
}
54+
}
55+
56+
/// <summary>
57+
/// <inheritdoc/>
58+
/// </summary>
59+
public void Dispose()
60+
{
61+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
62+
Dispose(disposing: true);
63+
System.GC.SuppressFinalize(this);
64+
}
65+
}
66+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// *
2+
// *****************************************************************************
3+
// *
4+
// * Copyright (c) 2025 Deskasoft International
5+
// *
6+
// * Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// * of this software and associated documentation files (the "Software"), to deal
8+
// * in the Software without restriction, including without limitation the rights
9+
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// * copies of the Software, and to permit persons to whom the Software is
11+
// * furnished to do so, subject to the following conditions:
12+
// *
13+
// * The above copyright notice and this permission notice shall be included in all
14+
// * copies or substantial portions of the Software.
15+
// *
16+
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// * SOFTWARE.
23+
// *
24+
// *
25+
// * Please refer to LICENSE file.
26+
// *
27+
// ******************************************************************************
28+
// *
29+
30+
namespace HashifyNet.Core
31+
{
32+
/// <summary>
33+
/// Common interface to non-cryptographic hash functions that can be computed over a stream of data without buffering.
34+
/// </summary>
35+
public abstract class CryptographicStreamableHashFunctionBase<CName>
36+
: StreamableHashFunctionBase<CName>,
37+
IStreamableHashFunction<CName>, ICryptographicStreamableHashFunction<CName> where CName : ICryptographicHashConfig<CName>
38+
{
39+
private bool disposedValue;
40+
41+
private void Dispose(bool disposing)
42+
{
43+
if (!disposedValue)
44+
{
45+
if (disposing)
46+
{
47+
Config?.Dispose();
48+
}
49+
50+
// TODO: free unmanaged resources (unmanaged objects) and override finalizer
51+
// TODO: set large fields to null
52+
disposedValue = true;
53+
}
54+
}
55+
56+
/// <summary>
57+
/// <inheritdoc/>
58+
/// </summary>
59+
public void Dispose()
60+
{
61+
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
62+
Dispose(disposing: true);
63+
System.GC.SuppressFinalize(this);
64+
}
65+
}
66+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// *
2+
// *****************************************************************************
3+
// *
4+
// * Copyright (c) 2025 Deskasoft International
5+
// *
6+
// * Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// * of this software and associated documentation files (the ""Software""), to deal
8+
// * in the Software without restriction, including without limitation the rights
9+
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// * copies of the Software, and to permit persons to whom the Software is
11+
// * furnished to do so, subject to the following conditions:
12+
// *
13+
// * The above copyright notice and this permission notice shall be included in all
14+
// * copies or substantial portions of the Software.
15+
// *
16+
// * THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// * SOFTWARE.
23+
// *
24+
// *
25+
// * Please refer to LICENSE file.
26+
// *
27+
// ******************************************************************************
28+
// *
29+
30+
using System;
31+
32+
namespace HashifyNet
33+
{
34+
/// <summary>
35+
/// Defines the configuration interface for cryptographic hash algorithms, supporting customization and resource
36+
/// management.
37+
/// </summary>
38+
/// <remarks>This interface extends <see cref="IHashConfig{CName}"/> to provide additional functionality for
39+
/// cryptographic hash configurations, including support for resource cleanup through <see
40+
/// cref="IDisposable"/>.</remarks>
41+
/// <typeparam name="CName">The type representing the specific hash configuration. This type must implement <see cref="IHashConfig{CName}"/>.</typeparam>
42+
public interface ICryptographicHashConfig<CName> : IHashConfig<CName>, IDisposable where CName : IHashConfig<CName>
43+
{
44+
}
45+
}

0 commit comments

Comments
 (0)