2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * Pure-PHP implementation of DES.
7 * Uses mcrypt, if available, and an internal implementation, otherwise.
11 * Useful resources are as follows:
13 * - {@link http://en.wikipedia.org/wiki/DES_supplementary_material Wikipedia: DES supplementary material}
14 * - {@link http://www.itl.nist.gov/fipspubs/fip46-2.htm FIPS 46-2 - (DES), Data Encryption Standard}
15 * - {@link http://www.cs.eku.edu/faculty/styer/460/Encrypt/JS-DES.html JavaScript DES Example}
17 * Here's a short example of how to use this library:
20 * include('Crypt/DES.php');
22 * $des = new Crypt_DES();
24 * $des->setKey('abcdefgh');
28 * for ($i = 0; $i < $size; $i++) {
32 * echo $des->decrypt($des->encrypt($plaintext));
36 * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
37 * of this software and associated documentation files (the "Software"), to deal
38 * in the Software without restriction, including without limitation the rights
39 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
40 * copies of the Software, and to permit persons to whom the Software is
41 * furnished to do so, subject to the following conditions:
43 * The above copyright notice and this permission notice shall be included in
44 * all copies or substantial portions of the Software.
46 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
47 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
48 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
49 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
50 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
51 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
56 * @author Jim Wigginton <terrafrost@php.net>
57 * @copyright MMVII Jim Wigginton
58 * @license http://www.opensource.org/licenses/mit-license.html MIT License
59 * @version $Id: DES.php,v 1.12 2010/02/09 06:10:26 terrafrost Exp $
60 * @link http://phpseclib.sourceforge.net
65 * @see Crypt_DES::_prepareKey()
66 * @see Crypt_DES::_processBlock()
69 * Contains array_reverse($keys[CRYPT_DES_DECRYPT])
71 define('CRYPT_DES_ENCRYPT', 0);
73 * Contains array_reverse($keys[CRYPT_DES_ENCRYPT])
75 define('CRYPT_DES_DECRYPT', 1);
80 * @see Crypt_DES::encrypt()
81 * @see Crypt_DES::decrypt()
84 * Encrypt / decrypt using the Counter mode.
86 * Set to -1 since that's what Crypt/Random.php uses to index the CTR mode.
88 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Counter_.28CTR.29
90 define('CRYPT_DES_MODE_CTR', -1);
92 * Encrypt / decrypt using the Electronic Code Book mode.
94 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Electronic_codebook_.28ECB.29
96 define('CRYPT_DES_MODE_ECB', 1);
98 * Encrypt / decrypt using the Code Book Chaining mode.
100 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher-block_chaining_.28CBC.29
102 define('CRYPT_DES_MODE_CBC', 2);
104 * Encrypt / decrypt using the Cipher Feedback mode.
106 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Cipher_feedback_.28CFB.29
108 define('CRYPT_DES_MODE_CFB', 3);
110 * Encrypt / decrypt using the Cipher Feedback mode.
112 * @link http://en.wikipedia.org/wiki/Block_cipher_modes_of_operation#Output_feedback_.28OFB.29
114 define('CRYPT_DES_MODE_OFB', 4);
119 * @see Crypt_DES::Crypt_DES()
122 * Toggles the internal implementation
124 define('CRYPT_DES_MODE_INTERNAL', 1);
126 * Toggles the mcrypt implementation
128 define('CRYPT_DES_MODE_MCRYPT', 2);
132 * Pure-PHP implementation of DES.
134 * @author Jim Wigginton <terrafrost@php.net>
143 * @see Crypt_DES::setKey()
147 var $keys = "\0\0\0\0\0\0\0\0";
150 * The Encryption Mode
152 * @see Crypt_DES::Crypt_DES()
159 * Continuous Buffer status
161 * @see Crypt_DES::enableContinuousBuffer()
165 var $continuousBuffer = false;
170 * @see Crypt_DES::enablePadding()
177 * The Initialization Vector
179 * @see Crypt_DES::setIV()
183 var $iv = "\0\0\0\0\0\0\0\0";
186 * A "sliding" Initialization Vector
188 * @see Crypt_DES::enableContinuousBuffer()
192 var $encryptIV = "\0\0\0\0\0\0\0\0";
195 * A "sliding" Initialization Vector
197 * @see Crypt_DES::enableContinuousBuffer()
201 var $decryptIV = "\0\0\0\0\0\0\0\0";
204 * mcrypt resource for encryption
206 * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
207 * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
209 * @see Crypt_DES::encrypt()
216 * mcrypt resource for decryption
218 * The mcrypt resource can be recreated every time something needs to be created or it can be created just once.
219 * Since mcrypt operates in continuous mode, by default, it'll need to be recreated when in non-continuous mode.
221 * @see Crypt_DES::decrypt()
228 * Does the enmcrypt resource need to be (re)initialized?
230 * @see Crypt_DES::setKey()
231 * @see Crypt_DES::setIV()
235 var $enchanged = true;
238 * Does the demcrypt resource need to be (re)initialized?
240 * @see Crypt_DES::setKey()
241 * @see Crypt_DES::setIV()
245 var $dechanged = true;
248 * Is the mode one that is paddable?
250 * @see Crypt_DES::Crypt_DES()
254 var $paddable = false;
257 * Encryption buffer for CTR, OFB and CFB modes
259 * @see Crypt_DES::encrypt()
266 * Decryption buffer for CTR, OFB and CFB modes
268 * @see Crypt_DES::decrypt()
275 * mcrypt resource for CFB mode
277 * @see Crypt_DES::encrypt()
278 * @see Crypt_DES::decrypt()
285 * Default Constructor.
287 * Determines whether or not the mcrypt extension should be used. $mode should only, at present, be
288 * CRYPT_DES_MODE_ECB or CRYPT_DES_MODE_CBC. If not explictly set, CRYPT_DES_MODE_CBC will be used.
290 * @param optional Integer $mode
294 function Crypt_DES($mode = CRYPT_DES_MODE_CBC
)
296 if ( !defined('CRYPT_DES_MODE') ) {
298 case extension_loaded('mcrypt') && in_array('des', mcrypt_list_algorithms()):
299 define('CRYPT_DES_MODE', CRYPT_DES_MODE_MCRYPT
);
302 define('CRYPT_DES_MODE', CRYPT_DES_MODE_INTERNAL
);
306 switch ( CRYPT_DES_MODE
) {
307 case CRYPT_DES_MODE_MCRYPT
:
309 case CRYPT_DES_MODE_ECB
:
310 $this->paddable
= true;
311 $this->mode
= MCRYPT_MODE_ECB
;
313 case CRYPT_DES_MODE_CTR
:
315 //$this->mode = in_array('ctr', mcrypt_list_modes()) ? 'ctr' : CRYPT_DES_MODE_CTR;
317 case CRYPT_DES_MODE_CFB
:
318 $this->mode
= 'ncfb';
320 case CRYPT_DES_MODE_OFB
:
321 $this->mode
= MCRYPT_MODE_NOFB
;
323 case CRYPT_DES_MODE_CBC
:
325 $this->paddable
= true;
326 $this->mode
= MCRYPT_MODE_CBC
;
332 case CRYPT_DES_MODE_ECB
:
333 case CRYPT_DES_MODE_CBC
:
334 $this->paddable
= true;
337 case CRYPT_DES_MODE_CTR
:
338 case CRYPT_DES_MODE_CFB
:
339 case CRYPT_DES_MODE_OFB
:
343 $this->paddable
= true;
344 $this->mode
= CRYPT_DES_MODE_CBC
;
352 * Keys can be of any length. DES, itself, uses 64-bit keys (eg. strlen($key) == 8), however, we
353 * only use the first eight, if $key has more then eight characters in it, and pad $key with the
354 * null byte if it is less then eight characters long.
356 * DES also requires that every eighth bit be a parity bit, however, we'll ignore that.
358 * If the key is not explicitly set, it'll be assumed to be all zero's.
363 function setKey($key)
365 $this->keys
= ( CRYPT_DES_MODE
== CRYPT_DES_MODE_MCRYPT
) ?
str_pad(substr($key, 0, 8), 8, chr(0)) : $this->_prepareKey($key);
366 $this->changed
= true;
372 * Depending on what $method is set to, setPassword()'s (optional) parameters are as follows:
373 * {@link http://en.wikipedia.org/wiki/PBKDF2 pbkdf2}:
374 * $hash, $salt, $count
376 * @param String $password
377 * @param optional String $method
380 function setPassword($password, $method = 'pbkdf2')
386 list(, , $hash, $salt, $count) = func_get_args();
390 // WPA and WPA use the SSID as the salt
392 $salt = 'phpseclib/salt';
394 // RFC2898#section-4.2 uses 1,000 iterations by default
395 // WPA and WPA2 use 4,096.
396 if (!isset($count)) {
400 if (!class_exists('Crypt_Hash')) {
401 require_once('Crypt/Hash.php');
405 while (strlen($key) < 8) { // $dkLen == 8
406 //$dk.= $this->_pbkdf($password, $salt, $count, $i++);
407 $hmac = new Crypt_Hash();
408 $hmac->setHash($hash);
409 $hmac->setKey($password);
410 $f = $u = $hmac->hash($salt . pack('N', $i++
));
411 for ($j = 2; $j <= $count; $j++
) {
412 $u = $hmac->hash($u);
423 * Sets the initialization vector. (optional)
425 * SetIV is not required when CRYPT_DES_MODE_ECB is being used. If not explictly set, it'll be assumed
433 $this->encryptIV
= $this->decryptIV
= $this->iv
= str_pad(substr($iv, 0, 8), 8, chr(0));
434 $this->changed
= true;
438 * Generate CTR XOR encryption key
440 * Encrypt the output of this and XOR it against the ciphertext / plaintext to get the
441 * plaintext / ciphertext in CTR mode.
443 * @see Crypt_DES::decrypt()
444 * @see Crypt_DES::encrypt()
446 * @param Integer $length
449 function _generate_xor($length, &$iv)
452 $num_blocks = ($length +
7) >> 3;
453 for ($i = 0; $i < $num_blocks; $i++
) {
455 for ($j = 4; $j <= 8; $j+
=4) {
456 $temp = substr($iv, -$j, 4);
458 case "\xFF\xFF\xFF\xFF":
459 $iv = substr_replace($iv, "\x00\x00\x00\x00", -$j, 4);
461 case "\x7F\xFF\xFF\xFF":
462 $iv = substr_replace($iv, "\x80\x00\x00\x00", -$j, 4);
465 extract(unpack('Ncount', $temp));
466 $iv = substr_replace($iv, pack('N', $count +
1), -$j, 4);
476 * Encrypts a message.
478 * $plaintext will be padded with up to 8 additional bytes. Other DES implementations may or may not pad in the
479 * same manner. Other common approaches to padding and the reasons why it's necessary are discussed in the following
482 * {@link http://www.di-mgt.com.au/cryptopad.html http://www.di-mgt.com.au/cryptopad.html}
484 * An alternative to padding is to, separately, send the length of the file. This is what SSH, in fact, does.
485 * strlen($plaintext) will still need to be a multiple of 8, however, arbitrary values can be added to make it that
488 * @see Crypt_DES::decrypt()
490 * @param String $plaintext
492 function encrypt($plaintext)
494 if ($this->paddable
) {
495 $plaintext = $this->_pad($plaintext);
498 if ( CRYPT_DES_MODE
== CRYPT_DES_MODE_MCRYPT
) {
499 if ($this->enchanged
) {
500 if (!isset($this->enmcrypt
)) {
501 $this->enmcrypt
= mcrypt_module_open(MCRYPT_DES
, '', $this->mode
, '');
503 mcrypt_generic_init($this->enmcrypt
, $this->keys
, $this->encryptIV
);
504 if ($this->mode
!= 'ncfb') {
505 $this->enchanged
= false;
509 if ($this->mode
!= 'ncfb') {
510 $ciphertext = mcrypt_generic($this->enmcrypt
, $plaintext);
512 if ($this->enchanged
) {
513 $this->ecb
= mcrypt_module_open(MCRYPT_DES
, '', MCRYPT_MODE_ECB
, '');
514 mcrypt_generic_init($this->ecb
, $this->keys
, "\0\0\0\0\0\0\0\0");
515 $this->enchanged
= false;
518 if (strlen($this->enbuffer
)) {
519 $ciphertext = $plaintext ^
substr($this->encryptIV
, strlen($this->enbuffer
));
520 $this->enbuffer
.= $ciphertext;
521 if (strlen($this->enbuffer
) == 8) {
522 $this->encryptIV
= $this->enbuffer
;
523 $this->enbuffer
= '';
524 mcrypt_generic_init($this->enmcrypt
, $this->keys
, $this->encryptIV
);
526 $plaintext = substr($plaintext, strlen($ciphertext));
531 $last_pos = strlen($plaintext) & 0xFFFFFFF8;
532 $ciphertext.= $last_pos ?
mcrypt_generic($this->enmcrypt
, substr($plaintext, 0, $last_pos)) : '';
534 if (strlen($plaintext) & 0x7) {
535 if (strlen($ciphertext)) {
536 $this->encryptIV
= substr($ciphertext, -8);
538 $this->encryptIV
= mcrypt_generic($this->ecb
, $this->encryptIV
);
539 $this->enbuffer
= substr($plaintext, $last_pos) ^
$this->encryptIV
;
540 $ciphertext.= $this->enbuffer
;
544 if (!$this->continuousBuffer
) {
545 mcrypt_generic_init($this->enmcrypt
, $this->keys
, $this->encryptIV
);
551 if (!is_array($this->keys
)) {
552 $this->keys
= $this->_prepareKey("\0\0\0\0\0\0\0\0");
555 $buffer = &$this->enbuffer
;
556 $continuousBuffer = $this->continuousBuffer
;
558 switch ($this->mode
) {
559 case CRYPT_DES_MODE_ECB
:
560 for ($i = 0; $i < strlen($plaintext); $i+
=8) {
561 $ciphertext.= $this->_processBlock(substr($plaintext, $i, 8), CRYPT_DES_ENCRYPT
);
564 case CRYPT_DES_MODE_CBC
:
565 $xor = $this->encryptIV
;
566 for ($i = 0; $i < strlen($plaintext); $i+
=8) {
567 $block = substr($plaintext, $i, 8);
568 $block = $this->_processBlock($block ^
$xor, CRYPT_DES_ENCRYPT
);
570 $ciphertext.= $block;
572 if ($this->continuousBuffer
) {
573 $this->encryptIV
= $xor;
576 case CRYPT_DES_MODE_CTR
:
577 $xor = $this->encryptIV
;
578 if (strlen($buffer['encrypted'])) {
579 for ($i = 0; $i < strlen($plaintext); $i+
=8) {
580 $block = substr($plaintext, $i, 8);
581 $buffer['encrypted'].= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT
);
582 $key = $this->_string_shift($buffer['encrypted'], 8);
583 $ciphertext.= $block ^
$key;
586 for ($i = 0; $i < strlen($plaintext); $i+
=8) {
587 $block = substr($plaintext, $i, 8);
588 $key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT
);
589 $ciphertext.= $block ^
$key;
592 if ($this->continuousBuffer
) {
593 $this->encryptIV
= $xor;
594 if ($start = strlen($plaintext) & 7) {
595 $buffer['encrypted'] = substr($key, $start) . $buffer['encrypted'];
599 case CRYPT_DES_MODE_CFB
:
600 if (!empty($buffer['xor'])) {
601 $ciphertext = $plaintext ^
$buffer['xor'];
602 $iv = $buffer['encrypted'] . $ciphertext;
603 $start = strlen($ciphertext);
604 $buffer['encrypted'].= $ciphertext;
605 $buffer['xor'] = substr($buffer['xor'], strlen($ciphertext));
608 $iv = $this->encryptIV
;
612 for ($i = $start; $i < strlen($plaintext); $i+
=8) {
613 $block = substr($plaintext, $i, 8);
614 $xor = $this->_processBlock($iv, CRYPT_DES_ENCRYPT
);
616 if ($continuousBuffer && strlen($iv) != 8) {
619 'xor' => substr($xor, strlen($iv))
625 if ($this->continuousBuffer
) {
626 $this->encryptIV
= $iv;
629 case CRYPT_DES_MODE_OFB
:
630 $xor = $this->encryptIV
;
631 if (strlen($buffer)) {
632 for ($i = 0; $i < strlen($plaintext); $i+
=8) {
633 $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT
);
635 $key = $this->_string_shift($buffer, 8);
636 $ciphertext.= substr($plaintext, $i, 8) ^
$key;
639 for ($i = 0; $i < strlen($plaintext); $i+
=8) {
640 $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT
);
641 $ciphertext.= substr($plaintext, $i, 8) ^
$xor;
645 if ($this->continuousBuffer
) {
646 $this->encryptIV
= $xor;
647 if ($start = strlen($plaintext) & 7) {
648 $buffer = substr($key, $start) . $buffer;
657 * Decrypts a message.
659 * If strlen($ciphertext) is not a multiple of 8, null bytes will be added to the end of the string until it is.
661 * @see Crypt_DES::encrypt()
663 * @param String $ciphertext
665 function decrypt($ciphertext)
667 if ($this->paddable
) {
668 // we pad with chr(0) since that's what mcrypt_generic does. to quote from http://php.net/function.mcrypt-generic :
669 // "The data is padded with "\0" to make sure the length of the data is n * blocksize."
670 $ciphertext = str_pad($ciphertext, (strlen($ciphertext) +
7) & 0xFFFFFFF8, chr(0));
673 if ( CRYPT_DES_MODE
== CRYPT_DES_MODE_MCRYPT
) {
674 if ($this->dechanged
) {
675 if (!isset($this->demcrypt
)) {
676 $this->demcrypt
= mcrypt_module_open(MCRYPT_DES
, '', $this->mode
, '');
678 mcrypt_generic_init($this->demcrypt
, $this->keys
, $this->decryptIV
);
679 if ($this->mode
!= 'ncfb') {
680 $this->dechanged
= false;
684 if ($this->mode
!= 'ncfb') {
685 $plaintext = mdecrypt_generic($this->demcrypt
, $ciphertext);
687 if ($this->dechanged
) {
688 $this->ecb
= mcrypt_module_open(MCRYPT_DES
, '', MCRYPT_MODE_ECB
, '');
689 mcrypt_generic_init($this->ecb
, $this->keys
, "\0\0\0\0\0\0\0\0");
690 $this->dechanged
= false;
693 if (strlen($this->debuffer
)) {
694 $plaintext = $ciphertext ^
substr($this->decryptIV
, strlen($this->debuffer
));
696 $this->debuffer
.= substr($ciphertext, 0, strlen($plaintext));
697 if (strlen($this->debuffer
) == 8) {
698 $this->decryptIV
= $this->debuffer
;
699 $this->debuffer
= '';
700 mcrypt_generic_init($this->demcrypt
, $this->keys
, $this->decryptIV
);
702 $ciphertext = substr($ciphertext, strlen($plaintext));
707 $last_pos = strlen($ciphertext) & 0xFFFFFFF8;
708 $plaintext.= $last_pos ?
mdecrypt_generic($this->demcrypt
, substr($ciphertext, 0, $last_pos)) : '';
710 if (strlen($ciphertext) & 0x7) {
711 if (strlen($plaintext)) {
712 $this->decryptIV
= substr($ciphertext, $last_pos - 8, 8);
714 $this->decryptIV
= mcrypt_generic($this->ecb
, $this->decryptIV
);
715 $this->debuffer
= substr($ciphertext, $last_pos);
716 $plaintext.= $this->debuffer ^
$this->decryptIV
;
722 if (!$this->continuousBuffer
) {
723 mcrypt_generic_init($this->demcrypt
, $this->keys
, $this->decryptIV
);
726 return $this->paddable ?
$this->_unpad($plaintext) : $plaintext;
729 if (!is_array($this->keys
)) {
730 $this->keys
= $this->_prepareKey("\0\0\0\0\0\0\0\0");
733 $buffer = &$this->debuffer
;
734 $continuousBuffer = $this->continuousBuffer
;
736 switch ($this->mode
) {
737 case CRYPT_DES_MODE_ECB
:
738 for ($i = 0; $i < strlen($ciphertext); $i+
=8) {
739 $plaintext.= $this->_processBlock(substr($ciphertext, $i, 8), CRYPT_DES_DECRYPT
);
742 case CRYPT_DES_MODE_CBC
:
743 $xor = $this->decryptIV
;
744 for ($i = 0; $i < strlen($ciphertext); $i+
=8) {
745 $block = substr($ciphertext, $i, 8);
746 $plaintext.= $this->_processBlock($block, CRYPT_DES_DECRYPT
) ^
$xor;
749 if ($this->continuousBuffer
) {
750 $this->decryptIV
= $xor;
753 case CRYPT_DES_MODE_CTR
:
754 $xor = $this->decryptIV
;
755 if (strlen($buffer['ciphertext'])) {
756 for ($i = 0; $i < strlen($ciphertext); $i+
=8) {
757 $block = substr($ciphertext, $i, 8);
758 $buffer['ciphertext'].= $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT
);
759 $key = $this->_string_shift($buffer['ciphertext'], 8);
760 $plaintext.= $block ^
$key;
763 for ($i = 0; $i < strlen($ciphertext); $i+
=8) {
764 $block = substr($ciphertext, $i, 8);
765 $key = $this->_processBlock($this->_generate_xor(8, $xor), CRYPT_DES_ENCRYPT
);
766 $plaintext.= $block ^
$key;
769 if ($this->continuousBuffer
) {
770 $this->decryptIV
= $xor;
771 if ($start = strlen($ciphertext) %
8) {
772 $buffer['ciphertext'] = substr($key, $start) . $buffer['ciphertext'];
776 case CRYPT_DES_MODE_CFB
:
777 if (!empty($buffer['ciphertext'])) {
778 $plaintext = $ciphertext ^
substr($this->decryptIV
, strlen($buffer['ciphertext']));
779 $buffer['ciphertext'].= substr($ciphertext, 0, strlen($plaintext));
780 if (strlen($buffer['ciphertext']) == 8) {
781 $xor = $this->_processBlock($buffer['ciphertext'], CRYPT_DES_ENCRYPT
);
782 $buffer['ciphertext'] = '';
784 $start = strlen($plaintext);
785 $block = $this->decryptIV
;
788 $xor = $this->_processBlock($this->decryptIV
, CRYPT_DES_ENCRYPT
);
792 for ($i = $start; $i < strlen($ciphertext); $i+
=8) {
793 $block = substr($ciphertext, $i, 8);
794 $plaintext.= $block ^
$xor;
795 if ($continuousBuffer && strlen($block) != 8) {
796 $buffer['ciphertext'].= $block;
798 } else if (strlen($block) == 8) {
799 $xor = $this->_processBlock($block, CRYPT_DES_ENCRYPT
);
802 if ($this->continuousBuffer
) {
803 $this->decryptIV
= $block;
806 case CRYPT_DES_MODE_OFB
:
807 $xor = $this->decryptIV
;
808 if (strlen($buffer)) {
809 for ($i = 0; $i < strlen($ciphertext); $i+
=8) {
810 $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT
);
812 $key = $this->_string_shift($buffer, 8);
813 $plaintext.= substr($ciphertext, $i, 8) ^
$key;
816 for ($i = 0; $i < strlen($ciphertext); $i+
=8) {
817 $xor = $this->_processBlock($xor, CRYPT_DES_ENCRYPT
);
818 $plaintext.= substr($ciphertext, $i, 8) ^
$xor;
822 if ($this->continuousBuffer
) {
823 $this->decryptIV
= $xor;
824 if ($start = strlen($ciphertext) %
8) {
825 $buffer = substr($key, $start) . $buffer;
830 return $this->paddable ?
$this->_unpad($plaintext) : $plaintext;
834 * Treat consecutive "packets" as if they are a continuous buffer.
836 * Say you have a 16-byte plaintext $plaintext. Using the default behavior, the two following code snippets
837 * will yield different outputs:
840 * echo $des->encrypt(substr($plaintext, 0, 8));
841 * echo $des->encrypt(substr($plaintext, 8, 8));
844 * echo $des->encrypt($plaintext);
847 * The solution is to enable the continuous buffer. Although this will resolve the above discrepancy, it creates
848 * another, as demonstrated with the following:
851 * $des->encrypt(substr($plaintext, 0, 8));
852 * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
855 * echo $des->decrypt($des->encrypt(substr($plaintext, 8, 8)));
858 * With the continuous buffer disabled, these would yield the same output. With it enabled, they yield different
859 * outputs. The reason is due to the fact that the initialization vector's change after every encryption /
860 * decryption round when the continuous buffer is enabled. When it's disabled, they remain constant.
862 * Put another way, when the continuous buffer is enabled, the state of the Crypt_DES() object changes after each
863 * encryption / decryption round, whereas otherwise, it'd remain constant. For this reason, it's recommended that
864 * continuous buffers not be used. They do offer better security and are, in fact, sometimes required (SSH uses them),
865 * however, they are also less intuitive and more likely to cause you problems.
867 * @see Crypt_DES::disableContinuousBuffer()
870 function enableContinuousBuffer()
872 $this->continuousBuffer
= true;
876 * Treat consecutive packets as if they are a discontinuous buffer.
878 * The default behavior.
880 * @see Crypt_DES::enableContinuousBuffer()
883 function disableContinuousBuffer()
885 $this->continuousBuffer
= false;
886 $this->encryptIV
= $this->iv
;
887 $this->decryptIV
= $this->iv
;
893 * DES works by encrypting eight bytes at a time. If you ever need to encrypt or decrypt something that's not
894 * a multiple of eight, it becomes necessary to pad the input so that it's length is a multiple of eight.
896 * Padding is enabled by default. Sometimes, however, it is undesirable to pad strings. Such is the case in SSH1,
897 * where "packets" are padded with random bytes before being encrypted. Unpad these packets and you risk stripping
898 * away characters that shouldn't be stripped away. (SSH knows how many bytes are added because the length is
899 * transmitted separately)
901 * @see Crypt_DES::disablePadding()
904 function enablePadding()
906 $this->padding
= true;
910 * Do not pad packets.
912 * @see Crypt_DES::enablePadding()
915 function disablePadding()
917 $this->padding
= false;
923 * Pads a string using the RSA PKCS padding standards so that its length is a multiple of the blocksize (8).
924 * 8 - (strlen($text) & 7) bytes are added, each of which is equal to chr(8 - (strlen($text) & 7)
926 * If padding is disabled and $text is not a multiple of the blocksize, the string will be padded regardless
927 * and padding will, hence forth, be enabled.
929 * @see Crypt_DES::_unpad()
934 $length = strlen($text);
936 if (!$this->padding
) {
937 if (($length & 7) == 0) {
940 user_error("The plaintext's length ($length) is not a multiple of the block size (8)", E_USER_NOTICE
);
941 $this->padding
= true;
945 $pad = 8 - ($length & 7);
946 return str_pad($text, $length +
$pad, chr($pad));
952 * If padding is enabled and the reported padding length is invalid the encryption key will be assumed to be wrong
953 * and false will be returned.
955 * @see Crypt_DES::_pad()
958 function _unpad($text)
960 if (!$this->padding
) {
964 $length = ord($text[strlen($text) - 1]);
966 if (!$length ||
$length > 8) {
970 return substr($text, 0, -$length);
974 * Encrypts or decrypts a 64-bit block
976 * $mode should be either CRYPT_DES_ENCRYPT or CRYPT_DES_DECRYPT. See
977 * {@link http://en.wikipedia.org/wiki/Image:Feistel.png Feistel.png} to get a general
978 * idea of what this function does.
981 * @param String $block
982 * @param Integer $mode
985 function _processBlock($block, $mode)
987 // s-boxes. in the official DES docs, they're described as being matrices that
988 // one accesses by using the first and last bits to determine the row and the
989 // middle four bits to determine the column. in this implementation, they've
990 // been converted to vectors
991 static $sbox = array(
993 14, 0, 4, 15, 13, 7, 1, 4, 2, 14, 15, 2, 11, 13, 8, 1,
994 3, 10 ,10, 6, 6, 12, 12, 11, 5, 9, 9, 5, 0, 3, 7, 8,
995 4, 15, 1, 12, 14, 8, 8, 2, 13, 4, 6, 9, 2, 1, 11, 7,
996 15, 5, 12, 11, 9, 3, 7, 14, 3, 10, 10, 0, 5, 6, 0, 13
999 15, 3, 1, 13, 8, 4, 14, 7, 6, 15, 11, 2, 3, 8, 4, 14,
1000 9, 12, 7, 0, 2, 1, 13, 10, 12, 6, 0, 9, 5, 11, 10, 5,
1001 0, 13, 14, 8, 7, 10, 11, 1, 10, 3, 4, 15, 13, 4, 1, 2,
1002 5, 11, 8, 6, 12, 7, 6, 12, 9, 0, 3, 5, 2, 14, 15, 9
1005 10, 13, 0, 7, 9, 0, 14, 9, 6, 3, 3, 4, 15, 6, 5, 10,
1006 1, 2, 13, 8, 12, 5, 7, 14, 11, 12, 4, 11, 2, 15, 8, 1,
1007 13, 1, 6, 10, 4, 13, 9, 0, 8, 6, 15, 9, 3, 8, 0, 7,
1008 11, 4, 1, 15, 2, 14, 12, 3, 5, 11, 10, 5, 14, 2, 7, 12
1011 7, 13, 13, 8, 14, 11, 3, 5, 0, 6, 6, 15, 9, 0, 10, 3,
1012 1, 4, 2, 7, 8, 2, 5, 12, 11, 1, 12, 10, 4, 14, 15, 9,
1013 10, 3, 6, 15, 9, 0, 0, 6, 12, 10, 11, 1, 7, 13, 13, 8,
1014 15, 9, 1, 4, 3, 5, 14, 11, 5, 12, 2, 7, 8, 2, 4, 14
1017 2, 14, 12, 11, 4, 2, 1, 12, 7, 4, 10, 7, 11, 13, 6, 1,
1018 8, 5, 5, 0, 3, 15, 15, 10, 13, 3, 0, 9, 14, 8, 9, 6,
1019 4, 11, 2, 8, 1, 12, 11, 7, 10, 1, 13, 14, 7, 2, 8, 13,
1020 15, 6, 9, 15, 12, 0, 5, 9, 6, 10, 3, 4, 0, 5, 14, 3
1023 12, 10, 1, 15, 10, 4, 15, 2, 9, 7, 2, 12, 6, 9, 8, 5,
1024 0, 6, 13, 1, 3, 13, 4, 14, 14, 0, 7, 11, 5, 3, 11, 8,
1025 9, 4, 14, 3, 15, 2, 5, 12, 2, 9, 8, 5, 12, 15, 3, 10,
1026 7, 11, 0, 14, 4, 1, 10, 7, 1, 6, 13, 0, 11, 8, 6, 13
1029 4, 13, 11, 0, 2, 11, 14, 7, 15, 4, 0, 9, 8, 1, 13, 10,
1030 3, 14, 12, 3, 9, 5, 7, 12, 5, 2, 10, 15, 6, 8, 1, 6,
1031 1, 6, 4, 11, 11, 13, 13, 8, 12, 1, 3, 4, 7, 10, 14, 7,
1032 10, 9, 15, 5, 6, 0, 8, 15, 0, 14, 5, 2, 9, 3, 2, 12
1035 13, 1, 2, 15, 8, 13, 4, 8, 6, 10, 15, 3, 11, 7, 1, 4,
1036 10, 12, 9, 5, 3, 6, 14, 11, 5, 0, 0, 14, 12, 9, 7, 2,
1037 7, 2, 11, 1, 4, 14, 1, 7, 9, 4, 12, 10, 14, 8, 2, 13,
1038 0, 15, 6, 12, 10, 9, 13, 0, 15, 3, 3, 5, 5, 6, 8, 11
1042 $keys = $this->keys
;
1044 $temp = unpack('Na/Nb', $block);
1045 $block = array($temp['a'], $temp['b']);
1047 // because php does arithmetic right shifts, if the most significant bits are set, right
1048 // shifting those into the correct position will add 1's - not 0's. this will intefere
1049 // with the | operation unless a second & is done. so we isolate these bits and left shift
1050 // them into place. we then & each block with 0x7FFFFFFF to prevennt 1's from being added
1051 // for any other shifts.
1053 ($block[0] >> 31) & 1,
1054 ($block[1] >> 31) & 1
1056 $block[0] &= 0x7FFFFFFF;
1057 $block[1] &= 0x7FFFFFFF;
1059 // we isolate the appropriate bit in the appropriate integer and shift as appropriate. in
1060 // some cases, there are going to be multiple bits in the same integer that need to be shifted
1061 // in the same way. we combine those into one shift operation.
1063 (($block[1] & 0x00000040) << 25) |
(($block[1] & 0x00004000) << 16) |
1064 (($block[1] & 0x00400001) << 7) |
(($block[1] & 0x40000100) >> 2) |
1065 (($block[0] & 0x00000040) << 21) |
(($block[0] & 0x00004000) << 12) |
1066 (($block[0] & 0x00400001) << 3) |
(($block[0] & 0x40000100) >> 6) |
1067 (($block[1] & 0x00000010) << 19) |
(($block[1] & 0x00001000) << 10) |
1068 (($block[1] & 0x00100000) << 1) |
(($block[1] & 0x10000000) >> 8) |
1069 (($block[0] & 0x00000010) << 15) |
(($block[0] & 0x00001000) << 6) |
1070 (($block[0] & 0x00100000) >> 3) |
(($block[0] & 0x10000000) >> 12) |
1071 (($block[1] & 0x00000004) << 13) |
(($block[1] & 0x00000400) << 4) |
1072 (($block[1] & 0x00040000) >> 5) |
(($block[1] & 0x04000000) >> 14) |
1073 (($block[0] & 0x00000004) << 9) |
( $block[0] & 0x00000400 ) |
1074 (($block[0] & 0x00040000) >> 9) |
(($block[0] & 0x04000000) >> 18) |
1075 (($block[1] & 0x00010000) >> 11) |
(($block[1] & 0x01000000) >> 20) |
1076 (($block[0] & 0x00010000) >> 15) |
(($block[0] & 0x01000000) >> 24)
1078 (($block[1] & 0x00000080) << 24) |
(($block[1] & 0x00008000) << 15) |
1079 (($block[1] & 0x00800002) << 6) |
(($block[0] & 0x00000080) << 20) |
1080 (($block[0] & 0x00008000) << 11) |
(($block[0] & 0x00800002) << 2) |
1081 (($block[1] & 0x00000020) << 18) |
(($block[1] & 0x00002000) << 9) |
1082 ( $block[1] & 0x00200000 ) |
(($block[1] & 0x20000000) >> 9) |
1083 (($block[0] & 0x00000020) << 14) |
(($block[0] & 0x00002000) << 5) |
1084 (($block[0] & 0x00200000) >> 4) |
(($block[0] & 0x20000000) >> 13) |
1085 (($block[1] & 0x00000008) << 12) |
(($block[1] & 0x00000800) << 3) |
1086 (($block[1] & 0x00080000) >> 6) |
(($block[1] & 0x08000000) >> 15) |
1087 (($block[0] & 0x00000008) << 8) |
(($block[0] & 0x00000800) >> 1) |
1088 (($block[0] & 0x00080000) >> 10) |
(($block[0] & 0x08000000) >> 19) |
1089 (($block[1] & 0x00000200) >> 3) |
(($block[0] & 0x00000200) >> 7) |
1090 (($block[1] & 0x00020000) >> 12) |
(($block[1] & 0x02000000) >> 21) |
1091 (($block[0] & 0x00020000) >> 16) |
(($block[0] & 0x02000000) >> 25) |
1092 ($msb[1] << 28) |
($msb[0] << 24)
1095 for ($i = 0; $i < 16; $i++
) {
1096 // start of "the Feistel (F) function" - see the following URL:
1097 // http://en.wikipedia.org/wiki/Image:Data_Encryption_Standard_InfoBox_Diagram.png
1098 $temp = (($sbox[0][((($block[1] >> 27) & 0x1F) |
(($block[1] & 1) << 5)) ^
$keys[$mode][$i][0]]) << 28)
1099 |
(($sbox[1][(($block[1] & 0x1F800000) >> 23) ^
$keys[$mode][$i][1]]) << 24)
1100 |
(($sbox[2][(($block[1] & 0x01F80000) >> 19) ^
$keys[$mode][$i][2]]) << 20)
1101 |
(($sbox[3][(($block[1] & 0x001F8000) >> 15) ^
$keys[$mode][$i][3]]) << 16)
1102 |
(($sbox[4][(($block[1] & 0x0001F800) >> 11) ^
$keys[$mode][$i][4]]) << 12)
1103 |
(($sbox[5][(($block[1] & 0x00001F80) >> 7) ^
$keys[$mode][$i][5]]) << 8)
1104 |
(($sbox[6][(($block[1] & 0x000001F8) >> 3) ^
$keys[$mode][$i][6]]) << 4)
1105 |
( $sbox[7][((($block[1] & 0x1F) << 1) |
(($block[1] >> 31) & 1)) ^
$keys[$mode][$i][7]]);
1107 $msb = ($temp >> 31) & 1;
1108 $temp &= 0x7FFFFFFF;
1109 $newBlock = (($temp & 0x00010000) << 15) |
(($temp & 0x02020120) << 5)
1110 |
(($temp & 0x00001800) << 17) |
(($temp & 0x01000000) >> 10)
1111 |
(($temp & 0x00000008) << 24) |
(($temp & 0x00100000) << 6)
1112 |
(($temp & 0x00000010) << 21) |
(($temp & 0x00008000) << 9)
1113 |
(($temp & 0x00000200) << 12) |
(($temp & 0x10000000) >> 27)
1114 |
(($temp & 0x00000040) << 14) |
(($temp & 0x08000000) >> 8)
1115 |
(($temp & 0x00004000) << 4) |
(($temp & 0x00000002) << 16)
1116 |
(($temp & 0x00442000) >> 6) |
(($temp & 0x40800000) >> 15)
1117 |
(($temp & 0x00000001) << 11) |
(($temp & 0x20000000) >> 20)
1118 |
(($temp & 0x00080000) >> 13) |
(($temp & 0x00000004) << 3)
1119 |
(($temp & 0x04000000) >> 22) |
(($temp & 0x00000480) >> 7)
1120 |
(($temp & 0x00200000) >> 19) |
($msb << 23);
1121 // end of "the Feistel (F) function" - $newBlock is F's output
1124 $block[1] = $block[0] ^
$newBlock;
1129 ($block[0] >> 31) & 1,
1130 ($block[1] >> 31) & 1
1132 $block[0] &= 0x7FFFFFFF;
1133 $block[1] &= 0x7FFFFFFF;
1136 (($block[0] & 0x01000004) << 7) |
(($block[1] & 0x01000004) << 6) |
1137 (($block[0] & 0x00010000) << 13) |
(($block[1] & 0x00010000) << 12) |
1138 (($block[0] & 0x00000100) << 19) |
(($block[1] & 0x00000100) << 18) |
1139 (($block[0] & 0x00000001) << 25) |
(($block[1] & 0x00000001) << 24) |
1140 (($block[0] & 0x02000008) >> 2) |
(($block[1] & 0x02000008) >> 3) |
1141 (($block[0] & 0x00020000) << 4) |
(($block[1] & 0x00020000) << 3) |
1142 (($block[0] & 0x00000200) << 10) |
(($block[1] & 0x00000200) << 9) |
1143 (($block[0] & 0x00000002) << 16) |
(($block[1] & 0x00000002) << 15) |
1144 (($block[0] & 0x04000000) >> 11) |
(($block[1] & 0x04000000) >> 12) |
1145 (($block[0] & 0x00040000) >> 5) |
(($block[1] & 0x00040000) >> 6) |
1146 (($block[0] & 0x00000400) << 1) |
( $block[1] & 0x00000400 ) |
1147 (($block[0] & 0x08000000) >> 20) |
(($block[1] & 0x08000000) >> 21) |
1148 (($block[0] & 0x00080000) >> 14) |
(($block[1] & 0x00080000) >> 15) |
1149 (($block[0] & 0x00000800) >> 8) |
(($block[1] & 0x00000800) >> 9)
1151 (($block[0] & 0x10000040) << 3) |
(($block[1] & 0x10000040) << 2) |
1152 (($block[0] & 0x00100000) << 9) |
(($block[1] & 0x00100000) << 8) |
1153 (($block[0] & 0x00001000) << 15) |
(($block[1] & 0x00001000) << 14) |
1154 (($block[0] & 0x00000010) << 21) |
(($block[1] & 0x00000010) << 20) |
1155 (($block[0] & 0x20000080) >> 6) |
(($block[1] & 0x20000080) >> 7) |
1156 ( $block[0] & 0x00200000 ) |
(($block[1] & 0x00200000) >> 1) |
1157 (($block[0] & 0x00002000) << 6) |
(($block[1] & 0x00002000) << 5) |
1158 (($block[0] & 0x00000020) << 12) |
(($block[1] & 0x00000020) << 11) |
1159 (($block[0] & 0x40000000) >> 15) |
(($block[1] & 0x40000000) >> 16) |
1160 (($block[0] & 0x00400000) >> 9) |
(($block[1] & 0x00400000) >> 10) |
1161 (($block[0] & 0x00004000) >> 3) |
(($block[1] & 0x00004000) >> 4) |
1162 (($block[0] & 0x00800000) >> 18) |
(($block[1] & 0x00800000) >> 19) |
1163 (($block[0] & 0x00008000) >> 12) |
(($block[1] & 0x00008000) >> 13) |
1164 ($msb[0] << 7) |
($msb[1] << 6)
1167 return pack('NN', $block[0], $block[1]);
1171 * Creates the key schedule.
1174 * @param String $key
1177 function _prepareKey($key)
1179 static $shifts = array( // number of key bits shifted per round
1180 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1
1183 // pad the key and remove extra characters as appropriate.
1184 $key = str_pad(substr($key, 0, 8), 8, chr(0));
1186 $temp = unpack('Na/Nb', $key);
1187 $key = array($temp['a'], $temp['b']);
1189 ($key[0] >> 31) & 1,
1192 $key[0] &= 0x7FFFFFFF;
1193 $key[1] &= 0x7FFFFFFF;
1196 (($key[1] & 0x00000002) << 26) |
(($key[1] & 0x00000204) << 17) |
1197 (($key[1] & 0x00020408) << 8) |
(($key[1] & 0x02040800) >> 1) |
1198 (($key[0] & 0x00000002) << 22) |
(($key[0] & 0x00000204) << 13) |
1199 (($key[0] & 0x00020408) << 4) |
(($key[0] & 0x02040800) >> 5) |
1200 (($key[1] & 0x04080000) >> 10) |
(($key[0] & 0x04080000) >> 14) |
1201 (($key[1] & 0x08000000) >> 19) |
(($key[0] & 0x08000000) >> 23) |
1202 (($key[0] & 0x00000010) >> 1) |
(($key[0] & 0x00001000) >> 10) |
1203 (($key[0] & 0x00100000) >> 19) |
(($key[0] & 0x10000000) >> 28)
1205 (($key[1] & 0x00000080) << 20) |
(($key[1] & 0x00008000) << 11) |
1206 (($key[1] & 0x00800000) << 2) |
(($key[0] & 0x00000080) << 16) |
1207 (($key[0] & 0x00008000) << 7) |
(($key[0] & 0x00800000) >> 2) |
1208 (($key[1] & 0x00000040) << 13) |
(($key[1] & 0x00004000) << 4) |
1209 (($key[1] & 0x00400000) >> 5) |
(($key[1] & 0x40000000) >> 14) |
1210 (($key[0] & 0x00000040) << 9) |
( $key[0] & 0x00004000 ) |
1211 (($key[0] & 0x00400000) >> 9) |
(($key[0] & 0x40000000) >> 18) |
1212 (($key[1] & 0x00000020) << 6) |
(($key[1] & 0x00002000) >> 3) |
1213 (($key[1] & 0x00200000) >> 12) |
(($key[1] & 0x20000000) >> 21) |
1214 (($key[0] & 0x00000020) << 2) |
(($key[0] & 0x00002000) >> 7) |
1215 (($key[0] & 0x00200000) >> 16) |
(($key[0] & 0x20000000) >> 25) |
1216 (($key[1] & 0x00000010) >> 1) |
(($key[1] & 0x00001000) >> 10) |
1217 (($key[1] & 0x00100000) >> 19) |
(($key[1] & 0x10000000) >> 28) |
1218 ($msb[1] << 24) |
($msb[0] << 20)
1222 for ($i = 0; $i < 16; $i++
) {
1223 $key[0] <<= $shifts[$i];
1224 $temp = ($key[0] & 0xF0000000) >> 28;
1225 $key[0] = ($key[0] |
$temp) & 0x0FFFFFFF;
1227 $key[1] <<= $shifts[$i];
1228 $temp = ($key[1] & 0xF0000000) >> 28;
1229 $key[1] = ($key[1] |
$temp) & 0x0FFFFFFF;
1232 (($key[1] & 0x00004000) >> 9) |
(($key[1] & 0x00000800) >> 7) |
1233 (($key[1] & 0x00020000) >> 14) |
(($key[1] & 0x00000010) >> 2) |
1234 (($key[1] & 0x08000000) >> 26) |
(($key[1] & 0x00800000) >> 23)
1236 (($key[1] & 0x02400000) >> 20) |
(($key[1] & 0x00000001) << 4) |
1237 (($key[1] & 0x00002000) >> 10) |
(($key[1] & 0x00040000) >> 18) |
1238 (($key[1] & 0x00000080) >> 6)
1240 ( $key[1] & 0x00000020 ) |
(($key[1] & 0x00000200) >> 5) |
1241 (($key[1] & 0x00010000) >> 13) |
(($key[1] & 0x01000000) >> 22) |
1242 (($key[1] & 0x00000004) >> 1) |
(($key[1] & 0x00100000) >> 20)
1244 (($key[1] & 0x00001000) >> 7) |
(($key[1] & 0x00200000) >> 17) |
1245 (($key[1] & 0x00000002) << 2) |
(($key[1] & 0x00000100) >> 6) |
1246 (($key[1] & 0x00008000) >> 14) |
(($key[1] & 0x04000000) >> 26)
1248 (($key[0] & 0x00008000) >> 10) |
( $key[0] & 0x00000010 ) |
1249 (($key[0] & 0x02000000) >> 22) |
(($key[0] & 0x00080000) >> 17) |
1250 (($key[0] & 0x00000200) >> 8) |
(($key[0] & 0x00000002) >> 1)
1252 (($key[0] & 0x04000000) >> 21) |
(($key[0] & 0x00010000) >> 12) |
1253 (($key[0] & 0x00000020) >> 2) |
(($key[0] & 0x00000800) >> 9) |
1254 (($key[0] & 0x00800000) >> 22) |
(($key[0] & 0x00000100) >> 8)
1256 (($key[0] & 0x00001000) >> 7) |
(($key[0] & 0x00000088) >> 3) |
1257 (($key[0] & 0x00020000) >> 14) |
(($key[0] & 0x00000001) << 2) |
1258 (($key[0] & 0x00400000) >> 21)
1260 (($key[0] & 0x00000400) >> 5) |
(($key[0] & 0x00004000) >> 10) |
1261 (($key[0] & 0x00000040) >> 3) |
(($key[0] & 0x00100000) >> 18) |
1262 (($key[0] & 0x08000000) >> 26) |
(($key[0] & 0x01000000) >> 24)
1269 CRYPT_DES_ENCRYPT
=> $keys,
1270 CRYPT_DES_DECRYPT
=> array_reverse($keys)
1279 * Inspired by array_shift
1281 * @param String $string
1282 * @param optional Integer $index
1286 function _string_shift(&$string, $index = 1)
1288 $substr = substr($string, 0, $index);
1289 $string = substr($string, $index);
1294 // vim: ts=4:sw=4:et: