I want to make an application in which any asset/image's rgb information is copied on a text file and then he can use that text file on my other application which reads rgb data of all pixels and construct a new copy of that particular asset. It reduces chances of asset alteration or simply makes it hard for others to change actual asset (according to me atleast).
To be fair though, it's not that hard to write a program to construct a bitmap from R,G,B,R,G,B, etc.
It's harder if you perform a basic encryption and decryption of your assets because then people have to examine the binary of your program to figure out the key and how to decrypt the assets. Plus encryption works on any asset type - not just rgb images.
You could calculate a sha1 checksum of your resources and use that for verifying them.
If you're so concerned about people not being able to use modified assets with that second application (I'm assuming the first app is a private one only you have access to and the second one is the public application that everyone else uses) your best bet is implementing a digital signature system:
Your application generates a digital signature for your assets, using a private key.
Your other application checks that the signature is correct when it loads the assets. People won't be able to modify your assets without knowing the private key, as they won't be able to generate a valid digital signature.
This is very easy to do by using any cryptography lib.
1) LennyLen, I'm using Allegro 5.2.0.
2) jmasterx, i know about that, but I'm making this application for learning purpose only and yes i was gonna store rgb info in binary form too.
3) beoran, i don't know much about sha1, i know it is some form of hashing (i guess,).Can you pls elaborate it?
4) Oscar Giner, as i said earlier I'm making it for information purpose only. But i would like to know how can i give my assets a digital signature. Is it done using allegro?
Thanks in advance.
You can use the sha2 functions from AllegroFlare if you're interested:
Test file: sha2_test.cpp
4) Oscar Giner, as i said earlier I'm making it for information purpose only. But i would like to know how can i give my assets a digital signature. Is it done using allegro?
1. Generate a pair of public and private keys for a public-key cryptography algorithm (like RSA). Your first app will use the private key to generate the signature and your other app will use the public key to verify the signature.
1. Generate a hash of the data (this is for performance reasons not for security, so just use the fastest hashing algorithm you can get).
2. Encrypt the hash with the private key. This is the signature.
3. When the other program wants to read the data you generate again the hash (using the exact same algorithm, both programs must generate the same hash for the same data) and decrypt the signature with the public key (remember, the signature is just the hash encrypted). The computed hash and the decrypted signature must be identical, otherwise the data has been modified.
Since no one knows the private key but you, if anyone tries to modify the data they won't be able to generate the signature (encrypted hash). Verifying the signature only requires the public key, so anyone can do that.
Oscar Giner, i understand what you say but how to actually implement it in a code? Can you provide a pseudo code or something like that?
Also i want to learn how to store multiple value in a single key and read em back or just store values in a section without any keys and read em back. For example,
Or like this,
I want to know how to write this type of config file and also how to read from this files and get any desired value.
Thanks in advance.
If you're only interested in validating that the content hasn't been tampered with, a simple hashing function is the easiest approach. A public/private key isn't necessary in this case because you aren't needing to prove the identity of the owner.
The easiest way to generate a hash of an image file is to use the get_sha256_hash_of_file() function from AllegroFlare. Simply pass the filename of the image to obtain the string.
std::string image_hash = get_sha256_hash_of_file("bin/data/doggie.jpg"); std::cout << image_hash << std::endl; // will print 479d98072585c81c26019899c4b7161f6a844ed5ae9d707b656b7767cc5fb9aa
You can then save the output of this string in your application as a constant.
#define DOGGIE_IMAGE_HASH "479d98072585c81c26019899c4b7161f6a844ed5ae9d707b656b7767cc5fb9aa"
When you want to load the image in your application, you can check that the generated hash is identical to the expected hash:
void validate_doggie_image(std::string filename="bin/data/doggie.jpg") { std::string image_hash = get_sha256_hash_of_file(filename); if (image_hash != DOGGIE_IMAGE_HASH) raise std::runtime_error("Doggie image invalid"); }
Mark Oates, your answer for hash function came in handy to me. Thanks for that. But my original question still remains unanswered, how do i store rgb values of a single pixel of an image to a text file or config file(pls)? To quote again I'm using allegro 5.2.2 with code blocks compiler on windows 7. And secondly i want to know how to store them in a specific manner in a config file in the way i told in my last reply.
Are you asking about basic file input/output or are you asking about accessing the pixel data? LennyLen already demonstrated writing the file in the second post (first you need to fopen the filename). One option to read it back in is using fscanf to read it as a formatted string in the reverse of fprintf. Those are C functions.
If you want a C++ solution then you'd use a std::ofstream and std::ifstream to write and read the file similarly to how you write data to the standard output (`std::cout`) (console) stream.
You can learn loads of information about writing and reading files in C or C++ with Google. We'd be happy to help you understand it if you have trouble.
Update: Fixed C++ class names.
how do i store rgb values of a single pixel of an image to a text file or config file(pls)?
Well, there's two parts to that. First, you need to obtain the pixel color data from the image, and next, write it to the file.
To obtain the color from an image, you'll use al_get_pixel to grab individual color components at a coordinate.
Note that you could also get the alpha color value if you use al_map_rgba, or you can get the values as floats rather than unsigned chars with the al_map_rgba_f, and al_map_rgb_f variations.
If you're using C++, writing data to a file is relatively simple.
There are a lot of optimizations you could make with these functions - for example, if you wanted to store multiple color values to a file you might fold get_image_color_at() and write_color_to_file() together (and possibly add a loop). Ideally, you should only open an ALLEGRO_BITMAP once.
Sorry late reply guys. Mark Oates, let me make self very very clear now, the function in allegro 5 lib "al_set_config_value()" takes a const char* as it's last parameter which is for the value to be written to a configuration file. The function "al_unmap_rgb()" takes unsigned char for the last 3 parameters, which are red, green and blue value of a pixel. Now my compiler "code::blocks v16.01" gives an error in the function that "it cannot convert unsigned char to const char*". Now how to solve that one. That's the problem I've been facing since LennyLen and Arthur Kalliokoski first replied. Please me with this problem.
"it cannot convert unsigned char to const char*"
It's asking for a char pointer, which is not a char, but a pointer to a char.
What al_unmap_rgb() returns are certainly chars, but it would be better to think of them as bytes, rather than chars, which seems to hint that they're printable characters.
What you need is to "print" the values of those unsigned chars to a char buffer, then point your al_set_config_value() at that buffer.
Now I can't get the above to compile because it doesn't know the size of ALLEGRO_CONFIG or something, but it should get you closer to what you want.
The best way to get help with this kind of problem is to post the exact error message text and ideally the code (or a shortened subset of the code) suffering from the error so that we don't have to go out of our way to invent a program that's nothing like what you're doing.
Looks like there's an example program (ex_config) that demonstrates it, and the source for that is available here by clicking on "ex_config":