Explorar o código

srp6计算hash时ToUBigIntegerArray可能需要指定长度,超出的字节填0

tanghai %!s(int64=13) %!d(string=hai) anos
pai
achega
d44020e947

+ 8 - 7
CSharp/App/LoginClient/SRP6Client.cs

@@ -48,7 +48,7 @@ namespace LoginClient
 			this.u = this.CalculateU();  // U = H(A, B)
 			this.s = this.CalculateS();  // S = (B - (k * g.ModExp(x, N))).ModExp(a + (u * x), N)
 			this.k = this.CalculateK();
-			this.m = this.CalculateM();  // 
+			this.m = this.CalculateM();  // H(H(N) ^ H(g), H(P), s, A, B, K)
 		}
 
 		public BigInteger N
@@ -185,9 +185,10 @@ namespace LoginClient
 		private BigInteger CalculateU()
 		{
 			hashAlgorithm.Initialize();
+
 			var joinBytes = new byte[0]
-				.Concat(this.A.ToUBigIntegerArray())
-				.Concat(this.B.ToUBigIntegerArray())
+				.Concat(this.A.ToUBigIntegerArray(32))
+				.Concat(this.B.ToUBigIntegerArray(32))
 				.ToArray();
 			return hashAlgorithm.ComputeHash(joinBytes).ToUBigInteger();
 		}
@@ -262,10 +263,10 @@ namespace LoginClient
 			var mBytes = hashAlgorithm.ComputeHash(new byte[0]
 				.Concat(hashGXorhashN)
 				.Concat(hashedIdentity)
-				.Concat(this.Salt.ToUBigIntegerArray())
-				.Concat(this.A.ToUBigIntegerArray())
-				.Concat(this.B.ToUBigIntegerArray())
-				.Concat(this.K.ToUBigIntegerArray())
+				.Concat(this.Salt.ToUBigIntegerArray(32))
+				.Concat(this.A.ToUBigIntegerArray(32))
+				.Concat(this.B.ToUBigIntegerArray(32))
+				.Concat(this.K.ToUBigIntegerArray(40))
 				.ToArray());
 			return mBytes.ToUBigInteger();
 		}

+ 15 - 1
CSharp/Platform/Helper/BigIntegerHelper.cs

@@ -19,7 +19,7 @@ namespace Helper
 			var bigIntegerBytes = new byte[byteNum];
 			var random = new Random();
 			random.NextBytes(bigIntegerBytes);
-			//bigIntegerBytes = "C5487A2909326D52A615C07ADFECB55FCD0771".HexToBytes().Reverse();
+			//bigIntegerBytes = "C6DFEDA1EAAC7417A191EE5EC6062CE9546614".HexToBytes().Reverse();
 
 			return bigIntegerBytes.ToUBigInteger();
 		}
@@ -46,6 +46,20 @@ namespace Helper
 			return result;
 		}
 
+		public static byte[] ToUBigIntegerArray(this BigInteger bigInteger, int length)
+		{
+			var result = bigInteger.ToByteArray();
+			if (result[result.Length - 1] == 0 && (result.Length % 0x10) != 0)
+			{
+				Array.Resize(ref result, result.Length - 1);
+			}
+			if (length > result.Length)
+			{
+				Array.Resize(ref result, length);
+			}
+			return result;
+		}
+
 		/// <summary>
 		/// 返回非负值
 		/// </summary>