15 #include "encrypt_string.h" 18 #include "encryptStream.h" 19 #include "virtualFileSystem.h" 20 #include "config_express.h" 30 encrypt_string(
const string &source,
const string &password,
31 const string &algorithm,
int key_length,
int iteration_count) {
35 OEncryptStream encrypt;
36 if (!algorithm.empty()) {
37 encrypt.set_algorithm(algorithm);
40 encrypt.set_key_length(key_length);
42 if (iteration_count >= 0) {
43 encrypt.set_iteration_count(iteration_count);
45 encrypt.open(&dest,
false, password);
46 encrypt.write(source.data(), source.length());
69 decrypt_string(
const string &source,
const string &password) {
73 if (!decrypt_stream(source_stream, dest_stream, password)) {
77 return dest_stream.str();
89 EXPCL_PANDAEXPRESS
bool 90 encrypt_file(
const Filename &source,
const Filename &dest,
const string &password,
91 const string &algorithm,
int key_length,
int iteration_count) {
98 istream *source_stream = vfs->
open_read_file(source_filename,
true);
99 if (source_stream == NULL) {
100 express_cat.info() <<
"Couldn't open file " << source_filename <<
"\n";
104 Filename dest_filename = Filename::binary_filename(dest);
105 ostream *dest_stream = vfs->
open_write_file(dest_filename,
true,
true);
106 if (dest_stream == NULL) {
107 express_cat.info() <<
"Couldn't open file " << dest_filename <<
"\n";
112 bool result = encrypt_stream(*source_stream, *dest_stream, password,
113 algorithm, key_length, iteration_count);
133 EXPCL_PANDAEXPRESS
bool 134 decrypt_file(
const Filename &source,
const Filename &dest,
const string &password) {
135 Filename source_filename = Filename::binary_filename(source);
137 istream *source_stream = vfs->
open_read_file(source_filename,
false);
138 if (source_stream == NULL) {
139 express_cat.info() <<
"Couldn't open file " << source_filename <<
"\n";
148 ostream *dest_stream = vfs->
open_write_file(dest_filename,
true,
true);
149 if (dest_stream == NULL) {
150 express_cat.info() <<
"Couldn't open file " << dest_filename <<
"\n";
155 bool result = decrypt_stream(*source_stream, *dest_stream, password);
172 encrypt_stream(istream &source, ostream &dest,
const string &password,
173 const string &algorithm,
int key_length,
int iteration_count) {
174 OEncryptStream encrypt;
175 if (!algorithm.empty()) {
176 encrypt.set_algorithm(algorithm);
178 if (key_length > 0) {
179 encrypt.set_key_length(key_length);
181 if (iteration_count >= 0) {
182 encrypt.set_iteration_count(iteration_count);
184 encrypt.open(&dest,
false, password);
186 static const size_t buffer_size = 4096;
187 char buffer[buffer_size];
189 source.read(buffer, buffer_size);
190 size_t count = source.gcount();
192 encrypt.write(buffer, count);
193 source.read(buffer, buffer_size);
194 count = source.gcount();
198 return (!source.fail() || source.eof()) && (!encrypt.fail());
217 decrypt_stream(istream &source, ostream &dest,
const string &password) {
218 IDecryptStream decrypt(&source,
false, password);
220 static const size_t buffer_size = 4096;
221 char buffer[buffer_size];
223 decrypt.read(buffer, buffer_size);
224 size_t count = decrypt.gcount();
226 dest.write(buffer, count);
227 decrypt.read(buffer, buffer_size);
228 count = decrypt.gcount();
231 return (!decrypt.fail() || decrypt.eof()) && (!dest.fail());
234 #endif // HAVE_OPENSSL A hierarchy of directories and files that appears to be one continuous file system, even though the files may originate from several different sources that may not be related to the actual OS's file system.
istream * open_read_file(const Filename &filename, bool auto_unwrap) const
Convenience function; returns a newly allocated istream if the file exists and can be read...
void set_binary()
Indicates that the filename represents a binary file.
static void close_read_file(istream *stream)
Closes a file opened by a previous call to open_read_file().
The name of a file, such as a texture file or an Egg file.
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
ostream * open_write_file(const Filename &filename, bool auto_wrap, bool truncate)
Convenience function; returns a newly allocated ostream if the file exists and can be written...
static void close_write_file(ostream *stream)
Closes a file opened by a previous call to open_write_file().
bool is_binary_or_text() const
Returns true either is_binary() or is_text() is true; that is, that the filename has been specified a...