Skip to content

Commit ab4287e

Browse files
authored
Remove custom endianness methods and use Endianness Utility instead in SM3 implementation
Signed-off-by: Xen <lordofxen@deskasoft.com>
1 parent 5136a3a commit ab4287e

File tree

1 file changed

+4
-37
lines changed

1 file changed

+4
-37
lines changed

HashifyNet/Algorithms/SM3/SM3_Implementation.cs

Lines changed: 4 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// *
1+
// *
22
// *****************************************************************************
33
// *
44
// * Copyright (c) 2025 Deskasoft International
@@ -122,7 +122,7 @@ protected override IHashValue FinalizeHashValueInternal(CancellationToken cancel
122122
byte[] hash = new byte[32];
123123
for (int i = 0; i < 8; i++)
124124
{
125-
byte[] wordBytes = ToBigEndian(_v[i]);
125+
byte[] wordBytes = Endianness.GetBytesBigEndian(_v[i]);
126126
Buffer.BlockCopy(wordBytes, 0, hash, i * 4, 4);
127127
}
128128

@@ -141,7 +141,7 @@ private byte[] CreatePaddedBlock(byte[] remainder)
141141

142142
padded[remainderLen] = 0x80;
143143

144-
byte[] lengthBytes = ToBigEndian64(totalBits);
144+
byte[] lengthBytes = Endianness.GetBytesBigEndian((ulong)totalBits);
145145
Buffer.BlockCopy(lengthBytes, 0, padded, padded.Length - 8, 8);
146146

147147
return padded;
@@ -154,7 +154,7 @@ private void ProcessBlock(byte[] block, int offset)
154154

155155
for (int i = 0; i < 16; i++)
156156
{
157-
W[i] = ToBigEndian(block, offset + (i * 4));
157+
W[i] = Endianness.ToUInt32BigEndian(block, offset + (i * 4));
158158
}
159159

160160
for (int j = 16; j < 68; j++)
@@ -208,39 +208,6 @@ private void ProcessBlock(byte[] block, int offset)
208208
private static uint P0(uint x) => x ^ RotL(x, 9) ^ RotL(x, 17);
209209
private static uint P1(uint x) => x ^ RotL(x, 15) ^ RotL(x, 23);
210210
private static uint RotL(uint x, int n) => (x << n) | (x >> (32 - n));
211-
212-
private static uint ToBigEndian(byte[] bytes, int offset)
213-
{
214-
return ((uint)bytes[offset] << 24) |
215-
((uint)bytes[offset + 1] << 16) |
216-
((uint)bytes[offset + 2] << 8) |
217-
bytes[offset + 3];
218-
}
219-
220-
private static byte[] ToBigEndian(uint val)
221-
{
222-
return new byte[] {
223-
(byte)(val >> 24),
224-
(byte)(val >> 16),
225-
(byte)(val >> 8),
226-
(byte)val
227-
};
228-
}
229-
230-
private static byte[] ToBigEndian64(long val)
231-
{
232-
return new byte[] {
233-
(byte)(val >> 56),
234-
(byte)(val >> 48),
235-
(byte)(val >> 40),
236-
(byte)(val >> 32),
237-
(byte)(val >> 24),
238-
(byte)(val >> 16),
239-
(byte)(val >> 8),
240-
(byte)val
241-
};
242-
}
243211
}
244212
}
245213
}
246-

0 commit comments

Comments
 (0)