t_secret_squirrel.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_secret_string                                                *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1996-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include <basis/chaos.h>
00016 #include <basis/guards.h>
00017 #include <basis/istring.h>
00018 #include <crypto/secret_string.h>
00019 #include <data_struct/static_memory_gremlin.h>
00020 #include <loggers/file_logger.h>
00021 #include <opsystem/filename.h>
00022 #include <opsystem/path_configuration.h>
00023 #include <data_struct/static_memory_gremlin.h>
00024 #include <textual/byte_format.h>
00025 #include <textual/string_manipulation.h>
00026 
00027 HOOPLE_STARTUP_CODE;
00028 
00029 #define LOG(s) program_wide_logger().log(s)
00030 
00031 const int TEST_ITERATIONS = 100000;
00032 
00033 // define the specific keys that we use.
00034 byte TEST_KEY_1[] = {
00035   0x28, 0x94, 0x32, 0x99, 0xFE, 0x2B, 0x9F, 0x75,
00036   0x47, 0x26, 0xC8, 0x37, 0xED, 0x59, 0x8E, 0x1B
00037 };
00038 
00039 byte TEST_KEY_2[] = {
00040   0x92, 0x18, 0x2C, 0xF8, 0xCE, 0xEE, 0x34, 0xD0,
00041   0x91, 0x23, 0x85, 0x5C, 0xBB, 0x7A, 0x98, 0x85,
00042   0x12, 0x02, 0xF0, 0xE8, 0x14, 0x83, 0x9E, 0x3D,
00043   0x38, 0x23, 0xB0, 0x9D, 0x7D, 0x3D, 0x92, 0x94
00044 };
00045 
00046 istring get_base(const istring &to_strip)
00047 {
00048   filename ted = filename(to_strip);
00049   return ted.basename().raw();
00050 }
00051 
00052 SAFE_STATIC(istring_object, APP_NAME,
00053     (get_base(portable::application_name())) )
00054 
00055 int main(int formal(argc), char *formal(argv)[])
00056 {
00057   SET_DEFAULT_COMBO_LOGGER;
00058 
00059   {
00060     // test group 1: test using a particular key.
00061     #define GROUP "test group 1: specific key"
00062 
00063     byte_array the_key(sizeof(TEST_KEY_1), TEST_KEY_1);
00064     secret_string encryptor(the_key);
00065 
00066     istring to_encrypt_1("I solely forged the permaderm butter arch, man.");
00067     istring enco = encryptor.encrypt_string(to_encrypt_1);
00068     LOG("test 1: encoded form...");
00069     LOG(enco);
00070     byte_array just_bytes;
00071     byte_format::string_to_bytes(enco, just_bytes);
00072     just_bytes += byte(0);
00073     LOG("test 1: encrypted shown as text...");
00074     LOG((char *)just_bytes.observe());
00075 
00076     istring deco = encryptor.decrypt_string(enco);
00077     if (deco != to_encrypt_1)
00078       deadly_error(APP_NAME(), GROUP, "the decrypted data is erroneous");
00079     #undef GROUP
00080   }
00081 
00082   {
00083     // test group 2: using a longer key.
00084     #define GROUP "test group 2: longer key"
00085 
00086     byte_array the_key(sizeof(TEST_KEY_2), TEST_KEY_2);
00087     secret_string encryptor(the_key);
00088 
00089     istring to_encrypt_2("Jubel toasted a smarmadillo over the open fame.");
00090     istring enco = encryptor.encrypt_string(to_encrypt_2);
00091     LOG("test 2: encoded form...");
00092     LOG(enco);
00093     byte_array just_bytes;
00094     byte_format::string_to_bytes(enco, just_bytes);
00095     just_bytes += byte(0);
00096     LOG("test 2: encrypted shown as text...");
00097     LOG((char *)just_bytes.observe());
00098 
00099     istring deco = encryptor.decrypt_string(enco);
00100     if (deco != to_encrypt_2) 
00101       deadly_error(APP_NAME(), GROUP, "the decrypted data is erroneous");
00102     #undef GROUP
00103   }
00104   {
00105     // test group 3: random keys and random encodings.
00106     #define GROUP "test group 3: random keys"
00107 
00108     LOG(istring("starting--") + GROUP);
00109 
00110     chaos randomizer;
00111 
00112     for (int outer = 0; outer < TEST_ITERATIONS; outer++) {
00113       // pick a random key size.
00114       int key_size = randomizer.inclusive(1, 12);
00115 
00116       // pick a random key.
00117       byte_array the_key(key_size * 8);
00118       int i;  // loop variable used in multiple loops.
00119       for (i = 0; i < the_key.length(); i++)
00120         the_key[i] = byte(randomizer.inclusive(0, 255));
00121 
00122       secret_string encryptor(the_key);
00123 
00124       // pick something to encode.
00125       istring jughead = string_manipulation::make_random_name(100, 1000);
00126 
00127       // now encode it.
00128       istring enco = encryptor.encrypt_string(jughead);
00129 
00130       // now, with little ado, decode it.
00131       istring deco = encryptor.decrypt_string(enco);
00132 
00133       if (deco != jughead)
00134         deadly_error(APP_NAME(), GROUP, "the decrypted data is erroneous");
00135       #undef GROUP
00136     }
00137   }
00138 
00139   istring to_print("secret_string:: works for those functions tested.");
00140   guards::alert_message(to_print.s());
00141 
00142   return 0;
00143 }
00144 

Generated on Sat Oct 11 04:29:00 2008 for HOOPLE Libraries by  doxygen 1.5.1