zhaojs
2023-08-01 cc6247528b559cf6a9468591bb889bb58dc14199
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
 
namespace PhpZip\Constants;
 
use PhpZip\Exception\InvalidArgumentException;
 
/**
 * Class ZipEncryptionMethod.
 */
final class ZipEncryptionMethod
{
    const NONE = -1;
 
    /** @var int Traditional PKWARE encryption. */
    const PKWARE = 0;
 
    /** @var int WinZip AES-256 */
    const WINZIP_AES_256 = 1;
 
    /** @var int WinZip AES-128 */
    const WINZIP_AES_128 = 2;
 
    /** @var int WinZip AES-192 */
    const WINZIP_AES_192 = 3;
 
    /** @var array<int, string> */
    private static $ENCRYPTION_METHODS = [
        self::NONE => 'no encryption',
        self::PKWARE => 'Traditional PKWARE encryption',
        self::WINZIP_AES_128 => 'WinZip AES-128',
        self::WINZIP_AES_192 => 'WinZip AES-192',
        self::WINZIP_AES_256 => 'WinZip AES-256',
    ];
 
    /**
     * @param int $value
     *
     * @return string
     */
    public static function getEncryptionMethodName($value)
    {
        $value = (int) $value;
 
        return isset(self::$ENCRYPTION_METHODS[$value]) ?
            self::$ENCRYPTION_METHODS[$value] :
            'Unknown Encryption Method';
    }
 
    /**
     * @param int $encryptionMethod
     *
     * @return bool
     */
    public static function hasEncryptionMethod($encryptionMethod)
    {
        return isset(self::$ENCRYPTION_METHODS[$encryptionMethod]);
    }
 
    /**
     * @param int $encryptionMethod
     *
     * @return bool
     */
    public static function isWinZipAesMethod($encryptionMethod)
    {
        return \in_array(
            (int) $encryptionMethod,
            [
                self::WINZIP_AES_256,
                self::WINZIP_AES_192,
                self::WINZIP_AES_128,
            ],
            true
        );
    }
 
    /**
     * @param int $encryptionMethod
     *
     * @throws InvalidArgumentException
     */
    public static function checkSupport($encryptionMethod)
    {
        $encryptionMethod = (int) $encryptionMethod;
 
        if (!self::hasEncryptionMethod($encryptionMethod)) {
            throw new InvalidArgumentException(sprintf(
                'Encryption method %d is not supported.',
                $encryptionMethod
            ));
        }
    }
}