示例#1
0
 /**
  * @covers Cougar\Util\Arc4::setMagic
  * @covers Cougar\Util\Arc4::useCompression
  * @covers Cougar\Util\Arc4::setKey
  * @covers Cougar\Util\Arc4::encode
  */
 public function testEncodeDecodeWithCompressionAndMagic()
 {
     Arc4::setKey(bin2hex("d8vsR7nnKmG4FwKgX82gzW58QtDWiZGnjVedrZsNnG"));
     Arc4::setMagic("Magic string");
     Arc4::useCompression(true);
     # Define a list of strings
     $strings = array("Hello", "Hello World!", "Attack at dawn", "The quick brown fox jumps over the lazy dog.");
     # Go through each string and encode/decode
     foreach ($strings as $string) {
         $encoded_string = Arc4::encode($string);
         $decoded_string = Arc4::decode($encoded_string);
         $this->assertEquals($string, $decoded_string);
     }
 }
示例#2
0
 /**
  * Creates a new connection file with the parameters given. The username
  * and password will encoded either in ARC4 or via obfuscation. The key and
  * other pertinent values must be set up beforehand.
  *
  * @history
  * 2013.09.30:
  *   (AT)  Initial release
  * 2014.03.10:
  *   (AT)  Add support for persistent connections
  *
  * @version 2014.03.10
  * @author (AT) Alberto Trevino, Brigham Young Univ. <*****@*****.**>
  *
  * @param string $name
  *   The name of the database connection
  * @param string $environment
  *   Connection environment
  * @param string $dsn
  *   PDO DSN connection string
  * @param string $username
  *   Username
  * @param string $password
  *   Password
  * @param string $encoding
  *   Encoding to use (Arc4 or Obfuscation)
  * @param string $arc4_config_file
  *   Configuration file with ARC4 parameters
  * @param bool $persistent
  *   Whether to establish a persistent connection
  * @return string Filename of created file
  * @throws \Cougar\Exceptions\Exception
  */
 public static function createConnectionFile($name, $environment, $dsn, $username, $password, $encoding = "Arc4", $arc4_config_file = null, $persistent = false)
 {
     # Make sure we have a name
     if (!$name) {
         throw new Exception("Connection name is required");
     }
     # Come up with the filename
     if ($environment) {
         $filename = strtolower($name . "." . $environment . ".conf");
         $env = $environment;
     } else {
         $filename = strtolower($name . ".conf");
         $env = "(default)";
     }
     # Make sure we have a DSN
     if (!$dsn) {
         throw new Exception("PDO DSN is required");
     }
     # Encode the username and password
     switch ($encoding) {
         case "Arc4":
             $enc_username = Arc4::encode($username);
             $enc_password = Arc4::encode($password);
             break;
         case "Obfuscation":
             $enc_username = StringObfuscator::encode($username);
             $enc_password = StringObfuscator::encode($password);
             break;
         default:
             throw new Exception("Invalid encoding: " . $encoding);
     }
     # See if we had an ARC4 encryption file
     $arc4_config_line = "";
     if ($arc4_config_file) {
         $arc4_config_line = "arc4 = " . $arc4_config_file;
     }
     # See if the connection is persistent
     if ($persistent) {
         $persistence_string = "yes";
     } else {
         $persistence_string = "no";
     }
     # Create the contents of the file
     $file = "# Begin Database Connection File\n\n# Connection name: " . $name . "\n# Environment: " . $env . "\n\ndsn = " . $dsn . "\nusername = "******"\npassword = "******"\npersistent = " . $persistence_string . "\n" . $arc4_config_line . "\n\n# End Database connection File\n";
     # Write out the contents
     file_put_contents($filename, $file);
     # Return the filename
     return $filename;
 }