int cServer::Init(short a_ListenPort, short a_ConnectPort) { m_ConnectPort = a_ConnectPort; WSAData wsa; int res = WSAStartup(0x0202, &wsa); if (res != 0) { printf("Cannot initialize WinSock: %d\n", res); return res; } printf("Generating protocol encryption keypair...\n"); time_t CurTime = time(NULL); RandomPool rng; rng.Put((const byte *)&CurTime, sizeof(CurTime)); m_PrivateKey.GenerateRandomWithKeySize(rng, 1024); RSA::PublicKey pk(m_PrivateKey); m_PublicKey = pk; m_ListenSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); sockaddr_in local; memset(&local, 0, sizeof(local)); local.sin_family = AF_INET; local.sin_addr.s_addr = 0; // All interfaces local.sin_port = htons(a_ListenPort); bind(m_ListenSocket, (sockaddr *)&local, sizeof(local)); listen(m_ListenSocket, 1); printf("Listening on port %d, connecting to localhost:%d\n", a_ListenPort, a_ConnectPort); return 0; }
void SecretShareFile(int threshold, int nShares, const char *filename, const char *seed) { assert(nShares<=1000); RandomPool rng; rng.Put((byte *)seed, strlen(seed)); ChannelSwitch *channelSwitch; FileSource source(filename, false, new SecretSharing(rng, threshold, nShares, channelSwitch = new ChannelSwitch)); vector_member_ptrs<FileSink> fileSinks(nShares); string channel; for (int i=0; i<nShares; i++) { char extension[5] = ".000"; extension[1]='0'+byte(i/100); extension[2]='0'+byte((i/10)%10); extension[3]='0'+byte(i%10); fileSinks[i].reset(new FileSink((string(filename)+extension).c_str())); channel = WordToString<word32>(i); fileSinks[i]->Put((byte *)channel.data(), 4); channelSwitch->AddRoute(channel, *fileSinks[i], BufferedTransformation::NULL_CHANNEL); } source.PumpAll(); }
string RSAEncryptString(const char *pubFilename, const char *seed, const char *message) { FileSource pubFile(pubFilename, true, new HexDecoder); RSAES_OAEP_SHA_Encryptor pub(pubFile); RandomPool randPool; randPool.Put((byte *)seed, strlen(seed)); string result; StringSource(message, true, new PK_EncryptorFilter(randPool, pub, new HexEncoder(new StringSink(result)))); return result; }
string RsaEncrypt(const string& pubkey, const char * data,int datalen) { StringSource pubSrc(pubkey,true); RSAES_OAEP_SHA_Encryptor pub(pubSrc); RandomPool randPool; string seed=GenRandomStr(16,32); randPool.IncorporateEntropy((byte *)seed.c_str(), seed.length()); string result; StringSource((const byte *)data,datalen, true, new PK_EncryptorFilter(randPool, pub,new StringSink(result))); return result; }
void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed) { RandomPool randPool; randPool.Put((byte *)seed, strlen(seed)); RSAES_PKCS1v15_Decryptor priv(randPool, keyLength); FileSink privFile(privFilename); priv.DEREncode(privFile); privFile.MessageEnd(); RSAES_PKCS1v15_Encryptor pub(priv); FileSink pubFile(pubFilename); pub.DEREncode(pubFile); pubFile.MessageEnd(); }
void GenerateRSAKey(unsigned int keyLength, const char *privFilename, const char *pubFilename, const char *seed) { RandomPool randPool; randPool.IncorporateEntropy((byte *)seed, strlen(seed)); RSAES_OAEP_SHA_Decryptor priv(randPool, keyLength); HexEncoder privFile(new FileSink(privFilename)); priv.DEREncode(privFile); privFile.MessageEnd(); RSAES_OAEP_SHA_Encryptor pub(priv); HexEncoder pubFile(new FileSink(pubFilename)); pub.DEREncode(pubFile); pubFile.MessageEnd(); }
void SecretShareFile(int threshold, int nShares, const char *filename, const char *seed) { assert(nShares<=1000); RandomPool rng; rng.IncorporateEntropy((byte *)seed, strlen(seed)); ChannelSwitch *channelSwitch; string data = "ABCDABCD"; //FileSource source(filename, false, new SecretSharing(rng, threshold, nShares, channelSwitch = new ChannelSwitch)); ArraySource source((byte *)data.c_str(), data.length() + 4, false, new SecretSharing(rng, threshold, nShares, channelSwitch = new ChannelSwitch)); std::vector<string> buf(nShares); /*SecretSharing *sss = new SecretSharing(rng, threshold, nShares, channelSwitch = new ChannelSwitch); string pt = "abcd"; const unsigned char *c = (const unsigned char*) pt.c_str(); sss->Put(c, 5, true); sss->MessageEnd(-1, true); bool mesgs = sss->AnyMessages(); cout<<mesgs<<endl; lword max_ret = sss->MaxRetrievable(); cout<<max_ret<<endl; */ vector_member_ptrs<FileSink> fileSinks(nShares); vector_member_ptrs<ArraySink> sinks(nShares); string channel; for (int i=0; i<nShares; i++) { char extension[5] = ".000"; extension[1]='0'+byte(i/100); extension[2]='0'+byte((i/10)%10); extension[3]='0'+byte(i%10); fileSinks[i].reset(new FileSink((string(filename)+extension).c_str())); sinks[i].reset(new ArraySink((byte *) buf.at(i).c_str(), buf.at(i).length())); channel = WordToString<word32>(i); //cout<<channel.data()<<endl; //fileSinks[i]->Put((byte *)channel.data(), 4); sinks[i]->Put((byte *) channel.data(), 4); //channelSwitch->AddRoute(channel, *fileSinks[i], DEFAULT_CHANNEL); channelSwitch->AddRoute(channel, *sinks[i], DEFAULT_CHANNEL); } //istream *is = source.GetStream(); //cout<<is->rdbuf(); /*char cs = is->get(); while(is) { //cout<<cs; cs = is->get(); }*/ source.PumpAll(); cout<<buf.at(0)<<endl; }
//genrate random pubkey and privkey. bool GenKeyPair(string & pubKey, string& privKey,int keylen) { pubKey=""; privKey=""; RandomPool rp; string seed=GenRandomStr(32,64); rp.IncorporateEntropy((const byte *)seed.c_str(),seed.length()); RSAES_OAEP_SHA_Decryptor priv(rp,keylen); StringSink privkey(privKey); priv.DEREncode(privkey); privkey.MessageEnd(); RSAES_OAEP_SHA_Encryptor pub(priv); StringSink pubkey(pubKey); pub.DEREncode(pubkey); pubkey.MessageEnd(); return true; }
int DetectorDriver::ThreshAndCal(ChanEvent *chan, RawEvent& rawev) { Identifier chanId = chan->GetChanID(); int id = chan->GetID(); string type = chanId.GetType(); string subtype = chanId.GetSubtype(); map<string, int> tags = chanId.GetTagMap(); bool hasStartTag = chanId.HasTag("start"); Trace &trace = chan->GetTrace(); RandomPool* randoms = RandomPool::get(); double energy = 0.0; if (type == "ignore" || type == "") return(0); if ( !trace.empty() ) { plot(D_HAS_TRACE, id); for (vector<TraceAnalyzer *>::iterator it = vecAnalyzer.begin(); it != vecAnalyzer.end(); it++) { (*it)->Analyze(trace, type, subtype, tags); } if (trace.HasValue("filterEnergy") ) { if (trace.GetValue("filterEnergy") > 0) { energy = trace.GetValue("filterEnergy"); plot(D_FILTER_ENERGY + id, energy); trace.SetValue("filterEnergyCal", cali.GetCalEnergy(chanId, trace.GetValue("filterEnergy"))); } else { energy = 0.0; } /** Calibrate pulses numbered 2 and forth, * add filterEnergyXCal to the trace */ int pulses = trace.GetValue("numPulses"); for (int i = 1; i < pulses; ++i) { stringstream energyName; energyName << "filterEnergy" << i + 1; stringstream energyCalName; energyCalName << "filterEnergy" << i + 1 << "Cal"; trace.SetValue(energyCalName.str(), cali.GetCalEnergy(chanId, trace.GetValue(energyName.str()))); } } if (trace.HasValue("calcEnergy") ) { energy = trace.GetValue("calcEnergy"); chan->SetEnergy(energy); } else if (!trace.HasValue("filterEnergy")) { energy = chan->GetEnergy() + randoms->Get(); } if (trace.HasValue("phase") ) { //Saves the time in ns chan->SetHighResTime((trace.GetValue("phase") * Globals::get()->adcClockInSeconds() + chan->GetTrigTime() * Globals::get()->filterClockInSeconds())*1.e9); } } else { /// otherwise, use the Pixie on-board calculated energy /// add a random number to convert an integer value to a /// uniformly distributed floating point energy = chan->GetEnergy() + randoms->Get(); chan->SetHighResTime(0.0); } /** Calibrate energy and apply the walk correction. */ double time, walk_correction; if(chan->GetHighResTime() == 0.0) { time = chan->GetTime(); //time is in clock ticks walk_correction = walk.GetCorrection(chanId, energy); } else { time = chan->GetHighResTime(); //time here is in ns walk_correction = walk.GetCorrection(chanId, trace.GetValue("tqdc")); } chan->SetCalEnergy(cali.GetCalEnergy(chanId, energy)); chan->SetCorrectedTime(time - walk_correction); rawev.GetSummary(type)->AddEvent(chan); DetectorSummary *summary; summary = rawev.GetSummary(type + ':' + subtype, false); if (summary != NULL) summary->AddEvent(chan); if(hasStartTag && type != "logic") { summary = rawev.GetSummary(type + ':' + subtype + ':' + "start", false); if (summary != NULL) summary->AddEvent(chan); } return(1); }