17 #include "encrypt_string.h"
19 #include "panda_getopt.h"
20 #include "preprocess_argv.h"
23 bool got_password =
false;
25 bool got_algorithm =
false;
27 bool got_key_length =
false;
28 int iteration_count = -1;
29 bool got_iteration_count =
false;
35 <<
" pencrypt [opts] file [file2 file3 ...]\n"
36 <<
" pencrypt -o dest_file file\n\n"
38 <<
"This program will apply an encryption algorithm to a file (or multiple files),\n"
39 <<
"creating an encrypted version of each file which can only be recovered using\n"
40 <<
"pdecrypt and the same password that was supplied to pencrypt. The compressed\n"
41 <<
"versions are written to a file with the same name as the original, but the\n"
42 <<
"extension .pe added to the filename, and the original file is removed\n"
43 <<
"(unless the version with -o is used, in which case you can encrypt only one\n"
44 <<
"file, you specify the destination file name, and the original file is not\n"
48 <<
"Note that if you are adding files to a Panda multifile (.mf file) with\n"
49 <<
"the multify command, it is not necessary to encrypt them separately;\n"
50 <<
"multify has an inline encryption option.\n\n"
54 <<
" -p \"password\"\n"
55 <<
" Specifies the password to use for encryption. There are no\n"
56 <<
" restrictions on the password length or contents, but longer passwords\n"
57 <<
" are more secure. If this is not specified, the user is prompted from\n"
58 <<
" standard input.\n\n"
60 <<
" -t Read the file as a text file. This will automatically convert\n"
61 <<
" universal end-of-line characters into a newline character, ascii\n"
64 <<
" -a \"algorithm\"\n"
65 <<
" Specifies the particular encryption algorithm to use. The complete\n"
66 <<
" set of available algorithms is defined by the current version of\n"
67 <<
" OpenSSL. The default algorithm is taken from the encryption-\n"
68 <<
" algorithm config variable.\n\n"
71 <<
" Specifies the key length, in bits, for the selected encryption\n"
72 <<
" algorithm. This only makes sense for those algorithms that support\n"
73 <<
" a variable key length. The default value is taken from the\n"
74 <<
" encryption-key-length config variable.\n\n"
76 <<
" -i iteration_count\n"
77 <<
" Specifies the number of times the password is hashed to generate\n"
78 <<
" a key. The only purpose of this is to make it computationally\n"
79 <<
" more expensive for an attacker to search the key space exhaustively.\n"
80 <<
" This should be a multiple of 1,000 and should not exceed about 65\n"
81 <<
" million; the value 0 indicates just one application of the hashing\n"
82 <<
" algorithm. The default value is taken from the encryption-iteration-\n"
83 <<
" count config variable.\n\n";
87 main(
int argc,
char **argv) {
90 const char *optstr =
"o:p:ta:k:i:h";
93 bool got_dest_filename =
false;
94 bool text_file =
false;
96 preprocess_argv(argc, argv);
97 int flag = getopt(argc, argv, optstr);
103 got_dest_filename =
true;
117 got_algorithm =
true;
121 key_length = atoi(optarg);
122 got_key_length =
true;
126 iteration_count = atoi(optarg);
127 got_iteration_count =
true;
136 flag = getopt(argc, argv, optstr);
147 if (got_dest_filename && argc > 2) {
148 cerr <<
"Only one input file allowed in conjunction with -o.\n";
153 for (
int i = 1; i < argc; i++) {
156 cerr << source_file <<
" already ends .pe; skipping.\n";
159 if (!got_dest_filename) {
164 pifstream read_stream;
170 if (!source_file.
open_read(read_stream)) {
171 cerr <<
"Couldn't read: " << source_file << endl;
176 pofstream write_stream;
178 if (!dest_file.
open_write(write_stream,
true)) {
179 cerr <<
"Failed to open: " << dest_file << endl;
185 cerr <<
"Enter password: ";
186 getline(cin, password);
190 cerr << dest_file <<
"\n";
191 bool success = encrypt_stream(read_stream, write_stream, password,
192 algorithm, key_length, iteration_count);
195 write_stream.close();
198 cerr <<
"Failure writing " << dest_file <<
"\n";
203 if (!got_dest_filename) {
string get_fullpath() const
Returns the entire filename: directory, basename, extension.
bool unlink() const
Permanently deletes the file associated with the filename, if possible.
void set_binary()
Indicates that the filename represents a binary file.
void set_text()
Indicates that the filename represents a text file.
bool open_read(ifstream &stream) const
Opens the indicated ifstream for reading the file, if possible.
The name of a file, such as a texture file or an Egg file.
bool open_write(ofstream &stream, bool truncate=true) const
Opens the indicated ifstream for writing the file, if possible.
string get_extension() const
Returns the file extension.
static Filename from_os_specific(const string &os_specific, Type type=T_general)
This named constructor returns a Panda-style filename (that is, using forward slashes, and no drive letter) based on the supplied filename string that describes a filename in the local system conventions (for instance, on Windows, it may use backslashes or begin with a drive letter and a colon).