Skip to content

Commit e745d26

Browse files
committed
Changed checksum to a structure as output.
1 parent 3d974e3 commit e745d26

4 files changed

Lines changed: 50 additions & 16 deletions

File tree

OutSystems.NetChecksumUtils.UnitTests/NetChecksumUtils.ComputeChecksum.Tests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ public void ComputeChecksum_ValidInput_ReturnsCorrectHash(string algorithm, stri
6666
string expected = GetExpectedHash(algorithm, text);
6767

6868
// Act
69-
_sut.ComputeChecksum(algorithm, text, out string actualHex, out string actualBase64, out long ticks);
69+
_sut.ComputeChecksum(algorithm, text, out Checksum checksum, out long ticks);
7070

7171
// Assert
7272
Assert.Multiple(() =>
7373
{
74-
Assert.That(actualHex, Is.EqualTo(expected), $"Hex hash mismatch for {algorithm}");
75-
Assert.That(actualBase64, Is.EqualTo(GetExpectedBase64(algorithm, text)), $"Base64 hash mismatch for {algorithm}");
74+
Assert.That(checksum.Hex, Is.EqualTo(expected), $"Hex hash mismatch for {algorithm}");
75+
Assert.That(checksum.Base64, Is.EqualTo(GetExpectedBase64(algorithm, text)), $"Base64 hash mismatch for {algorithm}");
7676
Assert.That(ticks, Is.GreaterThanOrEqualTo(0), "Duration should be non-negative.");
7777
});
7878
}
@@ -134,8 +134,8 @@ public void Methods_NullInputs_ThrowArgumentNullException()
134134
Assert.Multiple(() =>
135135
{
136136
// ComputeChecksum null checks
137-
Assert.Throws<ArgumentNullException>(() => _sut.ComputeChecksum(null!, "text", out _, out _, out _));
138-
Assert.Throws<ArgumentNullException>(() => _sut.ComputeChecksum("SHA256", null!, out _, out _, out _));
137+
Assert.Throws<ArgumentNullException>(() => _sut.ComputeChecksum(null!, "text", out _, out _));
138+
Assert.Throws<ArgumentNullException>(() => _sut.ComputeChecksum("SHA256", null!, out _, out _));
139139

140140
// VerifyChecksum null checks
141141
Assert.Throws<ArgumentNullException>(() => _sut.VerifyChecksum(null!, "text", "hash", out _, out _));
@@ -153,7 +153,7 @@ public void Methods_NullInputs_ThrowArgumentNullException()
153153
public void Methods_InvalidAlgorithm_ThrowsArgumentException(string invalidAlgo)
154154
{
155155
Assert.Throws<ArgumentException>(() =>
156-
_sut.ComputeChecksum(invalidAlgo, "text", out _, out _, out _));
156+
_sut.ComputeChecksum(invalidAlgo, "text", out _, out _));
157157
}
158158

159159
#endregion
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using OutSystems.ExternalLibraries.SDK;
3+
4+
namespace OutSystems.NetChecksumUtils
5+
{
6+
/// <summary>
7+
/// The checksum result structure containing both Hex and Base64 representations.
8+
/// </summary>
9+
[OSStructure(
10+
Description = "Represents a computed checksum in both hexadecimal and Base64 encodings.",
11+
OriginalName = "Checksum"
12+
)]
13+
public struct Checksum
14+
{
15+
/// <summary>
16+
/// Gets the hexadecimal representation of the checksum.
17+
/// </summary>
18+
[OSStructureField(
19+
Description = "Hexadecimal representation of the checksum.",
20+
OriginalName = "Hex"
21+
)]
22+
public string Hex;
23+
24+
/// <summary>
25+
/// Gets the Base64 representation of the checksum.
26+
/// </summary>
27+
[OSStructureField(
28+
Description = "Base64 representation of the checksum.",
29+
OriginalName = "Base64"
30+
)]
31+
public string Base64;
32+
}
33+
}

OutSystems.NetChecksumUtils/INetChecksumUtils.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ void ComputeChecksum(
2020
string algorithm,
2121
[OSParameterAttribute(Description = "The text to compute the checksum for. The text is encoded as UTF-8 before hashing.")]
2222
string textToHash,
23-
[OSParameterAttribute(Description = "Output parameter that receives the computed checksum as an uppercase hexadecimal string.")]
24-
out string checksumText,
25-
[OSParameterAttribute(Description = "Output parameter that receives the computed checksum as a Base64 string.")]
26-
out string checksumBase64,
23+
[OSParameterAttribute(Description = "Output parameter that receives the computed checksum (Hex + Base64).")]
24+
out Checksum checksum,
2725
[OSParameterAttribute(Description = "Output parameter that receives the duration of the hashing operation in ticks.")]
2826
out long operationDuration
2927
);

OutSystems.NetChecksumUtils/NetChecksumUtils.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace OutSystems.NetChecksumUtils
1212
/// </summary>
1313
public class NetChecksumUtils : INetChecksumUtils
1414
{
15-
static (byte[] HashBytes, TimeSpan Elapsed) ComputeChecksum(Func<byte[], byte[]> hashFunc, byte[] bytes)
15+
static (byte[] HashBytes, TimeSpan Elapsed) ComputeHashBytes(Func<byte[], byte[]> hashFunc, byte[] bytes)
1616
{
1717
var sw = Stopwatch.StartNew();
1818
byte[] hashBytes = hashFunc(bytes);
@@ -59,7 +59,7 @@ private static Func<byte[], byte[]> GetHashFunction(string algorithm)
5959
/// <exception cref="ArgumentException">Thrown when <paramref name="algorithm"/> is not recognized/supported.</exception>
6060
public void ComputeChecksum(
6161
string algorithm, string textToHash,
62-
out string checksumTextHex, out string checksumTextBase64, out long operationDurationInTicks)
62+
out Checksum checksum, out long operationDurationInTicks)
6363
{
6464
// Ensure inputs are not null before proceeding
6565
ArgumentNullException.ThrowIfNull(algorithm);
@@ -72,11 +72,14 @@ public void ComputeChecksum(
7272
var hashFunc = GetHashFunction(algorithm);
7373

7474
// 3. Execute the hashing logic and measure performance.
75-
var (hashBytes, elapsed) = ComputeChecksum(hashFunc, inputBytes);
75+
var (hashBytes, elapsed) = ComputeHashBytes(hashFunc, inputBytes);
7676

7777
// 4. Assign results to output parameters (hex + base64)
78-
checksumTextHex = Convert.ToHexString(hashBytes);
79-
checksumTextBase64 = Convert.ToBase64String(hashBytes);
78+
checksum = new Checksum
79+
{
80+
Hex = Convert.ToHexString(hashBytes),
81+
Base64 = Convert.ToBase64String(hashBytes)
82+
};
8083
operationDurationInTicks = elapsed.Ticks;
8184
}
8285

@@ -110,7 +113,7 @@ public void VerifyChecksum(
110113

111114
// 3. Measure the whole operation: hashing + comparison
112115
var sw = Stopwatch.StartNew();
113-
var (hashBytes, _) = ComputeChecksum(hashFunc, inputBytes);
116+
var (hashBytes, _) = ComputeHashBytes(hashFunc, inputBytes);
114117
var newHex = Convert.ToHexString(hashBytes);
115118
var newBase64 = Convert.ToBase64String(hashBytes);
116119
isValid = string.Equals(newHex, existingChecksum, StringComparison.OrdinalIgnoreCase)

0 commit comments

Comments
 (0)