Permalink
Browse files

initial import

git-svn-id: http://svn.php.net/repository/pear/packages/Crypt_XXTEA/trunk@220902 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent fc50668 commit 321fd4f18bf0f872ce47fe4893ec0c96632b3718 Wudi Liu committed Oct 2, 2006
Showing with 582 additions and 0 deletions.
  1. +263 −0 Crypt/XXTEA.php
  2. +73 −0 package.xml
  3. +71 −0 tests/decrypt.phpt
  4. +71 −0 tests/encrypt.phpt
  5. +27 −0 tests/long2str.phpt
  6. +29 −0 tests/setkey.phpt
  7. +48 −0 tests/str2long.phpt
View
@@ -0,0 +1,263 @@
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+
+/**
+ * PHP implementation of XXTEA encryption algorithm.
+ *
+ * XXTEA is a secure and fast encryption algorithm. It's suitable for
+ * web development.
+ *
+ * PHP versions 4 and 5
+ *
+ * LICENSE: This library is free software; you can redistribute it
+ * and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301 USA.
+ *
+ * @category Encryption
+ * @package Crypt_XXTEA
+ * @author Ma Bingyao <andot@ujn.edu.cn>
+ * @author Wudi Liu <wudicgi@yahoo.de>
+ * @copyright 2005-2006 Coolcode.CN
+ * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
+ * @version CVS: $Id$
+ * @link http://pear.php.net/package/Crypt_XXTEA
+ */
+
+require_once 'PEAR.php';
+
+// {{{ constants
+
+define('CRYPT_XXTEA_DELTA', 0x9E3779B9);
+
+// }}}
+
+/**
+ * The main class
+ *
+ * @category Encryption
+ * @package Crypt_XXTEA
+ * @author Ma Bingyao <andot@ujn.edu.cn>
+ * @author Wudi Liu <wudicgi@yahoo.de>
+ * @copyright 2005-2006 Coolcode.CN
+ * @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/Crypt_XXTEA
+ */
+class Crypt_XXTEA {
+ // {{{ properties
+
+ /**
+ * The long array of secret key
+ *
+ * @access private
+ *
+ * @var array
+ */
+ var $_key;
+
+ // }}}
+
+ // {{{ setKey()
+
+ /**
+ * Sets the secret key
+ *
+ * The key must be non-empty, and less than or equal to 16 characters
+ *
+ * @access public
+ *
+ * @param string $key the secret key
+ *
+ * @return bool true on success, PEAR_Error on failure
+ */
+ function setKey($key) {
+ if (!is_string($key)) {
+ return PEAR::raiseError('The secret key must be a string.');
+ }
+ $k = $this->_str2long($key, false);
+ if (count($k) > 4) {
+ return PEAR::raiseError('The secret key cannot be more than 16 characters.');
+ } elseif (count($k) == 0) {
+ return PEAR::raiseError('The secret key cannot be empty.');
+ } elseif (count($k) < 4) {
+ for ($i = count($k); $i < 4; $i++) {
+ $k[$i] = 0;
+ }
+ }
+ $this->_key = $k;
+ return true;
+ }
+
+ // }}}
+
+ // {{{ encrypt()
+
+ /**
+ * Encrypts a plain text
+ *
+ * @access public
+ *
+ * @param string $str the plain text
+ *
+ * @return string the cipher text on success, PEAR_Error on failure
+ */
+ function encrypt($str) {
+ if (!is_string($str)) {
+ return PEAR::raiseError('The plain text must be a string.');
+ }
+ if ($this->_key == null) {
+ return PEAR::raiseError('Secret key is undefined.');
+ }
+ if ($str == '') {
+ return '';
+ }
+ $v = $this->_str2long($str, true);
+ $n = count($v) - 1;
+ $z = $v[$n];
+ $y = $v[0];
+ $q = floor(6 + 52 / ($n + 1));
+ $sum = 0;
+ while (0 < $q--) {
+ $sum = $this->_int32($sum + CRYPT_XXTEA_DELTA);
+ $e = $sum >> 2 & 3;
+ for ($p = 0; $p < $n; $p++) {
+ $y = $v[$p + 1];
+ $mx = $this->_int32((($z >> 5 & 0x07FFFFFF) ^ $y << 2) + (($y >> 3 & 0x1FFFFFFF) ^ $z << 4)) ^ $this->_int32(($sum ^ $y) + ($this->_key[$p & 3 ^ $e] ^ $z));
+ $z = $v[$p] = $this->_int32($v[$p] + $mx);
+ }
+ $y = $v[0];
+ $mx = $this->_int32((($z >> 5 & 0x07FFFFFF) ^ $y << 2) + (($y >> 3 & 0x1FFFFFFF) ^ $z << 4)) ^ $this->_int32(($sum ^ $y) + ($this->_key[$p & 3 ^ $e] ^ $z));
+ $z = $v[$n] = $this->_int32($v[$n] + $mx);
+ }
+ return $this->_long2str($v, false);
+ }
+
+ // }}}
+
+ // {{{ decrypt()
+
+ /**
+ * Decrypts a cipher text
+ *
+ * @access public
+ *
+ * @param string $str the cipher text
+ *
+ * @return string the plain text on success, PEAR_Error on failure
+ */
+ function decrypt($str) {
+ if (!is_string($str)) {
+ return PEAR::raiseError('The cipher text must be a string.');
+ }
+ if ($this->_key == null) {
+ return PEAR::raiseError('Secret key is undefined.');
+ }
+ if ($str == '') {
+ return '';
+ }
+ $v = $this->_str2long($str, false);
+ $n = count($v) - 1;
+ $z = $v[$n];
+ $y = $v[0];
+ $q = floor(6 + 52 / ($n + 1));
+ $sum = $this->_int32($q * CRYPT_XXTEA_DELTA);
+ while ($sum != 0) {
+ $e = $sum >> 2 & 3;
+ for ($p = $n; $p > 0; $p--) {
+ $z = $v[$p - 1];
+ $mx = $this->_int32((($z >> 5 & 0x07FFFFFF) ^ $y << 2) + (($y >> 3 & 0x1FFFFFFF) ^ $z << 4)) ^ $this->_int32(($sum ^ $y) + ($this->_key[$p & 3 ^ $e] ^ $z));
+ $y = $v[$p] = $this->_int32($v[$p] - $mx);
+ }
+ $z = $v[$n];
+ $mx = $this->_int32((($z >> 5 & 0x07FFFFFF) ^ $y << 2) + (($y >> 3 & 0x1FFFFFFF) ^ $z << 4)) ^ $this->_int32(($sum ^ $y) + ($this->_key[$p & 3 ^ $e] ^ $z));
+ $y = $v[0] = $this->_int32($v[0] - $mx);
+ $sum = $this->_int32($sum - CRYPT_XXTEA_DELTA);
+ }
+ return $this->_long2str($v, true);
+ }
+
+ // }}}
+
+ // {{{ _long2str()
+
+ /**
+ * Converts long array to string
+ *
+ * @access private
+ *
+ * @param array $v the long array
+ * @param bool $w whether the long array contains the length of
+ * original plain text
+ *
+ * @return string the string
+ */
+ function _long2str($v, $w) {
+ $len = count($v);
+ $s = '';
+ for ($i=0; $i<$len; $i++) {
+ $s .= pack('V', $v[$i]);
+ }
+ if ($w) {
+ return substr($s, 0, $v[$len-1]);
+ } else {
+ return $s;
+ }
+ }
+
+ // }}}
+
+ // {{{ _str2long()
+
+ /**
+ * Converts string to long array
+ *
+ * @access private
+ *
+ * @param string $s the string
+ * @param bool $w whether to append the length of string
+ *
+ * @return string the long array
+ */
+ function _str2long($s, $w) {
+ $v = array_values(unpack('V*', $s.str_repeat("\0", (4-strlen($s)%4)&3)));
+ if ($w) {
+ $v[] = strlen($s);
+ }
+ return $v;
+ }
+
+ // }}}
+
+ // {{{ _int32()
+
+ /**
+ * Fixes overflow problem
+ *
+ * @access private
+ *
+ * @param int $n the integer
+ *
+ * @return int the correct integer
+ */
+ function _int32($n) {
+ while ($n >= 2147483648) $n -= 4294967296;
+ while ($n <= -2147483649) $n += 4294967296;
+ return (int)$n;
+ }
+
+ // }}}
+
+}
+
+?>
View
@@ -0,0 +1,73 @@
+<?xml version="1.0"?>
+<package version="2.0" xmlns="http://pear.php.net/dtd/package-2.0" xmlns:tasks="http://pear.php.net/dtd/tasks-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0 http://pear.php.net/dtd/tasks-1.0.xsd http://pear.php.net/dtd/package-2.0 http://pear.php.net/dtd/package-2.0.xsd">
+ <name>Crypt_XXTEA</name>
+ <channel>pear.php.net</channel>
+ <summary>An implementation of XXTEA encryption algorithm.</summary>
+ <description>
+ XXTEA is a secure and fast encryption algorithm. It's suitable for web development.
+ This package allows you to encrypt or decrypt a string using the algorithm.
+ </description>
+ <lead>
+ <name>Wudi Liu</name>
+ <user>wudicgi</user>
+ <email>wudicgi@yahoo.de</email>
+ <active>yes</active>
+ </lead>
+ <date>2006-10-02</date>
+ <version>
+ <release>0.8.0</release>
+ <api>0.8.0</api>
+ </version>
+ <stability>
+ <release>beta</release>
+ <api>beta</api>
+ </stability>
+ <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL License</license>
+ <notes>
+ Initial release.
+ </notes>
+ <contents>
+ <dir name="/">
+ <dir name="Crypt">
+ <file name="XXTEA.php" role="php">
+ <tasks:replace from="@package_version@" to="version" type="package-info"/>
+ </file>
+ </dir>
+ <dir name="tests">
+ <file name="decrypt.phpt" role="test" />
+ <file name="encrypt.phpt" role="test" />
+ <file name="long2str.phpt" role="test" />
+ <file name="setkey.phpt" role="test" />
+ <file name="str2long.phpt" role="test" />
+ </dir>
+ </dir>
+ </contents>
+ <dependencies>
+ <required>
+ <php>
+ <min>4.2.0</min>
+ </php>
+ <pearinstaller>
+ <min>1.4.0b1</min>
+ </pearinstaller>
+ </required>
+ </dependencies>
+ <phprelease />
+ <changelog>
+ <release>
+ <version>
+ <release>0.8.0</release>
+ <api>0.8.0</api>
+ </version>
+ <stability>
+ <release>beta</release>
+ <api>beta</api>
+ </stability>
+ <date>2006-10-02</date>
+ <license uri="http://www.gnu.org/copyleft/lesser.html">LGPL License</license>
+ <notes>
+ Initial release.
+ </notes>
+ </release>
+ </changelog>
+</package>
Oops, something went wrong.

0 comments on commit 321fd4f

Please sign in to comment.