22 using std::istringstream;
24 using std::ostringstream;
32 encrypt_string(
const string &source,
const string &password,
33 const string &algorithm,
int key_length,
int iteration_count) {
37 OEncryptStream encrypt;
38 if (!algorithm.empty()) {
39 encrypt.set_algorithm(algorithm);
42 encrypt.set_key_length(key_length);
44 if (iteration_count >= 0) {
45 encrypt.set_iteration_count(iteration_count);
47 encrypt.open(&dest,
false, password);
48 encrypt.write(source.data(), source.length());
67 decrypt_string(
const string &source,
const string &password) {
68 istringstream source_stream(source);
69 ostringstream dest_stream;
71 if (!decrypt_stream(source_stream, dest_stream, password)) {
75 return dest_stream.str();
84 EXPCL_PANDA_EXPRESS
bool
85 encrypt_file(
const Filename &source,
const Filename &dest,
const string &password,
86 const string &algorithm,
int key_length,
int iteration_count) {
93 istream *source_stream = vfs->
open_read_file(source_filename,
true);
94 if (source_stream ==
nullptr) {
95 express_cat.info() <<
"Couldn't open file " << source_filename <<
"\n";
99 Filename dest_filename = Filename::binary_filename(dest);
100 ostream *dest_stream = vfs->
open_write_file(dest_filename,
true,
true);
101 if (dest_stream ==
nullptr) {
102 express_cat.info() <<
"Couldn't open file " << dest_filename <<
"\n";
107 bool result = encrypt_stream(*source_stream, *dest_stream, password,
108 algorithm, key_length, iteration_count);
124 EXPCL_PANDA_EXPRESS
bool
125 decrypt_file(
const Filename &source,
const Filename &dest,
const string &password) {
126 Filename source_filename = Filename::binary_filename(source);
128 istream *source_stream = vfs->
open_read_file(source_filename,
false);
129 if (source_stream ==
nullptr) {
130 express_cat.info() <<
"Couldn't open file " << source_filename <<
"\n";
139 ostream *dest_stream = vfs->
open_write_file(dest_filename,
true,
true);
140 if (dest_stream ==
nullptr) {
141 express_cat.info() <<
"Couldn't open file " << dest_filename <<
"\n";
146 bool result = decrypt_stream(*source_stream, *dest_stream, password);
159 encrypt_stream(istream &source, ostream &dest,
const string &password,
160 const string &algorithm,
int key_length,
int iteration_count) {
161 OEncryptStream encrypt;
162 if (!algorithm.empty()) {
163 encrypt.set_algorithm(algorithm);
165 if (key_length > 0) {
166 encrypt.set_key_length(key_length);
168 if (iteration_count >= 0) {
169 encrypt.set_iteration_count(iteration_count);
171 encrypt.open(&dest,
false, password);
173 static const size_t buffer_size = 4096;
174 char buffer[buffer_size];
176 source.read(buffer, buffer_size);
177 size_t count = source.gcount();
179 encrypt.write(buffer, count);
180 source.read(buffer, buffer_size);
181 count = source.gcount();
185 return (!source.fail() || source.eof()) && (!encrypt.fail());
199 decrypt_stream(istream &source, ostream &dest,
const string &password) {
200 IDecryptStream decrypt(&source,
false, password);
202 static const size_t buffer_size = 4096;
203 char buffer[buffer_size];
205 decrypt.read(buffer, buffer_size);
206 size_t count = decrypt.gcount();
208 dest.write(buffer, count);
209 decrypt.read(buffer, buffer_size);
210 count = decrypt.gcount();
213 return (!decrypt.fail() || decrypt.eof()) && (!dest.fail());
The name of a file, such as a texture file or an Egg file.
void set_binary()
Indicates that the filename represents a binary 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...
A hierarchy of directories and files that appears to be one continuous file system,...
static void close_write_file(std::ostream *stream)
Closes a file opened by a previous call to open_write_file().
std::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_read_file(std::istream *stream)
Closes a file opened by a previous call to open_read_file().
std::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,...
static VirtualFileSystem * get_global_ptr()
Returns the default global VirtualFileSystem.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.