Skip to content

Commit 33febe6

Browse files
committed
[优化] 优化代码的警告
1 parent 296aab9 commit 33febe6

7 files changed

Lines changed: 52 additions & 29 deletions

File tree

GameFrameX.Foundation.Encryption/Sm/SM4.cs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal sealed class Sm4
2828
/// <returns>转换后的长整型值</returns>
2929
private long GET_ULONG_BE(byte[] b, int i)
3030
{
31-
long n = 0;
31+
long n;
3232
if (ForJavascript)
3333
{
3434
n = (b[i] & 0xff) << 24 | ((b[i + 1] & 0xff) << 16) | ((b[i + 2] & 0xff) << 8) | (b[i + 3] & 0xff) & 0xff;
@@ -90,7 +90,7 @@ private static void Swap(long[] sk, int i)
9090
/// <summary>
9191
/// SM4算法使用的S盒
9292
/// </summary>
93-
private byte[] SboxTable = new byte[]
93+
private readonly byte[] _sboxTable = new byte[]
9494
{
9595
0xd6, 0x90, 0xe9, 0xfe, 0xcc, 0xe1, 0x3d, 0xb7,
9696
0x16, 0xb6, 0x14, 0xc2, 0x28, 0xfb, 0x2c, 0x05,
@@ -129,7 +129,7 @@ private static void Swap(long[] sk, int i)
129129
/// <summary>
130130
/// SM4算法系统参数FK
131131
/// </summary>
132-
public uint[] FK =
132+
public readonly uint[] Fk =
133133
{
134134
0xa3b1bac6,
135135
0x56aa3350,
@@ -140,7 +140,7 @@ private static void Swap(long[] sk, int i)
140140
/// <summary>
141141
/// SM4算法固定参数CK
142142
/// </summary>
143-
public uint[] CK =
143+
public readonly uint[] Ck =
144144
{
145145
0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269,
146146
0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9,
@@ -160,7 +160,7 @@ private static void Swap(long[] sk, int i)
160160
private byte Sm4Sbox(byte inch)
161161
{
162162
int i = inch & 0xFF;
163-
byte retVal = SboxTable[i];
163+
byte retVal = _sboxTable[i];
164164
return retVal;
165165
}
166166

@@ -219,25 +219,25 @@ private long Sm4CalciRk(long ka)
219219
/// <summary>
220220
/// 密钥扩展
221221
/// </summary>
222-
/// <param name="SK">轮密钥数组</param>
222+
/// <param name="sk">轮密钥数组</param>
223223
/// <param name="key">初始密钥</param>
224-
private void Sm4_Setkey(long[] SK, byte[] key)
224+
private void Sm4_Setkey(long[] sk, byte[] key)
225225
{
226-
long[] MK = new long[4];
226+
long[] mk = new long[4];
227227
long[] k = new long[36];
228228
int i = 0;
229-
MK[0] = GET_ULONG_BE(key, 0);
230-
MK[1] = GET_ULONG_BE(key, 4);
231-
MK[2] = GET_ULONG_BE(key, 8);
232-
MK[3] = GET_ULONG_BE(key, 12);
233-
k[0] = MK[0] ^ (long)FK[0];
234-
k[1] = MK[1] ^ (long)FK[1];
235-
k[2] = MK[2] ^ (long)FK[2];
236-
k[3] = MK[3] ^ (long)FK[3];
229+
mk[0] = GET_ULONG_BE(key, 0);
230+
mk[1] = GET_ULONG_BE(key, 4);
231+
mk[2] = GET_ULONG_BE(key, 8);
232+
mk[3] = GET_ULONG_BE(key, 12);
233+
k[0] = mk[0] ^ Fk[0];
234+
k[1] = mk[1] ^ Fk[1];
235+
k[2] = mk[2] ^ Fk[2];
236+
k[3] = mk[3] ^ Fk[3];
237237
for (; i < 32; i++)
238238
{
239-
k[(i + 4)] = (k[i] ^ Sm4CalciRk(k[(i + 1)] ^ k[(i + 2)] ^ k[(i + 3)] ^ (long)CK[i]));
240-
SK[i] = k[(i + 4)];
239+
k[(i + 4)] = (k[i] ^ Sm4CalciRk(k[(i + 1)] ^ k[(i + 2)] ^ k[(i + 3)] ^ Ck[i]));
240+
sk[i] = k[(i + 4)];
241241
}
242242
}
243243

@@ -376,7 +376,6 @@ public byte[] Sm4_crypt_cbc(Sm4Context ctx, byte[] iv, byte[] input)
376376
input = Padding(input, Sm4Encrypt);
377377
}
378378

379-
int i = 0;
380379
int length = input.Length;
381380
byte[] bins = new byte[length];
382381
Array.Copy(input, 0, bins, 0, length);

GameFrameX.Foundation.Extensions/LookupX.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public List<TElement> this[TKey key]
6262
get
6363
{
6464
ArgumentNullException.ThrowIfNull(key, nameof(key));
65-
return _dictionary.TryGetValue(key, out var value) ? value : [];
65+
return _dictionary.TryGetValue(key, out var value) ? value : new List<TElement> { };
6666
}
6767
}
6868

GameFrameX.Foundation.Extensions/NullObject.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static NullObject<T> Null
4040
/// <param name="value">要比较的对象。</param>
4141
/// <returns>一个整数,指示当前对象与 <paramref name="value" /> 的相对顺序。</returns>
4242
/// <exception cref="ArgumentException">当 Item 为 null 且无法进行比较时抛出。</exception>
43-
public int CompareTo(object? value)
43+
public int CompareTo(object value)
4444
{
4545
if (value is null)
4646
{
@@ -50,11 +50,19 @@ public int CompareTo(object? value)
5050
if (value is NullObject<T> nullObject)
5151
{
5252
if (Item is null && nullObject.Item is null)
53+
{
5354
return 0;
55+
}
56+
5457
if (Item is null)
58+
{
5559
return -1;
60+
}
61+
5662
if (nullObject.Item is null)
63+
{
5764
return 1;
65+
}
5866

5967
if (Item is IComparable comparable)
6068
{
@@ -78,14 +86,22 @@ public int CompareTo(object? value)
7886
/// <param name="other">要比较的对象。</param>
7987
/// <returns>一个整数,指示当前对象与 <paramref name="other" /> 的相对顺序。</returns>
8088
/// <exception cref="ArgumentException">当 Item 为 null 且无法进行比较时抛出。</exception>
81-
public int CompareTo(T? other)
89+
public int CompareTo(T other)
8290
{
8391
if (Item is null && other is null)
92+
{
8493
return 0;
94+
}
95+
8596
if (Item is null)
97+
{
8698
return -1;
99+
}
100+
87101
if (other is null)
102+
{
88103
return 1;
104+
}
89105

90106
if (Item is IComparable comparable)
91107
{

GameFrameX.Foundation.Extensions/NullableDictionary.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,21 @@ namespace GameFrameX.Foundation.Extensions;
1515
public readonly struct FallbackValue<TValue>
1616
{
1717
public TValue Value { get; }
18-
18+
19+
/// <summary>
20+
/// 初始化一个包含指定值的 FallbackValue
21+
/// </summary>
22+
/// <param name="value"></param>
1923
public FallbackValue(TValue value)
2024
{
2125
Value = value;
2226
}
23-
27+
28+
/// <summary>
29+
/// 将 TValue 转换为 FallbackValue
30+
/// </summary>
31+
/// <param name="value"></param>
32+
/// <returns></returns>
2433
public static implicit operator FallbackValue<TValue>(TValue value)
2534
{
2635
return new FallbackValue<TValue>(value);

GameFrameX.Foundation.Extensions/StringExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
namespace GameFrameX.Foundation.Extensions;
1111

12+
/// <summary>
13+
/// 提供字符串类型的扩展方法
14+
/// </summary>
1215
public static class StringExtensions
1316
{
1417
/// <summary>

GameFrameX.Foundation.Hash/MurmurHash3Helper.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System.Text;
22

3-
using System;
4-
using System.Text;
5-
63
namespace GameFrameX.Foundation.Hash;
74

85
/// <summary>

GameFrameX.Foundation.Tests/Hash/HmacSha256HelperTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using GameFrameX.Foundation.Hash;
2-
using System.Text;
32
using Xunit;
43

54
namespace GameFrameX.Foundation.Tests.Hash;
@@ -242,7 +241,7 @@ public void Hash_ValidBase64Output_ShouldBeValidBase64String()
242241
}
243242
catch (FormatException)
244243
{
245-
Assert.True(false, "返回的哈希值不是有效的Base64字符串");
244+
Assert.Fail("返回的哈希值不是有效的Base64字符串");
246245
}
247246
}
248247

0 commit comments

Comments
 (0)