Updated pthreads libraries for the Windows platform (pthread4w) to version 3.0.0.
[slunkcrypt.git] / examples / example_encrypt.cpp
blob483dbbdd07edd78bd5d304a4ae5d58101251330b
1 /******************************************************************************/
2 /* SlunkCrypt, by LoRd_MuldeR <MuldeR2@GMX.de> */
3 /* This work has been released under the CC0 1.0 Universal license! */
4 /******************************************************************************/
6 //SlunkCrypt API
7 #include <slunkcrypt.hpp>
9 //CRT
10 #include <cstdlib>
11 #include <iostream>
12 #include <fstream>
14 //Example password
15 static std::string EXAMPLE_PASSWORD("cMRe5E5D)'!2?5]QDlCQ4tBb");
17 //Const
18 #define BUFF_SIZE 4096U
20 static int encrypt_main(int argc, char *argv[])
22 std::cerr << "SlunkCrypt encrypt sample [" << __DATE__ << "]" << std::endl;
23 std::cerr << "using libSlunkCrypt v" << SLUNKCRYPT_VERSION_MAJOR << '.' << SLUNKCRYPT_VERSION_MINOR << '.' << SLUNKCRYPT_VERSION_PATCH << '\n' << std::endl;
25 if (argc < 3)
27 std::cerr << "Usage:\n encrypt.exe <plaintext.txt> <ciphertext.enc>\n" << std::endl;
28 return -1;
31 // -----------------------------------------------------------
32 // Open input/output files
33 // -----------------------------------------------------------
35 std::ifstream file_src(argv[1], std::ios::binary);
36 if (!file_src.is_open())
38 std::cerr << "Error: Failed to open input file for reading!" << std::endl;
39 return -1;
42 std::ofstream file_dst(argv[2], std::ios::binary);
43 if (!file_dst.is_open())
45 std::cerr << "Error: Failed to open output file for writing!" << std::endl;
46 return -1;
49 // -----------------------------------------------------------
50 // Initialize the SlunkCryptEncr instance
51 // -----------------------------------------------------------
53 std::cerr << "Initializing key, please wait... " << std::flush;
55 uint8_t buffer[BUFF_SIZE];
56 slunkcrypt::Encryptor slunk_encrypt(EXAMPLE_PASSWORD);
58 std::cerr << "done.\nSlunk-encrypting the file contents, please wait... " << std::flush;
60 // -----------------------------------------------------------
61 // Encryption loop
62 // -----------------------------------------------------------
64 file_src.exceptions(std::ifstream::badbit);
65 file_dst.exceptions(std::ifstream::failbit | std::ifstream::badbit);
67 try
69 while (file_src.good())
71 file_src.read(reinterpret_cast<char*>(buffer), BUFF_SIZE);
72 const std::streamsize count = file_src.gcount();
73 if (count > 0)
75 if (!slunk_encrypt.inplace(buffer, (size_t)count))
77 std::cerr << "failed!\n\nError: SlunkCrypt encryption has failed!" << std::endl;
78 return -1;
80 file_dst.write(reinterpret_cast<char*>(buffer), count);
84 catch (std::ios_base::failure e)
86 std::cerr << "failed!\n\nI/O Error: \"" << e.code().message() << "\" [Code: " << e.code().value() << "]\n" << std::endl;
87 return -1;
90 // -----------------------------------------------------------
91 // Clean up
92 // -----------------------------------------------------------
94 std::cerr << "done.\n\nNonce: " << std::hex << std::uppercase << slunk_encrypt.get_nonce() << '\n' << std::endl;
96 file_src.close();
97 file_dst.close();
99 return 0;
102 int main(int argc, char *argv[])
106 return encrypt_main(argc, argv);
108 catch (std::exception e)
110 std::cerr << "\n\nException: \"" << e.what() << "\"\n" << std::endl;
111 return -1;