Crypto++ 5.6.3rc5 GenerateBlock Not Implemented - c++

I am trying to derive key from password and want to generate randomly the salt(I dont know what size it should be for SHA-256 and does this matter like the IV in AES256, where it should be 128 bit,give a hint if someone know) with AutoSeededRandomPool but exception is cought
RandomNumberGenerator:GenerateBlock Not Implemented
I am using crypto++ 5.6.3rc5 with QT 5.5.1 and /MD release mode, this may be a bug, or unfinished work of someone.
#include <QCoreApplication>
#include <sha.h>
#include <base64.h>
#include <iostream>
#include <string>
#include <pwdbased.h>
#include <cstdio>
#include <iostream>
#include <osrng.h>
using CryptoPP::AutoSeededRandomPool;
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
#include <string>
using std::string;
#include <cstdlib>
using std::exit;
#include <cryptlib.h>
using CryptoPP::Exception;
#include <hex.h>
using CryptoPP::HexEncoder;
using CryptoPP::HexDecoder;
#include <filters.h>
using CryptoPP::StringSink;
//#include <stdlib.h>
#include <time.h>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
try
{
AutoSeededRandomPool rng;
byte salt[16*8];
rng.GenerateBlock(salt, 16*8);
byte password[] ="password";
size_t plen = strlen((const char*)password);
size_t slen = strlen((const char*)salt);
int c = 1;
byte derived[32];
CryptoPP::PKCS5_PBKDF2_HMAC<CryptoPP::SHA256> pbkdf2;
pbkdf2.DeriveKey(derived, sizeof(derived), 0, password, plen, salt, slen, c);
string result;
HexEncoder encoder(new StringSink(result));
encoder.Put(derived, sizeof(derived));
encoder.MessageEnd();
cout << "Derived: " << result << endl;
}
catch (const Exception& ex) {
cerr << ex.what() << endl;
}
return a.exec();
}

Crypto++ 5.6.3rc5 GenerateBlock Not Implemented
...
You can read the history on the change at Crash in RandomNumberGenerator::GenerateWord32 due to stack recursion. The change was eventually backed out.
It was fixed in RC6, but it has not been announced yet. There's a quasi-pre-RC6 at Crypto++ 5.6.3 Files. But as soon as it is announced, then its set in stone and will not be changed.
Right now, RC6 is undergoing minor changes due to Cygwin, MinGW and C++11 on Debian Unstable. The changes are not too bad, but testing them is painful. Some of the scripts take half a day to run under emulated platforms, like S/390x.
If you want to side step the issue and avoid the download of pre-RC6, then you can use one of the following generators. They call GenerateIntoBufferedTransformation:
AutoSeededX917RNG< AES >
X917RNG
RandomPool
Or, you can use OS_GenerateRandomBlock to draw directly from the OS's pool.
Or, you can remove the code that throws. Open cryptlib.h, find RandomNumberGenerator, remove the #if 0/#endif guarding the old code and delete the throw.
Also see RandomNumberGenerator on the Crypto++ wiki.

Related

readdir hangs up if libpocofoundation is linked with my tiny app

I've started debbuging on some app, which hangs up in a loop based on readdir call.
Step by step I've cut everything but problem code, this is it:
So, in basic, it shows name of first entry and nothing more. It even does not exits, just waiting for something.
Also, I've found, that if don't lin it against libpocofoundation, it works.
But I have to do it because it used in the original app.
I'm a little bit confused, I don't use Poco in this example in any way, but it some way hangs it.
Please help me, I'm in panic :D
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <cstring>
#include <string>
#include <fcntl.h>
using namespace std;
int main(int argc, char *argv[])
{
const char TMP_DIR[] = "/opt";
DIR *dir = opendir(TMP_DIR);
std::cerr
<< readdir(dir)->d_name
<< readdir(dir)->d_name
<< std::endl;
return 0;
}
So... I don't know why it was happening. So I just dropped libpoco.

