public void TestECPublicKeyEquals() { curve25519.Curve25519Native native = new curve25519.Curve25519Native(); byte[] privKey = native.generatePrivateKey(); byte[] pubKey = native.generatePublicKey(privKey); DjbECPublicKey key1 = new DjbECPublicKey(pubKey); byte[] pubKey2 = native.generatePublicKey(privKey); Assert.IsTrue(StructuralComparisons.StructuralEqualityComparer.Equals( pubKey, pubKey2)); DjbECPublicKey key2 = new DjbECPublicKey(pubKey2); Assert.IsTrue(key1.Equals(key2)); int hash1 = key1.GetHashCode(); int hash2 = key2.GetHashCode(); Assert.AreEqual <int>(hash1, hash2); }
/// <summary> /// intercept iq and precess the keys /// </summary> /// <param name="node"></param> public ProtocolTreeNode[] ProcessIqTreeNode(ProtocolTreeNode node) { try { if (cipherKeys.Contains(node.GetAttribute("id"))) { cipherKeys.Remove(node.GetAttribute("id")); foreach (var child in node.children) { string jid = child.GetChild("user").GetAttribute("jid"); uint registrationId = deAdjustId(child.GetChild("registration").GetData()); IdentityKey identityKey = new IdentityKey(new DjbECPublicKey(child.GetChild("identity").GetData())); uint signedPreKeyId = deAdjustId(child.GetChild("skey").GetChild("id").GetData()); DjbECPublicKey signedPreKeyPub = new DjbECPublicKey(child.GetChild("skey").GetChild("value").GetData()); byte[] signedPreKeySig = child.GetChild("skey").GetChild("signature").GetData(); uint preKeyId = deAdjustId(child.GetChild("key").GetChild("id").GetData()); DjbECPublicKey preKeyPublic = new DjbECPublicKey(child.GetChild("key").GetChild("value").GetData()); PreKeyBundle preKeyBundle = new PreKeyBundle(registrationId, 1, preKeyId, preKeyPublic, signedPreKeyId, signedPreKeyPub, signedPreKeySig, identityKey); SessionBuilder sessionBuilder = new SessionBuilder(this, this, this, this, new AxolotlAddress(ExtractNumber(jid), 1)); // now do the work return nodelist sessionBuilder.process(preKeyBundle); // SignedPreKeyRecord rc = new SignedPreKeyRecord(signedPreKeyId, KeyHelper.getTime(), bobSignedPreKeyPair, signedPreKeySig); // this.StoreSignedPreKey(signedPreKeyId,rc); if (pending_nodes.ContainsKey(ExtractNumber(jid))) { var pendingNodes = pending_nodes[ExtractNumber(jid)].ToArray(); pending_nodes.Remove(ExtractNumber(jid)); return(pendingNodes); } } } } catch (Exception e) { } finally { } return(null); }
private void LoadKeyIfAvailable(IDataStore dataStore, object context) { string privateKey = dataStore.GetString(context, "privateKey"); string publicKey = dataStore.GetString(context, "publicKey"); if (privateKey == null || privateKey == null) { return; } byte[] publicKeyBytes = Convert.FromBase64String(publicKey); if (IsKeyArrayValid(publicKeyBytes)) { DjbECPublicKey pubKey = new DjbECPublicKey(publicKeyBytes.Skip(1).ToArray()); DjbECPrivateKey privKey = new DjbECPrivateKey(Convert.FromBase64String(privateKey)); keyPair = new ECKeyPair(pubKey, privKey); // Make sure the key is valid if (!VerifySecuritySignature(CalculateSecuritySignature())) { keyPair = null; } } }