private ReqDHParamsArgs CreateReqDhParamsArgs(ResPQ resPQ, out PQInnerData pqInnerData) { Int256 pq = resPQ.Pq.ToInt256(asLittleEndian: false); Int256 p, q; pq.GetPrimeMultipliers(out p, out q); Int256 newNonce = _nonceGenerator.GetNonce(32).ToInt256(); pqInnerData = new PQInnerData { Pq = resPQ.Pq, P = p.ToBytes(false, true), Q = q.ToBytes(false, true), Nonce = resPQ.Nonce, ServerNonce = resPQ.ServerNonce, NewNonce = newNonce }; byte[] data = _tlRig.Serialize(pqInnerData); byte[] dataHash = ComputeSHA1(data); Debug.Assert((dataHash.Length + data.Length) <= 255); // data_with_hash := SHA1(data) + data + (any random bytes); such that the length equal 255 bytes; var dataWithHash = new byte[255]; using (var streamer = new TLStreamer(dataWithHash)) { streamer.Write(dataHash); streamer.Write(data); streamer.WriteRandomDataTillEnd(); } PublicKey publicKey = _keyChain.GetFirst(resPQ.ServerPublicKeyFingerprints); if (publicKey == null) { throw new PublicKeyNotFoundException(resPQ.ServerPublicKeyFingerprints); } byte[] encryptedData = _encryptionServices.RSAEncrypt(dataWithHash, publicKey); var reqDhParamsArgs = new ReqDHParamsArgs { Nonce = pqInnerData.Nonce, ServerNonce = pqInnerData.ServerNonce, P = pqInnerData.P, Q = pqInnerData.Q, PublicKeyFingerprint = publicKey.Fingerprint, EncryptedData = encryptedData }; return(reqDhParamsArgs); }
//[TestCase("2852213850458175921094913949697", "51539607551", "55340232221128654847")] //[TestCase("2253601067072664030639173111353", "2932031007403", "768614336404564651")] //[TestCase("154866286100907105216716400854538488352313", "768614336404564651", "201487636602438195784363")] //[TestCase("170277282318432095373149951383568233665261047797485113", "201487636602438195784363", "845100400152152934331135470251")] //[TestCase("47928794074934470746074693488053803551576675688093033978263006055993", "845100400152152934331135470251", "56713727820156410577229101238628035243")] public void Should_get_prime_multipliers_for_Int256(string pqS, string expectedPs, string expectedQs) { Int256 pq = Int256.Parse(pqS); Int256 expectedP = Int256.Parse(expectedPs); Int256 expectedQ = Int256.Parse(expectedQs); Int256 p, q; pq.GetPrimeMultipliers(out p, out q); p.Should().Be(expectedP); q.Should().Be(expectedQ); }