Mongodb c++ regex query

How to query MongoDB database using regex in c++.
mongo-cxx-driver-r3.1.1
hear is include
#include <cstdlib>
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
#include <cstdint>
#include <vector>
#include <mongocxx/stdx.hpp>
#include <bson.h>
#include <conio.h>
#include <sstream>
#include <stdio.h>
#include <string>
#include <bsoncxx/types.hpp>
#include <mongocxx/exception/exception.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
here is what I have tried.
void MyClass::on_querybtn_clicked()
{
auto collection = conn["TestDB"]["fdevices"];
bsoncxx::types::b_regex::b_regex();//i am lost here dont know how to use it
auto cursor = collection.find({});
for (auto doc : cursor) {
QString qstr = QString::fromStdString(bsoncxx::to_json(doc));
QJsonDocument docum = QJsonDocument::fromJson(qstr.toUtf8());
QJsonObject object = docum.object();
QString call = object["Data"].toString();
ui.mqttList->addItem(call);
}
}
Previously I build a software using Java now I am trying to build same software in Qt c++, I am new to c++.
Here is query code that I used in java for Query.
DBObject query = new BasicDBObject();
Pattern regex = Pattern.compile("^14-09-2017");
query.put("Data", regex);
here is how my data look like.
Database image
Use some builders and make the document, providing the string input for the pattern:
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
auto cursor = collection.find(make_document(
kvp("Data", bsoncxx::types::b_regex{"^14-09-2017"})
));
A bit easier to read than stream builder IMHO, but the basic premise is there. Still a string for input, much like Pattern.compile()
I've tried several approaches but finally I came up with the most straightforward one. I build a query as a json and then convert is to bson using bsoncxx::from_json function.
a working piece of code looks like:
mongocxx::instance instance{};
mongocxx::uri uri("mongodb://localhost:27017");
mongocxx::client *m_c = new mongocxx::client(uri);
std::string query = "{\"name.firstName\": {\"$options\": \"i\",\"$regex\": \"^pattern\"}}";
bsoncxx::builder::stream::document doc;
//this one to define q not inside try catch block.
bsoncxx::document::value q(doc.view());
try{
q = bsoncxx::from_json(query);
}catch (bsoncxx::exception &e){
std::cerr<<"error: "<<e.code()<<" "<<e.what();
return;
}
mongocxx::options::find opt;
mongocxx::cursor cursor = m_c->database("db_name").collection("collection_name").find( std::move(q), opt);
An important thing here is that I could not use q as bsoncxx::document::view.
Because a value for this view is created inside try - catch block, till the time
when the control flow came to the find function bsoncxx::document::view q was always empty. So I had to use bsoncxx::document::value q and move semantics in the find function.
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
auto cursor = collection.find(make_document(kvp("field",make_document(kvp("$regex":"pattern"),kvp("$options","i"))));
Refer To Official

include <iostream> issue the application unable to start correctly (0xc000007b)

When I started to use VS2013, I created just very basic application like this.
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Hello!";
return 0;
}
It crashed and when I commented out the #include <iostream> its no longer crash. I did several research on this error but nothing is suitable for my situation. This is the error :
Thanks for all your helps.
Once you create a new project, if you create it as an empty project I don't think you will face this issue. Then, you start it from scratch and you use int main() instead of that _tmain(...) and DO NOT EVER use using namespace std;
start a new EMPTY project and use something like this:
#include <iostream>
int main()
{
std::cout << "Hello World";
return 0;
}

Ownership issue with Clang frontend API

