Loading ...
Sorry, an error occurred while loading the content.

comments please on these metabase mcypt functions

Expand Messages
  • charles_killian@yahoo.com
    Below are the mcrypt functions I wrote for use with metabase. They should be saved to a file called metabase_mcrypt.php and included along with the other
    Message 1 of 1 , Oct 1, 2000
    • 0 Attachment
      Below are the mcrypt functions I wrote for use with metabase. They
      should be saved to a file called metabase_mcrypt.php and included
      along with the other metabase pages.

      Issues: I don't know how to throw a Metabase error. There are two
      places below where this should happen. How do I do it?

      Please have a look at the code below:

      <?
      /*
      * metabase_mcrypt.php
      *
      * $Header: metabase_mcrypt.php,v 1.1 2000/10/02 12:17:37 charlie Exp
      $
      *
      * Created by Charles Killian (Charles@...) Oct
      10, 2000

      NOTES:
      To use the mcrpt functions make sure you include this file along
      with the other
      Metabase files.

      PHP >= 4.0.2 with libmcrypt-2.x.x required.

      MetabaseGetMcryptFieldValue() returns the data as an encrypted
      string (not binary)
      so set the database field type to text.

      In order to use these function you need to add these values to the
      Options
      array of MetabaseSetupDatabase():
      McryptKey, McryptMode, McryptCipher, McryptIV

      Example of MetabaseSetupDatabase:
      $error=MetabaseSetupDatabase(array(
      "Host"=>"localhost",
      "Type"=>"mysql",
      "User"=>"user_name",
      "Password"=>"password_here",
      "Options"=>array(
      "McryptKey"=>"Very, very, very secret key", // string. required to
      use mcrypt functions
      "McryptMode"=>MCRYPT_MODE_CFB, // constant. default is
      MCRYPT_MODE_ECB
      "McryptCipher"=>MCRYPT_TripleDES, // constant. default is
      MCRYPT_TripleDES
      "McryptIV"=>"wxecdrfv" // string. not required
      if mode is MCRYPT_MODE_ECB
      // read the php manual Mcrypt section before using the mcrypt
      functions!
      )
      ),&$database);

      Example of function use:
      $encrypted_data = MetabaseGetMcryptFieldValue($database,"data_here");
      $decrypted_data = MetabaseFetchMcryptResult
      ($database,$result,$row,$field);

      */
      Function MetabaseGetMcryptFieldValue($database,$value)
      {
      global $metabase_databases,$metabase_interfaces;

      $td = SetupMcrypt($database);
      $value = bin2hex(mcrypt_generic ($td, $value));//change returned
      encrypted binary to hex
      mcrypt_generic_end($td);
      return($metabase_interfaces[$metabase_databases[$database]-
      >interface]->GetTextFieldValue($value));
      }

      Function MetabaseFetchMcryptResult($database,$result,$row,$field)
      {
      global $metabase_databases,$metabase_interfaces;

      $td = SetupMcrypt($database);
      $result = $metabase_interfaces[$metabase_databases[$database]-
      >interface]->FetchResult($result,$row,$field);
      $result = mdecrypt_generic($td, pack("H".strlen($result),
      $result));//pack() perfoms hex to binary
      mcrypt_generic_end($td);
      return($result);
      }

      Function SetupMcrypt($database)
      {
      global $metabase_databases,$metabase_interfaces;
      //check if mcrypt values are set in MetabaseSetupDatabase() options
      array.
      if(!$key = $metabase_interfaces[$metabase_databases[$database]-
      >interface]->options["McryptKey"]){
      //set Metabase error here
      print "you need to assign a secrect McryptKey in the
      MetabaseSetupDatabase() options array.";
      }
      if(!$mode = $metabase_interfaces[$metabase_databases[$database]-
      >interface]->options["McryptMode"])
      $mode = MCRYPT_MODE_ECB;//default
      if(!$cipher = $metabase_interfaces[$metabase_databases[$database]-
      >interface]->options["McryptCipher"])
      $cipher = MCRYPT_TripleDES;//default

      $td = mcrypt_module_open($cipher, "", $mode, "");

      if(!$iv = $metabase_interfaces[$metabase_databases[$database]-
      >interface]->options["McryptIV"]){
      if($mode == MCRYPT_MODE_ECB){// if mode is ecb the iv is ignored so
      set it to zeros
      $iv = "00000000";
      }else{
      $iv = "";// make php throw an error
      //set Metabase error here
      print "you need to assign a McryptIV in the MetabaseSetupDatabase()
      options array.";
      }
      }
      mcrypt_generic_init($td, $key, $iv);
      return $td;
      }

      ?>
    Your message has been successfully submitted and would be delivered to recipients shortly.