Example #1
0
		public byte[] ToBytes(byte[] nonce, byte[] serverNonce, List<byte[]> fingerprints, BigInteger pq)
		{
			new Random().NextBytes(newNonce);
			
			var pqPair = Factorizator.Factorize(pq);

			byte[] reqDhParamsBytes;

			using (MemoryStream pqInnerData = new MemoryStream(255))
			{
				using (BinaryWriter pqInnerDataWriter = new BinaryWriter(pqInnerData))
				{
					pqInnerDataWriter.Write(0x83c95aec); // pq_inner_data
					Serializers.Bytes.write(pqInnerDataWriter, pq.ToByteArrayUnsigned());
					Serializers.Bytes.write(pqInnerDataWriter, pqPair.Min.ToByteArrayUnsigned());
					Serializers.Bytes.write(pqInnerDataWriter, pqPair.Max.ToByteArrayUnsigned());
					pqInnerDataWriter.Write(nonce);
					pqInnerDataWriter.Write(serverNonce);
					pqInnerDataWriter.Write(newNonce);

					byte[] ciphertext = null;
					byte[] targetFingerprint = null;
					foreach (byte[] fingerprint in fingerprints)
					{
						ciphertext = RSA.Encrypt(BitConverter.ToString(fingerprint).Replace("-", string.Empty),
												 pqInnerData.GetBuffer(), 0, (int)pqInnerData.Position);
						if (ciphertext != null)
						{
							targetFingerprint = fingerprint;
							break;
						}
					}

					if (ciphertext == null)
					{
						throw new InvalidOperationException(
							String.Format("not found valid key for fingerprints: {0}", String.Join(", ", fingerprints)));
					}

					using (MemoryStream reqDHParams = new MemoryStream(1024))
					{
						using (BinaryWriter reqDHParamsWriter = new BinaryWriter(reqDHParams))
						{
							reqDHParamsWriter.Write(0xd712e4be); // req_dh_params
							reqDHParamsWriter.Write(nonce);
							reqDHParamsWriter.Write(serverNonce);
							Serializers.Bytes.write(reqDHParamsWriter, pqPair.Min.ToByteArrayUnsigned());
							Serializers.Bytes.write(reqDHParamsWriter, pqPair.Max.ToByteArrayUnsigned());
							reqDHParamsWriter.Write(targetFingerprint);
							Serializers.Bytes.write(reqDHParamsWriter, ciphertext);

							reqDhParamsBytes = reqDHParams.ToArray();
						}
					}
				}
				return reqDhParamsBytes;
			}
		}
Example #2
0
 public AuthKey(BigInteger gab) {
     key = gab.ToByteArrayUnsigned();
     using(SHA1 hash = new SHA1Managed()) {
         using(MemoryStream hashStream = new MemoryStream(hash.ComputeHash(key), false)) {
             using(BinaryReader hashReader = new BinaryReader(hashStream)) {
                 auxHash = hashReader.ReadUInt64();
                 hashReader.ReadBytes(4);
                 keyId = hashReader.ReadUInt64();
             }
         }
     }
 }