1 | <?php |
||
22 | class PasswordEncoder |
||
23 | { |
||
24 | const ALGORITHM_MD2 = 'MD2'; |
||
25 | const ALGORITHM_MD4 = 'MD4'; |
||
26 | const ALGORITHM_MD5 = 'MD5'; |
||
27 | const ALGORITHM_SHA_1 = 'SHA-1'; |
||
28 | const ALGORITHM_SHA_256 = 'SHA-256'; |
||
29 | const ALGORITHM_SHA_384 = 'SHA-384'; |
||
30 | const ALGORITHM_SHA_512 = 'SHA-512'; |
||
31 | const ALGORITHM_RIPEMD = 'RIPEMD'; |
||
32 | const ALGORITHM_RIPEMD_160 = 'RIPEMD-160'; |
||
33 | const ALGORITHM_MAC = 'MAC'; |
||
34 | const ALGORITHM_HMAC = 'HMAC'; |
||
35 | |||
36 | /** |
||
37 | * Mapping between algorithm name and algorithm ID |
||
38 | * |
||
39 | * @var array |
||
40 | * @see https://msdn.microsoft.com/en-us/library/documentformat.openxml.wordprocessing.writeprotection.cryptographicalgorithmsid(v=office.14).aspx |
||
41 | */ |
||
42 | private static $algorithmMapping = array( |
||
43 | self::ALGORITHM_MD2 => array(1, 'md2'), |
||
44 | self::ALGORITHM_MD4 => array(2, 'md4'), |
||
45 | self::ALGORITHM_MD5 => array(3, 'md5'), |
||
46 | self::ALGORITHM_SHA_1 => array(4, 'sha1'), |
||
47 | self::ALGORITHM_MAC => array(5, ''), // 'mac' -> not possible with hash() |
||
48 | self::ALGORITHM_RIPEMD => array(6, 'ripemd'), |
||
49 | self::ALGORITHM_RIPEMD_160 => array(7, 'ripemd160'), |
||
50 | self::ALGORITHM_HMAC => array(9, ''), //'hmac' -> not possible with hash() |
||
51 | self::ALGORITHM_SHA_256 => array(12, 'sha256'), |
||
52 | self::ALGORITHM_SHA_384 => array(13, 'sha384'), |
||
53 | self::ALGORITHM_SHA_512 => array(14, 'sha512'), |
||
54 | ); |
||
55 | |||
56 | private static $initialCodeArray = array( |
||
57 | 0xE1F0, |
||
58 | 0x1D0F, |
||
59 | 0xCC9C, |
||
60 | 0x84C0, |
||
61 | 0x110C, |
||
62 | 0x0E10, |
||
63 | 0xF1CE, |
||
64 | 0x313E, |
||
65 | 0x1872, |
||
66 | 0xE139, |
||
67 | 0xD40F, |
||
68 | 0x84F9, |
||
69 | 0x280C, |
||
70 | 0xA96A, |
||
71 | 0x4EC3, |
||
72 | ); |
||
73 | |||
74 | private static $encryptionMatrix = array( |
||
75 | array(0xAEFC, 0x4DD9, 0x9BB2, 0x2745, 0x4E8A, 0x9D14, 0x2A09), |
||
76 | array(0x7B61, 0xF6C2, 0xFDA5, 0xEB6B, 0xC6F7, 0x9DCF, 0x2BBF), |
||
77 | array(0x4563, 0x8AC6, 0x05AD, 0x0B5A, 0x16B4, 0x2D68, 0x5AD0), |
||
78 | array(0x0375, 0x06EA, 0x0DD4, 0x1BA8, 0x3750, 0x6EA0, 0xDD40), |
||
79 | array(0xD849, 0xA0B3, 0x5147, 0xA28E, 0x553D, 0xAA7A, 0x44D5), |
||
80 | array(0x6F45, 0xDE8A, 0xAD35, 0x4A4B, 0x9496, 0x390D, 0x721A), |
||
81 | array(0xEB23, 0xC667, 0x9CEF, 0x29FF, 0x53FE, 0xA7FC, 0x5FD9), |
||
82 | array(0x47D3, 0x8FA6, 0x0F6D, 0x1EDA, 0x3DB4, 0x7B68, 0xF6D0), |
||
83 | array(0xB861, 0x60E3, 0xC1C6, 0x93AD, 0x377B, 0x6EF6, 0xDDEC), |
||
84 | array(0x45A0, 0x8B40, 0x06A1, 0x0D42, 0x1A84, 0x3508, 0x6A10), |
||
85 | array(0xAA51, 0x4483, 0x8906, 0x022D, 0x045A, 0x08B4, 0x1168), |
||
86 | array(0x76B4, 0xED68, 0xCAF1, 0x85C3, 0x1BA7, 0x374E, 0x6E9C), |
||
87 | array(0x3730, 0x6E60, 0xDCC0, 0xA9A1, 0x4363, 0x86C6, 0x1DAD), |
||
88 | array(0x3331, 0x6662, 0xCCC4, 0x89A9, 0x0373, 0x06E6, 0x0DCC), |
||
89 | array(0x1021, 0x2042, 0x4084, 0x8108, 0x1231, 0x2462, 0x48C4), |
||
90 | ); |
||
91 | |||
92 | private static $passwordMaxLength = 15; |
||
93 | |||
94 | /** |
||
95 | * Create a hashed password that MS Word will be able to work with |
||
96 | * @see https://blogs.msdn.microsoft.com/vsod/2010/04/05/how-to-set-the-editing-restrictions-in-word-using-open-xml-sdk-2-0/ |
||
97 | * |
||
98 | * @param string $password |
||
99 | * @param string $algorithmName |
||
100 | * @param string $salt |
||
101 | * @param int $spinCount |
||
102 | * @return string |
||
103 | */ |
||
104 | 4 | public static function hashPassword($password, $algorithmName = self::ALGORITHM_SHA_1, $salt = null, $spinCount = 10000) |
|
147 | |||
148 | /** |
||
149 | * Get algorithm from self::$algorithmMapping |
||
150 | * |
||
151 | * @param string $algorithmName |
||
152 | * @return string |
||
153 | */ |
||
154 | 4 | private static function getAlgorithm($algorithmName) |
|
163 | |||
164 | /** |
||
165 | * Returns the algorithm ID |
||
166 | * |
||
167 | * @param string $algorithmName |
||
168 | * @return int |
||
169 | */ |
||
170 | public static function getAlgorithmId($algorithmName) |
||
174 | |||
175 | /** |
||
176 | * Build combined key from low-order word and high-order word |
||
177 | * |
||
178 | * @param array $byteChars byte array representation of password |
||
179 | * @return int |
||
180 | */ |
||
181 | 4 | private static function buildCombinedKey($byteChars) |
|
216 | |||
217 | /** |
||
218 | * Simulate behaviour of (signed) int32 |
||
219 | * |
||
220 | * @codeCoverageIgnore |
||
221 | * @param int $value |
||
222 | * @return int |
||
223 | */ |
||
224 | private static function int32($value) |
||
234 | } |
||
235 |