private async Task <bool> ValidateSignatureAsync(Address address, TryteString bundleTrytes, int contactPayloadEnd) { var signatureLength = Constants.MessengerSecurityLevel * Fragment.Length; var signature = bundleTrytes.GetChunk(contactPayloadEnd + Constants.End.TrytesLength, signatureLength); var publicKeyPayload = new PublicKeyPayload(bundleTrytes.GetChunk(0, contactPayloadEnd + Constants.End.TrytesLength).Value); return(await this.SignatureValidator.ValidateFragmentsAsync( signature.GetChunks(Fragment.Length).Select(c => new Fragment(c.Value)).ToList(), publicKeyPayload.Hash, address)); }
protected async Task <TryteString> CreateSignedPublicKeyPayloadAsync(IAsymmetricKey publicKey, TryteString requestAddress, AbstractPrivateKey addressPrivateKey) { var payload = new PublicKeyPayload(publicKey, requestAddress); var signature = await this.SignatureGenerator.GenerateAsync(addressPrivateKey, payload.Hash); var signedPayload = (TryteString)payload; foreach (var fragment in signature) { signedPayload = signedPayload.Concat(fragment); } return(signedPayload); }
public void InitDiffieHellman_ShouldGenerateSameDerivedKeys() //imitating client to test encrypting { ECDiffieHellmanCng eCDiffie = new ECDiffieHellmanCng(256); eCDiffie.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash; eCDiffie.HashAlgorithm = CngAlgorithm.Sha256; byte[] myPublicKey = eCDiffie.ExportSubjectPublicKeyInfo(); //export in x509 format String myPublicKeyBase64 = Convert.ToBase64String(myPublicKey); //Passing to project var packetToPreoject = new Packet('0', Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Concat("{", String.Format("\"Public_key\": \"{0}\"", myPublicKeyBase64), "}")))); ServiceProcessor.UserLoggedIn stubMethod = StubMethod; PacketProcessor packetProcessor = new PacketProcessor(stubMethod); Packet response = packetProcessor.Process(packetToPreoject); PublicKeyPayload serverPublicKey = JsonSerializer.Deserialize <PublicKeyPayload>(Convert.FromBase64String(response.Payload)); byte[] otherKeyFromBase64 = Convert.FromBase64String(serverPublicKey.Public_key); ECDiffieHellmanCng eCDiffie2 = new ECDiffieHellmanCng(256); eCDiffie2.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash; eCDiffie2.HashAlgorithm = CngAlgorithm.Sha256; int some = 0; eCDiffie2.ImportSubjectPublicKeyInfo(otherKeyFromBase64, out some); byte[] otherKeyDecoded = eCDiffie2.PublicKey.ToByteArray(); CngKey k = CngKey.Import(otherKeyDecoded, CngKeyBlobFormat.EccPublicBlob); byte[] derivedKey = eCDiffie.DeriveKeyMaterial(k); string actual = Convert.ToBase64String(packetProcessor.EncryptionModule.DerivedKey); string expected = Convert.ToBase64String(derivedKey); Assert.Equal(expected, actual); }