Source for file xteacmd.php

Documentation is available at xteacmd.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. //
  4. // +----------------------------------------------------------------------+
  5. // | PHP version 4.0 |
  6. // +----------------------------------------------------------------------+
  7. // | Copyright (c) 2004 The PHP Group |
  8. // +----------------------------------------------------------------------+
  9. // | This source file is subject to version 2.02 of the PHP license, |
  10. // | that is bundled with this package in the file LICENSE, and is |
  11. // | available at through the world-wide-web at |
  12. // | http://www.php.net/license/2_02.txt. |
  13. // | If you did not receive a copy of the PHP license and are unable to |
  14. // | obtain it through the world-wide-web, please send a note to |
  15. // | license@php.net so we can mail you a copy immediately. |
  16. // +----------------------------------------------------------------------+
  17. // | Authors: Jeroen Derks <jeroen@derks.it> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: xteacmd.php,v 1.3 2005/05/13 19:34:18 jeroend Exp $
  21.  
  22.  
  23.  
  24. /**
  25. * Simple xtea command line utility.
  26. * Simple xtea command line utility.
  27. *
  28. * @package Crypt_Xtea_Test
  29. * @modulegroup Crypt_Xtea_Test
  30. * @module xteacmd
  31. * @access public
  32. *
  33. * @version $Revision: 1.3 $
  34. * @since 2004/Sep/30
  35. * @author Jeroen Derks <jeroen@derks.it>
  36. */
  37.  
  38. /** Crypt_Xtea class */
  39. 'Crypt/Xtea.php';
  40.  
  41. $flag_decrypt = false; // do encryption by default
  42. $msg = ''; // initialize message to encrypt/decrypt
  43. $key = ''; // initialize key to use
  44.  
  45. $prog = basename($_SERVER['argv'][0]); // program name
  46. $argc = $_SERVER['argc']; // number of command line arguments
  47. $argv = $_SERVER['argv']; // command line arguments array
  48. // process command line flags
  49.  
  50. while (1 < $argc && '-' == $argv[1]{0})
  51. {
  52. $argc--;
  53. $arg = $argv[1];
  54. array_shift($argv);
  55. if ('--' == $arg)
  56. break;
  57.  
  58. $arg = substr($arg, 1);
  59. while (0 < strlen($arg))
  60. {
  61. switch ($arg{0})
  62. {
  63. case 'd': $flag_decrypt = true; break;
  64. case 'e': $flag_decrypt = false; break;
  65. }
  66. $arg = substr($arg, 1);
  67. }
  68. }
  69.  
  70. // check required parameters
  71. if (2 >= $argc)
  72. {
  73. echo "usage: $prog [-de] key msg [ msg ... ]\n" .
  74. "\t-d\tdecrypt message\n" .
  75. "\t-e\tencrypt message (default action)\n";
  76. exit(1);
  77. }
  78.  
  79. // get key
  80. $key = $argv[1];
  81. $argc--;
  82. array_shift($argv);
  83.  
  84. // get messages
  85. while (1 < $argc)
  86. {
  87. $msg = $argv[1];
  88. $obj = new Crypt_Xtea();
  89.  
  90. if ($flag_decrypt)
  91. {
  92. $binmsg = pack('H' . strlen($msg), $msg);
  93. $result = $obj->decrypt($binmsg, $key);
  94. }
  95. else
  96. {
  97. $binmsg = $obj->encrypt($msg, $key);
  98. $result = join('', unpack('H*', $binmsg));
  99. }
  100.  
  101. // output result
  102. echo $result;
  103.  
  104. // get next message
  105. $argc--;
  106. $arg = array_shift($argv);
  107. }
  108. ?>

Documentation generated on Thu, 25 May 2006 15:31:27 +0200 by phpDocumentor 1.3.0RC3