I'm using the Clang C++ API. As the API does not use smart pointers correctly, I've struggled with ownership. I stomped on all the issues I found so far on my own, but this one is vexing me. When the code executes, I get an access violation. I'm fairly certain it's a double delete, but since the documentation is nonexistent, I've got little idea where to look. Fortunately, the reproducing program is rather short. Any suggestions?
#define _SCL_SECURE_NO_WARNINGS
#pragma warning(push, 0)
#include <clang/CodeGen/CodeGenAction.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Sema/Lookup.h>
#include <clang/Lex/Preprocessor.h>
#include <clang/AST/ASTContext.h>
#include <clang/AST/Mangle.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Basic/TargetInfo.h>
#include <clang/Sema/Sema.h>
#include <clang/Sema/SemaConsumer.h>
#include <clang/Sema/CodeCompleteConsumer.h>
#include <llvm/LLVMContext.h>
#include <llvm/Support/DataTypes.h>
#include <llvm/Module.h>
#include <llvm/Support/Host.h>
#pragma warning(pop)
#include <string>
#include <iostream>
int main(int argc, char* argv[])
{
llvm::LLVMContext c;
llvm::Module m("", c);
clang::EmitLLVMOnlyAction emit(&c);
emit.setLinkModule(&m);
clang::CompilerInstance CI;
std::string errors;
llvm::raw_string_ostream error_stream(errors);
clang::DiagnosticOptions diagopts;
clang::TextDiagnosticPrinter printer(error_stream, &diagopts);
llvm::IntrusiveRefCntPtr<clang::DiagnosticIDs> diagids(new clang::DiagnosticIDs);
clang::DiagnosticsEngine engine(diagids, &diagopts, &printer, false);
CI.setDiagnostics(&engine);
CI.createFileManager();
CI.createSourceManager(CI.getFileManager());
llvm::raw_null_ostream empty;
clang::PrintingCodeCompleteConsumer print(CodeCompleteOptions(), empty);
clang::TargetOptions target;
target.Triple = llvm::sys::getDefaultTargetTriple();
CI.setTarget(clang::TargetInfo::CreateTargetInfo(engine, &target));
CI.createPreprocessor();
CI.createASTContext();
clang::SemaConsumer* cons = new clang::SemaConsumer();
CI.setASTConsumer(cons);
CI.createSema(clang::TranslationUnitKind::TU_Complete, &print);
cons->InitializeSema(CI.getSema());
clang::FrontendInputFile f("header", clang::InputKind::IK_CXX, true);
emit.BeginSourceFile(CI, f);
emit.Execute();
emit.EndSourceFile();
emit.takeModule();
clang::LookupResult lr(CI.getSema(), clang::DeclarationName(CI.getPreprocessor().getIdentifierInfo("function")), clang::SourceLocation(), clang::Sema::LookupNameKind::LookupOrdinaryName);
auto result = CI.getSema().LookupName(lr, CI.getSema().TUScope);
std::string temp;
llvm::raw_string_ostream out(temp);
CI.getASTContext().createMangleContext()->mangleName(lr.getFoundDecl(), out);
auto fun = m.getFunction(temp);
std::cout << fun->getName().str();
return 0;
}
Edit: Also, did I mention that if I change it so that the file actually exists, even if it's a trivial program, Clang fails with an access violation whilst executing the action, even before the previous one in EndSourceFile. Whyyyyyy.
I eventually solved this by skipping the entire Frontend, as that's mostly where all of the dodgy ownership lies, and simply calling the functions I need myself.

MinGW portability

I'm completely new to C++, but I have created a minor program, looking to port the program to other computers, but when I "install" the program I get this error...-static-libgcc -static-libstdc++ missing, is there a file I should be including in the program itself, or is this a library I have to install on each computer? The computers that I expect to run the program will be windows xp. Source code of the file is as follows:
#include <stdlib.h>
#include <windows.h>
#include <direct.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
int main(int argc, const char *argv[])
{
_chdir("C:\\Program Files\\NCHSoftware\\Talk\\");
string number = "start talk.exe -dial " + std::string(argv[1]+4);
system(number.c_str());
exit;
return 0;
}
They are shared lib's that would need to be on the host computer.
To learn how to compile a static version;
See here: http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
Read the "-static-libgcc" & "-static-libstdc++" sections.