mirror of
https://github.com/Ne-Lexa/php-zip.git
synced 2025-10-19 00:56:25 +02:00
Add ZipModel for all changes.
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace PhpZip\Crypto;
|
||||
|
||||
use PhpZip\Exception\ZipAuthenticationException;
|
||||
use PhpZip\Exception\ZipCryptoException;
|
||||
use PhpZip\Model\ZipEntry;
|
||||
use PhpZip\Util\CryptoUtil;
|
||||
use PhpZip\Util\PackUtil;
|
||||
|
||||
/**
|
||||
* Traditional PKWARE Encryption Engine.
|
||||
@@ -13,7 +15,7 @@ use PhpZip\Util\CryptoUtil;
|
||||
* @author Ne-Lexa alexey@nelexa.ru
|
||||
* @license MIT
|
||||
*/
|
||||
class TraditionalPkwareEncryptionEngine implements CryptoEngine
|
||||
class TraditionalPkwareEncryptionEngine implements ZipEncryptionEngine
|
||||
{
|
||||
/**
|
||||
* Encryption header size
|
||||
@@ -66,7 +68,6 @@ class TraditionalPkwareEncryptionEngine implements CryptoEngine
|
||||
* @var array
|
||||
*/
|
||||
private $keys = [];
|
||||
|
||||
/**
|
||||
* @var ZipEntry
|
||||
*/
|
||||
@@ -80,7 +81,6 @@ class TraditionalPkwareEncryptionEngine implements CryptoEngine
|
||||
public function __construct(ZipEntry $entry)
|
||||
{
|
||||
$this->entry = $entry;
|
||||
$this->initKeys($entry->getPassword());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -107,25 +107,8 @@ class TraditionalPkwareEncryptionEngine implements CryptoEngine
|
||||
{
|
||||
$this->keys[0] = self::crc32($this->keys[0], $charAt);
|
||||
$this->keys[1] = $this->keys[1] + ($this->keys[0] & 0xff);
|
||||
$this->keys[1] = self::toInt($this->keys[1] * 134775813 + 1);
|
||||
$this->keys[2] = self::toInt(self::crc32($this->keys[2], ($this->keys[1] >> 24) & 0xff));
|
||||
}
|
||||
|
||||
/**
|
||||
* Cast to int
|
||||
*
|
||||
* @param $i
|
||||
* @return int
|
||||
*/
|
||||
private static function toInt($i)
|
||||
{
|
||||
$i = (int)($i & 0xffffffff);
|
||||
if ($i > 2147483647) {
|
||||
return -(-$i & 0xffffffff);
|
||||
} elseif ($i < -2147483648) {
|
||||
return $i & -2147483648;
|
||||
}
|
||||
return $i;
|
||||
$this->keys[1] = PackUtil::toSignedInt32($this->keys[1] * 134775813 + 1);
|
||||
$this->keys[2] = PackUtil::toSignedInt32(self::crc32($this->keys[2], ($this->keys[1] >> 24) & 0xff));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -147,7 +130,11 @@ class TraditionalPkwareEncryptionEngine implements CryptoEngine
|
||||
*/
|
||||
public function decrypt($content)
|
||||
{
|
||||
$password = $this->entry->getPassword();
|
||||
$this->initKeys($password);
|
||||
|
||||
$headerBytes = array_values(unpack('C*', substr($content, 0, self::STD_DEC_HDR_SIZE)));
|
||||
$byte = 0;
|
||||
foreach ($headerBytes as &$byte) {
|
||||
$byte = ($byte ^ $this->decryptByte()) & 0xff;
|
||||
$this->updateKeys($byte);
|
||||
@@ -198,7 +185,9 @@ class TraditionalPkwareEncryptionEngine implements CryptoEngine
|
||||
$headerBytes = CryptoUtil::randomBytes(self::STD_DEC_HDR_SIZE);
|
||||
|
||||
// Initialize again since the generated bytes were encrypted.
|
||||
$this->initKeys($this->entry->getPassword());
|
||||
$password = $this->entry->getPassword();
|
||||
$this->initKeys($password);
|
||||
|
||||
$headerBytes[self::STD_DEC_HDR_SIZE - 1] = pack('c', ($crc >> 24) & 0xff);
|
||||
$headerBytes[self::STD_DEC_HDR_SIZE - 2] = pack('c', ($crc >> 16) & 0xff);
|
||||
|
||||
@@ -233,4 +222,4 @@ class TraditionalPkwareEncryptionEngine implements CryptoEngine
|
||||
$this->updateKeys($byte);
|
||||
return $tempVal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user