git-svn-id: svn://db.shs.com.ru/pip@661 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5
This commit is contained in:
331
main.cpp
331
main.cpp
@@ -1,112 +1,239 @@
|
|||||||
#include "picout.h"
|
#include "pip.h"
|
||||||
#include "pivector2d.h"
|
#include "piauth.h"
|
||||||
#include "Eigen/Dense"
|
|
||||||
|
|
||||||
typedef Eigen::MatrixXd PIMathMatrixd;
|
/*
|
||||||
typedef Eigen::MatrixXf PIMathMatrixf;
|
class Obj : public PIObject {
|
||||||
typedef Eigen::MatrixXcf PIMathMatrixcf;
|
PIOBJECT(Obj)
|
||||||
typedef Eigen::MatrixXcd PIMathMatrixcd;
|
public:
|
||||||
|
Obj() : PIObject() {}
|
||||||
|
|
||||||
template<typename Type>
|
EVENT_HANDLER2(void, authorizeServer, PIByteArray, info, bool *, ok) {
|
||||||
inline PICout operator <<(PICout s, const Eigen::DenseCoeffsBase<Type, Eigen::ReadOnlyAccessors> & m) {s << '{'; for (uint r = 0; r < m.rows(); ++r) { for (uint c = 0; c < m.cols(); ++c) { s << m(r, c); if (c < m.cols() - 1 || r < m.rows() - 1) s << ", ";} if (r < m.rows() - 1) s << PICoutManipulators::NewLine << ' ';} s << '}'; return s;}
|
piCout << "[authorizeServer]" << PIString(info);
|
||||||
template<typename Type>
|
//*ok = true;
|
||||||
inline PIVector<PIVector<typename Type::Scalar> > toVectors(const Eigen::DenseCoeffsBase<Type, Eigen::ReadOnlyAccessors> & m) {
|
}
|
||||||
PIVector<PIVector<typename Type::Scalar> > ret;
|
EVENT_HANDLER2(void, passwordCheck, PIByteArray, phash, bool *, ok) {
|
||||||
for(int i = 0; i < m.cols(); ++i)
|
piCout << "[passwordCheck]" << phash.toHex();
|
||||||
ret << PIVector<typename Type::Scalar>(&(m(i*m.rows())), m.rows());
|
PIByteArray ph = PICrypt::passwordHash("secret", PIByteArray::fromHex("AABBCCDD"));
|
||||||
|
piCout << "[passwordCheck]" << ph.toHex();
|
||||||
|
if (phash == ph) *ok = true;
|
||||||
|
}
|
||||||
|
EVENT_HANDLER1(void, userEnterPassword, PIString *, password) {
|
||||||
|
*password = "secret";
|
||||||
|
piCout << "[userEnterPassword]" << *password;
|
||||||
|
}
|
||||||
|
EVENT_HANDLER(void, clientConnect) {
|
||||||
|
piCout << "clientConnect OK!";
|
||||||
|
}
|
||||||
|
EVENT_HANDLER(void, serverConnect) {
|
||||||
|
piCout << "serverConnect OK!";
|
||||||
|
}
|
||||||
|
EVENT_HANDLER1(void, f, int, id) {
|
||||||
|
piCout << "finished" << id;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
class A: public PIObject {
|
||||||
|
PIOBJECT(A)
|
||||||
|
public:
|
||||||
|
A() {}
|
||||||
|
EVENT_HANDLER2(void, pcf, int, id, PIString*, buff) {
|
||||||
|
piCout << "PICout(" << id << ") finished:" << (*buff);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
/*class O {
|
||||||
|
public:
|
||||||
|
O() {piCout << "O";}
|
||||||
|
~O() {piCout << "~O";}
|
||||||
|
};
|
||||||
|
|
||||||
|
class Buffer {
|
||||||
|
public:
|
||||||
|
Buffer() {}
|
||||||
|
template<typename T>
|
||||||
|
static Buffer create() {
|
||||||
|
Buffer ret;
|
||||||
|
ret.data = new T();
|
||||||
|
ret.pdestroy = (void *)(T::~T);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
void destroy() {}
|
||||||
template<typename Type>
|
void * data;
|
||||||
inline Eigen::Matrix<Type, Eigen::Dynamic, Eigen::Dynamic> fromVectors(const PIVector<PIVector<Type> > & m) {
|
void * pdestroy;
|
||||||
if (m.isEmpty()) return Eigen::Matrix<Type, Eigen::Dynamic, Eigen::Dynamic>(0, 0);
|
};
|
||||||
if (m[0].isEmpty()) return Eigen::Matrix<Type, Eigen::Dynamic, Eigen::Dynamic>(1, 0);
|
|
||||||
Eigen::Matrix<Type, Eigen::Dynamic, Eigen::Dynamic> ret(m[0].size(), m.size());
|
|
||||||
for(int i = 0; i < m.size_s(); ++i)
|
|
||||||
for(int j = 0; j < m[i].size_s(); ++j)
|
|
||||||
ret(j,i) = m[i][j];
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
template<typename Type>
|
|
||||||
inline PIVector<typename Type::Scalar> toPlainVector(const Eigen::DenseCoeffsBase<Type, Eigen::ReadOnlyAccessors> & m) {
|
|
||||||
return PIVector<typename Type::Scalar>(&(m(0)), m.size());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
class __BufferStorage {
|
||||||
|
public:
|
||||||
|
|
||||||
|
};
|
||||||
|
*/
|
||||||
int main() {
|
int main() {
|
||||||
// Eigen::MatrixXd;
|
//Buffer b = Buffer::create<O>();
|
||||||
// PIMathMatrix<complexd> m1(2,2, PIVector<complexd>() << complexd(1.) << complexd(1.) << complexd(0.) << complexd(1.));
|
|
||||||
// PIMathMatrix<complexd> m2(2,3, PIVector<complexd>() << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.));
|
|
||||||
PIMathMatrixd m1(3,3);
|
|
||||||
m1 << 1,2,3,4,6,5,9,8,7;
|
|
||||||
// PIMathMatrix<double> m2(2,3, PIVector<double>() << 1. << 2. << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.) << complexd(2., 3.));
|
|
||||||
// PIMathMatrix<double> m3(vv);
|
|
||||||
// piCout << m2 << PICoutManipulators::NewLine;
|
|
||||||
// piCout << (PIVector2D<complexd>)m2 << PICoutManipulators::NewLine;
|
|
||||||
// for (uint r = 0; r < m2.rows(); ++r) {
|
|
||||||
// for (uint c = 0; c < m2.cols(); ++c) {
|
|
||||||
//// piCout << m2[r][c];
|
|
||||||
// m2[r][c] = complexd(m2[r][c].real(), -(m2[r][c].imag()));
|
|
||||||
//// m2[r][c] = complexd();
|
|
||||||
|
|
||||||
|
|
||||||
|
// PIEvaluator eval, eval2;
|
||||||
|
// eval.check("1-1*2");
|
||||||
|
// piCout << eval.evaluate().real();
|
||||||
|
/*eval.setVariable("t", complexd(1, 2));
|
||||||
|
eval.check("(t*t*(t<8))+(72*(t>8)-8*(9-t)*(9-t)*(t>8)*(t<8.8))+(3*(t-8.8)*(t>8.8))");
|
||||||
|
PIByteArray ba = eval.save();
|
||||||
|
piCout << ba.size();
|
||||||
|
PITimeMeasurer tm; double us = 0.;
|
||||||
|
|
||||||
|
tm.reset();
|
||||||
|
PIEvaluator e1;
|
||||||
|
piForTimes(100) {
|
||||||
|
e1.setVariable("t", complexd(1, 2));
|
||||||
|
e1.check("(t*t*(t<8))+(72*(t>8)-8*(9-t)*(9-t)*(t>8)*(t<8.8))+(3*(t-8.8)*(t>8.8))");
|
||||||
|
complexd ret = e1.evaluate();
|
||||||
|
}
|
||||||
|
us = tm.elapsed_u(); piCout << "check" << us / 100.;
|
||||||
|
|
||||||
|
tm.reset();
|
||||||
|
PIEvaluator e2;
|
||||||
|
e2.load(ba);
|
||||||
|
piForTimes(10000) {
|
||||||
|
e2.setVariable("t", complexd(1, 2));
|
||||||
|
complexd ret = e2.evaluate();
|
||||||
|
}
|
||||||
|
us = tm.elapsed_u(); piCout << " save" << us / 10000.;*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*A a;
|
||||||
|
CONNECTU(PICout::Notifier::object(), finished, &a, pcf);
|
||||||
|
PIString buffer = "my buff:";
|
||||||
|
PICout(&buffer, 1) << "int 10 ->" << 10 << ", time ->" << PITime::current();*/
|
||||||
|
/*PISerial ser(PISerial::availableDevices(false)[0], PISerial::S115200);
|
||||||
|
piCout << ser.constructFullPath();
|
||||||
|
piCout << ser.open();
|
||||||
|
ser.startThreadedRead();
|
||||||
|
FOREVER {
|
||||||
|
piCout << ser.isOpened();
|
||||||
|
piMSleep(200);
|
||||||
|
}*/
|
||||||
|
/*PIVector<PIFile::FileInfo> fl = PIDir::current().allEntries();
|
||||||
|
PIFile out("out.txt", PIIODevice::ReadWrite);
|
||||||
|
out.clear();
|
||||||
|
piForeachC (PIFile::FileInfo & f, fl) {
|
||||||
|
PIString e = f.extension();
|
||||||
|
PIString comm;
|
||||||
|
if (e == "h") comm = PIString::fromUTF8("Заголовочный файл");
|
||||||
|
if (e == "cpp") comm = PIString::fromUTF8("Файл релизации");
|
||||||
|
if (e == "ui") comm = PIString::fromUTF8("Форма");
|
||||||
|
if (e == "qrc") comm = PIString::fromUTF8("Файл ресурсов");
|
||||||
|
if (comm.isEmpty()) continue;
|
||||||
|
out << "\n\n" << comm << ": " << f.name() << "\n";
|
||||||
|
PIFile file(f.name(), PIIODevice::ReadOnly);
|
||||||
|
while (!file.isEnd()) {
|
||||||
|
PIString line = file.readLine(), tl = line.trimmed();
|
||||||
|
if (tl.isEmpty() || tl.startsWith("//")) continue;
|
||||||
|
out << line << "\n";
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
/*}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]) {
|
||||||
|
Obj o;
|
||||||
|
CONNECTU(PICout::Notifier::object(), finished, &o, f);
|
||||||
|
PIString s = "!!!";
|
||||||
|
piCout << s;
|
||||||
|
PICout(&s, 1) << "now" << PIDateTime::current() << ", int 1 =" << 1;
|
||||||
|
piCout << s;*/
|
||||||
|
|
||||||
|
/*piCout << "start";
|
||||||
|
PICrypt crypt;
|
||||||
|
PIByteArray skey1, pkey1;
|
||||||
|
crypt.generateKeypair(pkey1, skey1);
|
||||||
|
PIByteArray skey2, pkey2;
|
||||||
|
crypt.generateKeypair(pkey2, skey2);
|
||||||
|
PIByteArray sign = PIAuth::generateSign(PICrypt::generateKey());
|
||||||
|
piCout << "sign" << sign.toHex();
|
||||||
|
|
||||||
|
piCout << "key1 " << pkey1.toHex() << skey1.toHex();
|
||||||
|
piCout << "key2 " << pkey2.toHex() << skey2.toHex();
|
||||||
|
PIString msg = "what the f*ck?";
|
||||||
|
PIByteArray ba = msg.toUTF8();
|
||||||
|
piCout << "source" << ba.toHex() << msg;
|
||||||
|
PIByteArray cba = crypt.crypt(ba, pkey2, skey1);
|
||||||
|
PIByteArray psign = crypt.extractSignPublicKey(sign);
|
||||||
|
PIByteArray scba = crypt.signMessage(cba, sign);
|
||||||
|
piCout << "crypted" << cba.toHex();
|
||||||
|
piCout << "signed" << scba.toHex() << psign.toHex();
|
||||||
|
PIByteArray dba = crypt.decrypt(cba, pkey1, skey2);
|
||||||
|
piCout << "decrypted" << dba.toHex() << PIString(dba);
|
||||||
|
piCout << "verify" << crypt.verifySign(cba, scba, psign);
|
||||||
|
cba = crypt.crypt(ba, pkey1, skey2);
|
||||||
|
piCout << "crypted" << cba.toHex();
|
||||||
|
//cba[7] = 0;
|
||||||
|
dba = crypt.decrypt(cba, pkey2, skey1);
|
||||||
|
piCout << "decrypted" << dba.toHex() << PIString(dba);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
piCout << "=======================================";
|
||||||
|
|
||||||
|
PIAuth server(PIAuth::generateSign(pkey1));
|
||||||
|
|
||||||
|
msg = "This is Server with PIAuth";
|
||||||
|
server.setInfoData(msg.toUTF8());
|
||||||
|
|
||||||
|
PIAuth client(PIAuth::generateSign(pkey2));
|
||||||
|
|
||||||
|
client.setAuthorizedPublicKeys(PIVector<PIByteArray>() << server.getSignPublicKey());
|
||||||
|
// server.setAuthorizedPublicKeys(PIVector<PIByteArray>() << client.getSignPublicKey());
|
||||||
|
Obj o;
|
||||||
|
CONNECTU(&client, authorize, &o, authorizeServer);
|
||||||
|
CONNECTU(&client, passwordRequest, &o, userEnterPassword);
|
||||||
|
CONNECTU(&server, passwordCheck, &o, passwordCheck);
|
||||||
|
CONNECTU(&client, connected, &o, clientConnect);
|
||||||
|
CONNECTU(&server, connected, &o, serverConnect);
|
||||||
|
client.startClient();
|
||||||
|
ba = server.startServer();
|
||||||
|
int st = PIAuth::AuthProbe;
|
||||||
|
piCout << "server" << st << ba.toHex() << ba.size();*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ba[40] = 2;
|
||||||
|
// {
|
||||||
|
// int s;
|
||||||
|
// ba >> s;
|
||||||
|
// PIByteArray rinfo;
|
||||||
|
// PIByteArray rsign;
|
||||||
|
// PIByteArray rsign_pk;
|
||||||
|
// PIByteArray box_pk;
|
||||||
|
// PIByteArray noise;
|
||||||
|
// ba >> rinfo >> rsign_pk >> box_pk >> noise >> rsign;
|
||||||
|
// ba.clear();
|
||||||
|
// PIByteArray sign2 = PIAuth::generateSign(PICrypt::generateRandomBuff(100));
|
||||||
|
// msg = "Server";
|
||||||
|
// ba << s << msg.toUTF8() << crypt.extractSignPublicKey(sign2) << box_pk << noise;
|
||||||
|
// rsign = crypt.signMessage(ba, sign2);
|
||||||
|
// ba << rsign;
|
||||||
// }
|
// }
|
||||||
// }
|
|
||||||
piCout << m1 << PICoutManipulators::NewLine;
|
|
||||||
piCout << toPlainVector(m1);
|
/*
|
||||||
// piCout << m2 << PICoutManipulators::NewLine;
|
st = client.receive(ba);
|
||||||
// piCout << (PIVector2D<complexd>)m2 << PICoutManipulators::NewLine;
|
piCout << "client" << st << ba.toHex() << ba.size();
|
||||||
// piCout << m2.transposed() << PICoutManipulators::NewLine;
|
st = server.receive(ba);
|
||||||
// piCout << hermitian(m2) << PICoutManipulators::NewLine;
|
piCout << "server" << st << ba.toHex() << ba.size();
|
||||||
// piCout << (m2*m1);
|
st = client.receive(ba);
|
||||||
// PIMathVector<complexd> v(PIVector<complexd>() << complexd(1.) << complexd(2.));
|
piCout << "client" << st << ba.toHex() << ba.size();
|
||||||
// piCout << (m1*PIMathMatrix<complexd>::matrixCol(v));```
|
st = server.receive(ba);
|
||||||
// piCout << m2;
|
piCout << "server" << st << ba.toHex() << ba.size();
|
||||||
// piCout << m3;
|
st = client.receive(ba);
|
||||||
// bool ok;
|
piCout << "client" << st << ba.toHex() << ba.size();
|
||||||
PIMathMatrixd mi = m1.inverse();
|
st = server.receive(ba);
|
||||||
piCout << mi;
|
piCout << "server" << st << ba.toHex() << ba.size();
|
||||||
piCout << PIMathMatrixd(mi * m1);
|
st = client.receive(ba);
|
||||||
piCout << m1.row(0);
|
piCout << "client" << st << ba.toHex() << ba.size();
|
||||||
piCout << m1.row(1);
|
st = server.receive(ba);
|
||||||
piCout << m1.row(2);
|
piCout << "server" << st << ba.toHex() << ba.size();*/
|
||||||
piCout << "";
|
|
||||||
piCout << m1.col(0);
|
|
||||||
piCout << m1.col(1);
|
|
||||||
piCout << m1.col(2);
|
|
||||||
//m2 = m2.transposed();
|
|
||||||
// piCout << m2 << PICoutManipulators::NewLine << hermitian(m2);
|
|
||||||
// piCout << PICoutManipulators::NewLine << hermitian(m2) * mi;
|
|
||||||
// piCout << PICoutManipulators::NewLine << m1 * m2 << PICoutManipulators::NewLine << m2 * m1;
|
|
||||||
// piCout << PICoutManipulators::NewLine << hermitian(m1*m2) << PICoutManipulators::NewLine << hermitian(m2) * hermitian(m1);
|
|
||||||
// PIVector2D<double> v(3,2);
|
|
||||||
// for (uint r = 0; r < v.rows(); ++r) {
|
|
||||||
// for (uint c = 0; c < v.cols(); ++c) {
|
|
||||||
// piCout << r*v.cols()+c+1;
|
|
||||||
// v[r][c] = r*v.cols()+c+1;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// piCout << v;
|
|
||||||
// for (uint c = 0; c < v.cols(); ++c) {
|
|
||||||
// for (uint r = 0; r < v.rows(); ++r) {
|
|
||||||
// piCout << r*v.cols()+c+1;
|
|
||||||
// v[r][c] = -v[r][c];
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
piCout << "-------" << PICoutManipulators::NewLine;
|
|
||||||
PIMathMatrixd v = m1.block(0,0, 3,2);
|
|
||||||
m1 = v;
|
|
||||||
// PIMathMatrixd v2 = m1.block(0,0, 2,3);
|
|
||||||
// PIMathMatrixd v2(2,3, m1.toPlainVector().resize(6));
|
|
||||||
// piCout << v << PICoutManipulators::NewLine;
|
|
||||||
piCout << m1 << PICoutManipulators::NewLine;
|
|
||||||
PIVector<PIVector<double> > vv = toVectors(m1);
|
|
||||||
piCout << vv << PICoutManipulators::NewLine;
|
|
||||||
piCout << fromVectors(vv) << PICoutManipulators::NewLine;
|
|
||||||
// piCout << toVectors(PIMathMatrixd(m1.transpose())) << PICoutManipulators::NewLine;
|
|
||||||
// PIMathMatrix<double> v2 = PIMathMatrix<double>(v.toVectors());
|
|
||||||
// piCout << v2 << PICoutManipulators::NewLine;
|
|
||||||
// piCout << v*v2 << PICoutManipulators::NewLine;
|
|
||||||
// piCout << v2*v << PICoutManipulators::NewLine;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user