How to calculate the MD5 of a char* using C++ [duplicate] - c++

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
In C++, How to get MD5 hash of a file?
I am currently using Ubuntu and am wishing to calculate the MD5 of a char*. was wondering if there is a pre-installed library that would just need including, or would I have to download a specially designed one?

Include openssl/MD5.h and use the following to calculate the hash
MD5(<characters>, <length of it>, <the result(pointer)>);

Have a look at hashlib++ or Crypto API.

I would rephrase the question. In the context of C++, you're asking for the MD5 sum of a single pointer to char, which is practically meaningless.
That 'char *' could refer to a location in memory that refers to the file content you are after, in which case you're going to need a size somewhere, or it could refer to a null-terminated string, or a pascal-string, or, really, anything else.
With ubuntu, I'd do something like 'apt-cache search md5' and see what you get. On my debian system, libgcrypt11 looks intriguing.

Related

How do I get current file path in c++? [duplicate]

This question already has answers here:
How to get current source path in C++ - Linux
(2 answers)
Closed 2 years ago.
I want get current file path,like in a.cpp,I can get
/home/workspace/src/a.cpp
How can I get this path?
You can use the standard macro __FILE__ to which expands to a string literal that contains the path of the current source file.
Starting from C++20, you can use default constructed std::source_location.
I couldn't comment due to the reputation requirement.
But you might find this useful, a way to retrieve directory on Linux & Windows.
How do I get the directory that a program is running from?
Hope this help!

Is it possible to decompile a C++ executable file [duplicate]

This question already has answers here:
Is it possible to "decompile" a Windows .exe? Or at least view the Assembly?
(16 answers)
Is there a C++ decompiler? [closed]
(5 answers)
Closed 4 years ago.
I lost the source code to an executable file but still have the actual file. Is there any way to retrieve the original C++ code?
Duplicate of this question here.
Yes, it is possible, however when it comes to peeking function bodies and the like, you might have a little less luck. Operating systems like Kali Linux specialize in de-compilation and reverse engineering, so maybe look into a VM of that. And of course, windows has a lot of applications you can use as well to check the application code.
Look over the other question for specific app suggestions. :)
Edit : You will most likely have lost all your logic and function bodies, but you might be able to recover the overall structure. It's your EXE so you might be more familiar with how it was all connected up.
You cannot get the original source code but you can decompile the binary into source code using tools given in this similar question: Is there a C++ decompiler?
The output source code will not look like the original as the compiler will have optimised the original source when generating the executable.
Short answer NO.
Long answer, because C++ doesn't use some intermediate code like C# or Java you cannot decompile the app in some readable format. But if you can read assembly maybe you can save some time.

C++ - Create function at runtime [duplicate]

This question already has answers here:
Is it possible to create a function dynamically, during runtime in C++?
(14 answers)
Closed 8 years ago.
In C++, I have to make some user-defined actions on each cells of a big table.
Because of the size of the table, I'd like not to use interpreted instructions but to compile during runtime a function that I will call on each cell.
The user-defined actions are pretty simple :
if ((state1 && state2) || state3) then change_a_value_in_memory
That's why I don't need to use the LLVM or other JIT libraries.
I hesitate to just use mmap and add the code in hex directly.
I'd like to know if it exists better solutions, or, if not, where I can find the basic format of a C++ function code to directly write it in memory.
Thanks, and sorry for my english :/
This is not the most elegant, but it works always: generate a file with the C++ code of your function, call the compiler using system(), load the generated .so file using dlopen, and use the function!
This will take some time (due to the compiler call), but if you keep the function and maintain a database of the functions you already have, then you can save the amount of compilations.

How to generate code using a loop in c++? [duplicate]

This question already has answers here:
How can I convert string to code at run time
(4 answers)
Closed 9 years ago.
Is it possible to convert a raw string to code in c++?
For example I have a string
string s = "cout << "Hello World";";
I want to generate that code programatically. Maybe i want to insert that code in various parts of the program dynamically.
cout << "Hello World";
Also, is it possible to generate code using loops in c++ maybe using pre-processor directives?
It looks like you want something akin to the eval function provided by Lisp, Python, perl, and many other languages.
There is no such capability in C++. It just doesn't exist, by design.
One way to get around this missing functionality is to make your program write a program, store it in a file, invoke the compiler, and invoke the executable generated by the compiler. That's rather ugly. Anything you do is going to be rather ugly.
Is it possible to convert a raw string to code in c++?
C++ is typically a compiled language. This makes what you're trying to do tricky. In order to achieve what you're saying, you'd need to either have the code interpreted at runtime (in which case you'd be better served by binding to a scripting language such as Python or Lua), or embed or call a C++ compiler and pass the code to that, then run it.
It is possible that this is a XY problem and that the solution you are looking at is overly complicated for the actual root problem you are trying to solve. It may be an idea to put up a description of why you're trying to do this, as there may be a much simpler way to achieve the end result you want.

Reading from set of files [duplicate]

This question already has answers here:
How can I get the list of files in a directory using C or C++?
(33 answers)
Closed 9 years ago.
I am a newbie so please don't be harsh on me for asking simple questions. my main question is how can I read from several files located in a directory sequently using a loop in c++ and perform some actions on them?
My code is like this:
string corpus = "corpus.txt";
myfile.open(corpus);
if (myfile.is_open())
while (!myfile.eof())
{
//Do something
}//end of while
MergeFiles(corpus,count);`
How can I do this actions on a set of files instead of just one.
Looks like you could do one of the following:
use windows APIs
use third party code
It looks like the simplest way is using diren.h. Look for at the sample code here:
How can I get the list of files in a directory using C or C++?
you can use the boost libraries, more specific - the filesystem library. This is more powerful but complicated solution, requires you to use iterators and higher programming methods.
using the windows API is not recommended because it requires dipper understanding of windows, and won't result a portable code. If you want to use it, there is an example in MSDN.