Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ParagonIE_Sodium_Compat often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ParagonIE_Sodium_Compat, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class ParagonIE_Sodium_Compat |
||
29 | { |
||
30 | /** |
||
31 | * This parameter prevents the use of the PECL extension. |
||
32 | * It should only be used for unit testing. |
||
33 | * |
||
34 | * @var bool |
||
35 | */ |
||
36 | public static $disableFallbackForUnitTests = false; |
||
37 | |||
38 | /** |
||
39 | * Use fast multiplication rather than our constant-time multiplication |
||
40 | * implementation. Can be enabled at runtime. Only enable this if you |
||
41 | * are absolutely certain that there is no timing leak on your platform. |
||
42 | * |
||
43 | * @var bool |
||
44 | */ |
||
45 | public static $fastMult = false; |
||
46 | |||
47 | const LIBRARY_VERSION_MAJOR = 9; |
||
48 | const LIBRARY_VERSION_MINOR = 1; |
||
49 | const VERSION_STRING = 'polyfill-1.0.8'; |
||
50 | |||
51 | // From libsodium |
||
52 | const CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES = 32; |
||
53 | const CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES = 0; |
||
54 | const CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES = 8; |
||
55 | const CRYPTO_AEAD_CHACHA20POLY1305_ABYTES = 16; |
||
56 | const CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES = 32; |
||
57 | const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES = 0; |
||
58 | const CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES = 12; |
||
59 | const CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES = 16; |
||
60 | const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES = 32; |
||
61 | const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NSECBYTES = 0; |
||
62 | const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES = 24; |
||
63 | const CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES = 16; |
||
64 | const CRYPTO_AUTH_BYTES = 32; |
||
65 | const CRYPTO_AUTH_KEYBYTES = 32; |
||
66 | const CRYPTO_BOX_SEALBYTES = 16; |
||
67 | const CRYPTO_BOX_SECRETKEYBYTES = 32; |
||
68 | const CRYPTO_BOX_PUBLICKEYBYTES = 32; |
||
69 | const CRYPTO_BOX_KEYPAIRBYTES = 64; |
||
70 | const CRYPTO_BOX_MACBYTES = 16; |
||
71 | const CRYPTO_BOX_NONCEBYTES = 24; |
||
72 | const CRYPTO_BOX_SEEDBYTES = 32; |
||
73 | const CRYPTO_KX_BYTES = 32; |
||
74 | const CRYPTO_KX_PUBLICKEYBYTES = 32; |
||
75 | const CRYPTO_KX_SECRETKEYBYTES = 32; |
||
76 | const CRYPTO_GENERICHASH_BYTES = 32; |
||
77 | const CRYPTO_GENERICHASH_BYTES_MIN = 16; |
||
78 | const CRYPTO_GENERICHASH_BYTES_MAX = 64; |
||
79 | const CRYPTO_GENERICHASH_KEYBYTES = 32; |
||
80 | const CRYPTO_GENERICHASH_KEYBYTES_MIN = 16; |
||
81 | const CRYPTO_GENERICHASH_KEYBYTES_MAX = 64; |
||
82 | const CRYPTO_PWHASH_SALTBYTES = 16; |
||
83 | const CRYPTO_PWHASH_STRPREFIX = '$argon2i$'; |
||
84 | const CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE = 33554432; |
||
85 | const CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE = 4; |
||
86 | const CRYPTO_PWHASH_MEMLIMIT_MODERATE = 134217728; |
||
87 | const CRYPTO_PWHASH_OPSLIMIT_MODERATE = 6; |
||
88 | const CRYPTO_PWHASH_MEMLIMIT_SENSITIVE = 536870912; |
||
89 | const CRYPTO_PWHASH_OPSLIMIT_SENSITIVE = 8; |
||
90 | const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES = 32; |
||
91 | const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX = '$7$'; |
||
92 | const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE = 534288; |
||
93 | const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE = 16777216; |
||
94 | const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_SENSITIVE = 33554432; |
||
95 | const CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_SENSITIVE = 1073741824; |
||
96 | const CRYPTO_SCALARMULT_BYTES = 32; |
||
97 | const CRYPTO_SCALARMULT_SCALARBYTES = 32; |
||
98 | const CRYPTO_SHORTHASH_BYTES = 8; |
||
99 | const CRYPTO_SHORTHASH_KEYBYTES = 16; |
||
100 | const CRYPTO_SECRETBOX_KEYBYTES = 32; |
||
101 | const CRYPTO_SECRETBOX_MACBYTES = 16; |
||
102 | const CRYPTO_SECRETBOX_NONCEBYTES = 24; |
||
103 | const CRYPTO_SIGN_BYTES = 64; |
||
104 | const CRYPTO_SIGN_SEEDBYTES = 32; |
||
105 | const CRYPTO_SIGN_PUBLICKEYBYTES = 32; |
||
106 | const CRYPTO_SIGN_SECRETKEYBYTES = 64; |
||
107 | const CRYPTO_SIGN_KEYPAIRBYTES = 96; |
||
108 | const CRYPTO_STREAM_KEYBYTES = 32; |
||
109 | const CRYPTO_STREAM_NONCEBYTES = 24; |
||
110 | |||
111 | /** |
||
112 | * Cache-timing-safe implementation of bin2hex(). |
||
113 | * |
||
114 | * @param string $string A string (probably raw binary) |
||
115 | * @return string A hexadecimal-encoded string |
||
116 | * @throws TypeError |
||
117 | */ |
||
118 | View Code Duplication | public static function bin2hex($string) |
|
131 | |||
132 | /** |
||
133 | * Compare two strings, in constant-time. |
||
134 | * Compared to memcmp(), compare() is more useful for sorting. |
||
135 | * |
||
136 | * @param string $left The left operand; must be a string |
||
137 | * @param string $right The right operand; must be a string |
||
138 | * @return int < 0 if the left operand is less than the right |
||
139 | * 0 if both strings are equal |
||
140 | * > 0 if the right operand is less than the left |
||
141 | * @throws TypeError |
||
142 | */ |
||
143 | View Code Duplication | public static function compare($left, $right) |
|
157 | |||
158 | /** |
||
159 | * Authenticated Encryption with Associated Data: Decryption |
||
160 | * |
||
161 | * Algorithm: |
||
162 | * ChaCha20-Poly1305 |
||
163 | * |
||
164 | * This mode uses a 64-bit random nonce with a 64-bit counter. |
||
165 | * IETF mode uses a 96-bit random nonce with a 32-bit counter. |
||
166 | * |
||
167 | * @param string $ciphertext Encrypted message (with Poly1305 MAC appended) |
||
168 | * @param string $assocData Authenticated Associated Data (unencrypted) |
||
169 | * @param string $nonce Number to be used only Once; must be 8 bytes |
||
170 | * @param string $key Encryption key |
||
171 | * |
||
172 | * @return string The original plaintext message |
||
173 | * @throws Error |
||
174 | * @throws TypeError |
||
175 | */ |
||
176 | View Code Duplication | public static function crypto_aead_chacha20poly1305_decrypt( |
|
231 | |||
232 | /** |
||
233 | * Authenticated Encryption with Associated Data |
||
234 | * |
||
235 | * Algorithm: |
||
236 | * ChaCha20-Poly1305 |
||
237 | * |
||
238 | * This mode uses a 64-bit random nonce with a 64-bit counter. |
||
239 | * IETF mode uses a 96-bit random nonce with a 32-bit counter. |
||
240 | * |
||
241 | * @param string $plaintext Message to be encrypted |
||
242 | * @param string $assocData Authenticated Associated Data (unencrypted) |
||
243 | * @param string $nonce Number to be used only Once; must be 8 bytes |
||
244 | * @param string $key Encryption key |
||
245 | * |
||
246 | * @return string Ciphertext with a 16-byte Poly1305 message |
||
247 | * authentication code appended |
||
248 | * @throws Error |
||
249 | * @throws TypeError |
||
250 | */ |
||
251 | View Code Duplication | public static function crypto_aead_chacha20poly1305_encrypt( |
|
303 | |||
304 | /** |
||
305 | * Authenticated Encryption with Associated Data: Decryption |
||
306 | * |
||
307 | * Algorithm: |
||
308 | * ChaCha20-Poly1305 |
||
309 | * |
||
310 | * IETF mode uses a 96-bit random nonce with a 32-bit counter. |
||
311 | * Regular mode uses a 64-bit random nonce with a 64-bit counter. |
||
312 | * |
||
313 | * @param string $ciphertext Encrypted message (with Poly1305 MAC appended) |
||
314 | * @param string $assocData Authenticated Associated Data (unencrypted) |
||
315 | * @param string $nonce Number to be used only Once; must be 12 bytes |
||
316 | * @param string $key Encryption key |
||
317 | * |
||
318 | * @return string The original plaintext message |
||
319 | * @throws Error |
||
320 | * @throws TypeError |
||
321 | */ |
||
322 | View Code Duplication | public static function crypto_aead_chacha20poly1305_ietf_decrypt( |
|
377 | |||
378 | /** |
||
379 | * Authenticated Encryption with Associated Data |
||
380 | * |
||
381 | * Algorithm: |
||
382 | * ChaCha20-Poly1305 |
||
383 | * |
||
384 | * IETF mode uses a 96-bit random nonce with a 32-bit counter. |
||
385 | * Regular mode uses a 64-bit random nonce with a 64-bit counter. |
||
386 | * |
||
387 | * @param string $plaintext Message to be encrypted |
||
388 | * @param string $assocData Authenticated Associated Data (unencrypted) |
||
389 | * @param string $nonce Number to be used only Once; must be 8 bytes |
||
390 | * @param string $key Encryption key |
||
391 | * |
||
392 | * @return string Ciphertext with a 16-byte Poly1305 message |
||
393 | * authentication code appended |
||
394 | * @throws Error |
||
395 | * @throws TypeError |
||
396 | */ |
||
397 | View Code Duplication | public static function crypto_aead_chacha20poly1305_ietf_encrypt( |
|
449 | |||
450 | /** |
||
451 | * Authenticated Encryption with Associated Data: Decryption |
||
452 | * |
||
453 | * Algorithm: |
||
454 | * XChaCha20-Poly1305 |
||
455 | * |
||
456 | * This mode uses a 64-bit random nonce with a 64-bit counter. |
||
457 | * IETF mode uses a 96-bit random nonce with a 32-bit counter. |
||
458 | * |
||
459 | * @param string $ciphertext Encrypted message (with Poly1305 MAC appended) |
||
460 | * @param string $assocData Authenticated Associated Data (unencrypted) |
||
461 | * @param string $nonce Number to be used only Once; must be 8 bytes |
||
462 | * @param string $key Encryption key |
||
463 | * |
||
464 | * @return string The original plaintext message |
||
465 | * @throws Error |
||
466 | * @throws TypeError |
||
467 | */ |
||
468 | public static function crypto_aead_xchacha20poly1305_ietf_decrypt( |
||
506 | |||
507 | /** |
||
508 | * Authenticated Encryption with Associated Data |
||
509 | * |
||
510 | * Algorithm: |
||
511 | * XChaCha20-Poly1305 |
||
512 | * |
||
513 | * This mode uses a 64-bit random nonce with a 64-bit counter. |
||
514 | * IETF mode uses a 96-bit random nonce with a 32-bit counter. |
||
515 | * |
||
516 | * @param string $plaintext Message to be encrypted |
||
517 | * @param string $assocData Authenticated Associated Data (unencrypted) |
||
518 | * @param string $nonce Number to be used only Once; must be 8 bytes |
||
519 | * @param string $key Encryption key |
||
520 | * |
||
521 | * @return string Ciphertext with a 16-byte Poly1305 message |
||
522 | * authentication code appended |
||
523 | * @throws Error |
||
524 | * @throws TypeError |
||
525 | */ |
||
526 | public static function crypto_aead_xchacha20poly1305_ietf_encrypt( |
||
561 | |||
562 | /** |
||
563 | * Authenticate a message. Uses symmetric-key cryptography. |
||
564 | * |
||
565 | * Algorithm: |
||
566 | * HMAC-SHA512-256. Which is HMAC-SHA-512 truncated to 256 bits. |
||
567 | * Not to be confused with HMAC-SHA-512/256 which would use the |
||
568 | * SHA-512/256 hash function (uses different initial parameters |
||
569 | * but still truncates to 256 bits to sidestep length-extension |
||
570 | * attacks). |
||
571 | * |
||
572 | * @param string $message Message to be authenticated |
||
573 | * @param string $key Symmetric authentication key |
||
574 | * @return string Message authentication code |
||
575 | * @throws Error |
||
576 | * @throws TypeError |
||
577 | */ |
||
578 | View Code Duplication | public static function crypto_auth($message, $key) |
|
600 | |||
601 | /** |
||
602 | * Verify the MAC of a message previously authenticated with crypto_auth. |
||
603 | * |
||
604 | * @param string $mac Message authentication code |
||
605 | * @param string $message Message whose authenticity you are attempting to |
||
606 | * verify (with a given MAC and key) |
||
607 | * @param string $key Symmetric authentication key |
||
608 | * @return bool TRUE if authenticated, FALSE otherwise |
||
609 | * @throws Error |
||
610 | * @throws TypeError |
||
611 | */ |
||
612 | View Code Duplication | public static function crypto_auth_verify($mac, $message, $key) |
|
638 | |||
639 | /** |
||
640 | * Authenticated asymmetric-key encryption. Both the sender and recipient |
||
641 | * may decrypt messages. |
||
642 | * |
||
643 | * Algorithm: X25519-XSalsa20-Poly1305. |
||
644 | * X25519: Elliptic-Curve Diffie Hellman over Curve25519. |
||
645 | * XSalsa20: Extended-nonce variant of salsa20. |
||
646 | * Poyl1305: Polynomial MAC for one-time message authentication. |
||
647 | * |
||
648 | * @param string $plaintext The message to be encrypted |
||
649 | * @param string $nonce A Number to only be used Once; must be 24 bytes |
||
650 | * @param string $keypair Your secret key and your recipient's public key |
||
651 | * @return string Ciphertext with 16-byte Poly1305 MAC |
||
652 | * @throws Error |
||
653 | * @throws TypeError |
||
654 | */ |
||
655 | View Code Duplication | public static function crypto_box($plaintext, $nonce, $keypair) |
|
681 | |||
682 | /** |
||
683 | * Anonymous public-key encryption. Only the recipient may decrypt messages. |
||
684 | * |
||
685 | * Algorithm: X25519-XSalsa20-Poly1305, as with crypto_box. |
||
686 | * The sender's X25519 keypair is ephemeral. |
||
687 | * Nonce is generated from the BLAKE2b hash of both public keys. |
||
688 | * |
||
689 | * This provides ciphertext integrity. |
||
690 | * |
||
691 | * @param string $plaintext Message to be sealed |
||
692 | * @param string $publicKey Your recipient's public key |
||
693 | * @return string Sealed message that only your recipient can |
||
694 | * decrypt |
||
695 | * @throws Error |
||
696 | * @throws TypeError |
||
697 | */ |
||
698 | View Code Duplication | public static function crypto_box_seal($plaintext, $publicKey) |
|
720 | |||
721 | /** |
||
722 | * Opens a message encrypted with crypto_box_seal(). Requires |
||
723 | * the recipient's keypair (sk || pk) to decrypt successfully. |
||
724 | * |
||
725 | * This validates ciphertext integrity. |
||
726 | * |
||
727 | * @param string $ciphertext Sealed message to be opened |
||
728 | * @param string $keypair Your crypto_box keypair |
||
729 | * @return string The original plaintext message |
||
730 | * @throws Error |
||
731 | * @throws TypeError |
||
732 | */ |
||
733 | View Code Duplication | public static function crypto_box_seal_open($ciphertext, $keypair) |
|
755 | |||
756 | /** |
||
757 | * Generate a new random X25519 keypair. |
||
758 | * |
||
759 | * @return string A 64-byte string; the first 32 are your secret key, while |
||
760 | * the last 32 are your public key. crypto_box_secretkey() |
||
761 | * and crypto_box_publickey() exist to separate them so you |
||
762 | * don't accidentally get them mixed up! |
||
763 | */ |
||
764 | public static function crypto_box_keypair() |
||
774 | |||
775 | /** |
||
776 | * Combine two keys into a keypair for use in library methods that expect |
||
777 | * a keypair. This doesn't necessarily have to be the same person's keys. |
||
778 | * |
||
779 | * @param string $secretKey Secret key |
||
780 | * @param string $publicKey Public key |
||
781 | * @return string Keypair |
||
782 | * @throws Error |
||
783 | * @throws TypeError |
||
784 | */ |
||
785 | View Code Duplication | public static function crypto_box_keypair_from_secretkey_and_publickey($secretKey, $publicKey) |
|
810 | |||
811 | /** |
||
812 | * Decrypt a message previously encrypted with crypto_box(). |
||
813 | * |
||
814 | * @param string $ciphertext Encrypted message |
||
815 | * @param string $nonce Number to only be used Once; must be 24 bytes |
||
816 | * @param string $keypair Your secret key and the sender's public key |
||
817 | * @return string The original plaintext message |
||
818 | * @throws Error |
||
819 | * @throws TypeError |
||
820 | */ |
||
821 | public static function crypto_box_open($ciphertext, $nonce, $keypair) |
||
850 | |||
851 | /** |
||
852 | * Extract the public key from a crypto_box keypair. |
||
853 | * |
||
854 | * @param string $keypair |
||
855 | * @return string Your crypto_box public key |
||
856 | * @throws Error |
||
857 | * @throws TypeError |
||
858 | */ |
||
859 | View Code Duplication | public static function crypto_box_publickey($keypair) |
|
880 | |||
881 | /** |
||
882 | * Calculate the X25519 public key from a given X25519 secret key. |
||
883 | * |
||
884 | * @param string $secretKey Any X25519 secret key |
||
885 | * @return string The corresponding X25519 public key |
||
886 | * @throws Error |
||
887 | * @throws TypeError |
||
888 | */ |
||
889 | View Code Duplication | public static function crypto_box_publickey_from_secretkey($secretKey) |
|
910 | |||
911 | /** |
||
912 | * Extract the secret key from a crypto_box keypair. |
||
913 | * |
||
914 | * @param string $keypair |
||
915 | * @return string Your crypto_box secret key |
||
916 | * @throws Error |
||
917 | * @throws TypeError |
||
918 | */ |
||
919 | View Code Duplication | public static function crypto_box_secretkey($keypair) |
|
940 | |||
941 | /** |
||
942 | * Generate an X25519 keypair from a seed. |
||
943 | * |
||
944 | * @param string $seed |
||
945 | * @return string |
||
946 | */ |
||
947 | View Code Duplication | public static function crypto_box_seed_keypair($seed) |
|
960 | |||
961 | /** |
||
962 | * Calculates a BLAKE2b hash, with an optional key. |
||
963 | * |
||
964 | * @param string $message The message to be hashed |
||
965 | * @param string $key If specified, must be a string between 16 and 64 |
||
966 | * bytes long |
||
967 | * @param int $length Output length in bytes; must be between 16 and 64 |
||
968 | * (default = 32) |
||
969 | * @return string Raw binary |
||
970 | * @throws Error |
||
971 | * @throws TypeError |
||
972 | */ |
||
973 | public static function crypto_generichash($message, $key = '', $length = self::CRYPTO_GENERICHASH_BYTES) |
||
1004 | |||
1005 | /** |
||
1006 | * Get the final BLAKE2b hash output for a given context. |
||
1007 | * |
||
1008 | * @param string &$ctx BLAKE2 hashing context. Generated by crypto_generichash_init(). |
||
1009 | * @param int $length Hash output size. |
||
1010 | * @return string Final BLAKE2b hash. |
||
1011 | * @throws Error |
||
1012 | * @throws TypeError |
||
1013 | */ |
||
1014 | public static function crypto_generichash_final(&$ctx, $length = self::CRYPTO_GENERICHASH_BYTES) |
||
1039 | |||
1040 | /** |
||
1041 | * Initialize a BLAKE2b hashing context, for use in a streaming interface. |
||
1042 | * |
||
1043 | * @param string $key If specified must be a string between 16 and 64 bytes |
||
1044 | * @param int $length The size of the desired hash output |
||
1045 | * @return string A BLAKE2 hashing context, encoded as a string |
||
1046 | * (To be 100% compatible with ext/libsodium) |
||
1047 | * @throws Error |
||
1048 | * @throws TypeError |
||
1049 | */ |
||
1050 | public static function crypto_generichash_init($key = '', $length = self::CRYPTO_GENERICHASH_BYTES) |
||
1080 | |||
1081 | /** |
||
1082 | * Update a BLAKE2b hashing context with additional data. |
||
1083 | * |
||
1084 | * @param string &$ctx BLAKE2 hashing context. Generated by crypto_generichash_init(). |
||
1085 | * $ctx is passed by reference and gets updated in-place. |
||
1086 | * @param string $message The message to append to the existing hash state. |
||
1087 | * @return void |
||
1088 | * @throws TypeError |
||
1089 | */ |
||
1090 | View Code Duplication | public static function crypto_generichash_update(&$ctx, $message) |
|
1111 | |||
1112 | /** |
||
1113 | * Perform a key exchange, between a designated client and a server. |
||
1114 | * |
||
1115 | * Typically, you would designate one machine to be the client and the |
||
1116 | * other to be the server. The first two keys are what you'd expect for |
||
1117 | * scalarmult() below, but the latter two public keys don't swap places. |
||
1118 | * |
||
1119 | * | ALICE | BOB | |
||
1120 | * | Client | Server | |
||
1121 | * |--------------------------------|-------------------------------------| |
||
1122 | * | shared = crypto_kx( | shared = crypto_kx( | |
||
1123 | * | alice_sk, | bob_sk, | <- contextual |
||
1124 | * | bob_pk, | alice_pk, | <- contextual |
||
1125 | * | alice_pk, | alice_pk, | <----- static |
||
1126 | * | bob_pk | bob_pk | <----- static |
||
1127 | * | ) | ) | |
||
1128 | * |
||
1129 | * They are used along with the scalarmult product to generate a 256-bit |
||
1130 | * BLAKE2b hash unique to the client and server keys. |
||
1131 | * |
||
1132 | * @param string $my_secret |
||
1133 | * @param string $their_public |
||
1134 | * @param string $client_public |
||
1135 | * @param string $server_public |
||
1136 | * @return string |
||
1137 | * @throws Error |
||
1138 | * @throws TypeError |
||
1139 | */ |
||
1140 | public static function crypto_kx($my_secret, $their_public, $client_public, $server_public) |
||
1196 | |||
1197 | /** |
||
1198 | * @param int $outlen |
||
1199 | * @param string $passwd |
||
1200 | * @param string $salt |
||
1201 | * @param int $opslimit |
||
1202 | * @param int $memlimit |
||
1203 | * @return string |
||
1204 | * @throws Error |
||
1205 | */ |
||
1206 | View Code Duplication | public static function crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit) |
|
1225 | |||
1226 | /** |
||
1227 | * @param string $passwd |
||
1228 | * @param int $opslimit |
||
1229 | * @param int $memlimit |
||
1230 | * @return string |
||
1231 | * @throws Error |
||
1232 | */ |
||
1233 | View Code Duplication | public static function crypto_pwhash_str($passwd, $opslimit, $memlimit) |
|
1250 | |||
1251 | /** |
||
1252 | * @param string $passwd |
||
1253 | * @param string $hash |
||
1254 | * @return bool |
||
1255 | * @throws Error |
||
1256 | */ |
||
1257 | View Code Duplication | public static function crypto_pwhash_str_verify($passwd, $hash) |
|
1273 | |||
1274 | /** |
||
1275 | * @param int $outlen |
||
1276 | * @param string $passwd |
||
1277 | * @param string $salt |
||
1278 | * @param int $opslimit |
||
1279 | * @param int $memlimit |
||
1280 | * @return string |
||
1281 | * @throws Error |
||
1282 | */ |
||
1283 | View Code Duplication | public static function crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit) |
|
1302 | |||
1303 | /** |
||
1304 | * @param string $passwd |
||
1305 | * @param int $opslimit |
||
1306 | * @param int $memlimit |
||
1307 | * @return string |
||
1308 | * @throws Error |
||
1309 | */ |
||
1310 | View Code Duplication | public static function crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit) |
|
1327 | |||
1328 | /** |
||
1329 | * @param string $passwd |
||
1330 | * @param string $hash |
||
1331 | * @return bool |
||
1332 | * @throws Error |
||
1333 | */ |
||
1334 | View Code Duplication | public static function crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash) |
|
1350 | |||
1351 | /** |
||
1352 | * Calculate the shared secret between your secret key and your |
||
1353 | * recipient's public key. |
||
1354 | * |
||
1355 | * Algorithm: X25519 (ECDH over Curve25519) |
||
1356 | * |
||
1357 | * @param string $secretKey |
||
1358 | * @param string $publicKey |
||
1359 | * @return string |
||
1360 | * @throws Error |
||
1361 | * @throws TypeError |
||
1362 | */ |
||
1363 | public static function crypto_scalarmult($secretKey, $publicKey) |
||
1396 | |||
1397 | /** |
||
1398 | * Calculate an X25519 public key from an X25519 secret key. |
||
1399 | * |
||
1400 | * @param string $secretKey |
||
1401 | * @return string |
||
1402 | * @throws Error |
||
1403 | * @throws TypeError |
||
1404 | */ |
||
1405 | public static function crypto_scalarmult_base($secretKey) |
||
1429 | |||
1430 | /** |
||
1431 | * Authenticated symmetric-key encryption. |
||
1432 | * |
||
1433 | * Algorithm: XSalsa20-Poly1305 |
||
1434 | * |
||
1435 | * @param string $plaintext The message you're encrypting |
||
1436 | * @param string $nonce A Number to be used Once; must be 24 bytes |
||
1437 | * @param string $key Symmetric encryption key |
||
1438 | * @return string Ciphertext with Poly1305 MAC |
||
1439 | * @throws Error |
||
1440 | * @throws TypeError |
||
1441 | */ |
||
1442 | View Code Duplication | public static function crypto_secretbox($plaintext, $nonce, $key) |
|
1468 | |||
1469 | /** |
||
1470 | * Decrypts a message previously encrypted with crypto_secretbox(). |
||
1471 | * |
||
1472 | * @param string $ciphertext Ciphertext with Poly1305 MAC |
||
1473 | * @param string $nonce A Number to be used Once; must be 24 bytes |
||
1474 | * @param string $key Symmetric encryption key |
||
1475 | * @return string Original plaintext message |
||
1476 | * @throws Error |
||
1477 | * @throws TypeError |
||
1478 | */ |
||
1479 | View Code Duplication | public static function crypto_secretbox_open($ciphertext, $nonce, $key) |
|
1505 | |||
1506 | /** |
||
1507 | * Authenticated symmetric-key encryption. |
||
1508 | * |
||
1509 | * Algorithm: XChaCha20-Poly1305 |
||
1510 | * |
||
1511 | * @param string $plaintext The message you're encrypting |
||
1512 | * @param string $nonce A Number to be used Once; must be 24 bytes |
||
1513 | * @param string $key Symmetric encryption key |
||
1514 | * @return string Ciphertext with Poly1305 MAC |
||
1515 | * @throws Error |
||
1516 | * @throws TypeError |
||
1517 | */ |
||
1518 | View Code Duplication | public static function crypto_secretbox_xchacha20poly1305($plaintext, $nonce, $key) |
|
1537 | /** |
||
1538 | * Decrypts a message previously encrypted with crypto_secretbox_xchacha20poly1305(). |
||
1539 | * |
||
1540 | * @param string $ciphertext Ciphertext with Poly1305 MAC |
||
1541 | * @param string $nonce A Number to be used Once; must be 24 bytes |
||
1542 | * @param string $key Symmetric encryption key |
||
1543 | * @return string Original plaintext message |
||
1544 | * @throws Error |
||
1545 | * @throws TypeError |
||
1546 | */ |
||
1547 | View Code Duplication | public static function crypto_secretbox_xchacha20poly1305_open($ciphertext, $nonce, $key) |
|
1567 | |||
1568 | /** |
||
1569 | * Calculates a SipHash-2-4 hash of a message for a given key. |
||
1570 | * |
||
1571 | * @param string $message Input message |
||
1572 | * @param string $key SipHash-2-4 key |
||
1573 | * @return string Hash |
||
1574 | * @throws Error |
||
1575 | * @throws TypeError |
||
1576 | */ |
||
1577 | View Code Duplication | public static function crypto_shorthash($message, $key) |
|
1599 | |||
1600 | /** |
||
1601 | * Expand a key and nonce into a keystream of pseudorandom bytes. |
||
1602 | * |
||
1603 | * @param int $len Number of bytes desired |
||
1604 | * @param string $nonce Number to be used Once; must be 24 bytes |
||
1605 | * @param string $key XSalsa20 key |
||
1606 | * @return string Pseudorandom stream that can be XORed with messages |
||
1607 | * to provide encryption (but not authentication; see |
||
1608 | * Poly1305 or crypto_auth() for that, which is not |
||
1609 | * optional for security) |
||
1610 | * @throws Error |
||
1611 | * @throws TypeError |
||
1612 | */ |
||
1613 | View Code Duplication | public static function crypto_stream($len, $nonce, $key) |
|
1639 | |||
1640 | /** |
||
1641 | * DANGER! UNAUTHENTICATED ENCRYPTION! |
||
1642 | * |
||
1643 | * Unless you are following expert advice, do not used this feature. |
||
1644 | * |
||
1645 | * Algorithm: XSalsa20 |
||
1646 | * |
||
1647 | * This DOES NOT provide ciphertext integrity. |
||
1648 | * |
||
1649 | * @param string $message Plaintext message |
||
1650 | * @param string $nonce Number to be used Once; must be 24 bytes |
||
1651 | * @param string $key Encryption key |
||
1652 | * @return string Encrypted text which is vulnerable to chosen- |
||
1653 | * ciphertext attacks unless you implement some |
||
1654 | * other mitigation to the ciphertext (i.e. |
||
1655 | * Encrypt then MAC) |
||
1656 | * @throws Error |
||
1657 | * @throws TypeError |
||
1658 | */ |
||
1659 | View Code Duplication | public static function crypto_stream_xor($message, $nonce, $key) |
|
1685 | |||
1686 | /** |
||
1687 | * Returns a signed message. You probably want crypto_sign_detached() |
||
1688 | * instead, which only returns the signature. |
||
1689 | * |
||
1690 | * Algorithm: Ed25519 (EdDSA over Curve25519) |
||
1691 | * |
||
1692 | * @param string $message Message to be signed. |
||
1693 | * @param string $secretKey Secret signing key. |
||
1694 | * @return string Signed message (signature is prefixed). |
||
1695 | * @throws Error |
||
1696 | * @throws TypeError |
||
1697 | */ |
||
1698 | View Code Duplication | public static function crypto_sign($message, $secretKey) |
|
1720 | |||
1721 | /** |
||
1722 | * Validates a signed message then returns the message. |
||
1723 | * |
||
1724 | * @param string $signedMessage A signed message |
||
1725 | * @param string $publicKey A public key |
||
1726 | * @return string The original message (if the signature is |
||
1727 | * valid for this public key) |
||
1728 | * @throws Error |
||
1729 | * @throws TypeError |
||
1730 | */ |
||
1731 | View Code Duplication | public static function crypto_sign_open($signedMessage, $publicKey) |
|
1756 | |||
1757 | /** |
||
1758 | * Generate a new random Ed25519 keypair. |
||
1759 | * |
||
1760 | * @return string |
||
1761 | */ |
||
1762 | public static function crypto_sign_keypair() |
||
1777 | |||
1778 | /** |
||
1779 | * Generate an Ed25519 keypair from a seed. |
||
1780 | * |
||
1781 | * @param string $seed Input seed |
||
1782 | * @return string Keypair |
||
1783 | */ |
||
1784 | View Code Duplication | public static function crypto_sign_seed_keypair($seed) |
|
1803 | |||
1804 | /** |
||
1805 | * Extract an Ed25519 public key from an Ed25519 keypair. |
||
1806 | * |
||
1807 | * @param string $keypair Keypair |
||
1808 | * @return string Public key |
||
1809 | * @throws Error |
||
1810 | * @throws TypeError |
||
1811 | */ |
||
1812 | View Code Duplication | public static function crypto_sign_publickey($keypair) |
|
1833 | |||
1834 | /** |
||
1835 | * Calculate an Ed25519 public key from an Ed25519 secret key. |
||
1836 | * |
||
1837 | * @param string $secretKey Your Ed25519 secret key |
||
1838 | * @return string The corresponding Ed25519 public key |
||
1839 | * @throws Error |
||
1840 | * @throws TypeError |
||
1841 | */ |
||
1842 | View Code Duplication | public static function crypto_sign_publickey_from_secretkey($secretKey) |
|
1863 | |||
1864 | /** |
||
1865 | * Extract an Ed25519 secret key from an Ed25519 keypair. |
||
1866 | * |
||
1867 | * @param string $keypair Keypair |
||
1868 | * @return string Secret key |
||
1869 | * @throws Error |
||
1870 | * @throws TypeError |
||
1871 | */ |
||
1872 | View Code Duplication | public static function crypto_sign_secretkey($keypair) |
|
1893 | |||
1894 | /** |
||
1895 | * Calculate the Ed25519 signature of a message and return ONLY the signature. |
||
1896 | * |
||
1897 | * Algorithm: Ed25519 (EdDSA over Curve25519) |
||
1898 | * |
||
1899 | * @param string $message Message to be signed |
||
1900 | * @param string $secretKey Secret signing key |
||
1901 | * @return string Digital signature |
||
1902 | * @throws Error |
||
1903 | * @throws TypeError |
||
1904 | */ |
||
1905 | View Code Duplication | public static function crypto_sign_detached($message, $secretKey) |
|
1927 | |||
1928 | /** |
||
1929 | * Verify the Ed25519 signature of a message. |
||
1930 | * |
||
1931 | * @param string $signature Digital sginature |
||
1932 | * @param string $message Message to be verified |
||
1933 | * @param string $publicKey Public key |
||
1934 | * @return bool TRUE if this signature is good for this public key; |
||
1935 | * FALSE otherwise |
||
1936 | * @throws Error |
||
1937 | * @throws TypeError |
||
1938 | */ |
||
1939 | View Code Duplication | public static function crypto_sign_verify_detached($signature, $message, $publicKey) |
|
1965 | |||
1966 | /** |
||
1967 | * Convert an Ed25519 secret key to a Curve25519 secret key |
||
1968 | * |
||
1969 | * @param string $sk |
||
1970 | * @return string |
||
1971 | * @throws Error |
||
1972 | */ |
||
1973 | public static function crypto_sign_ed25519_sk_to_curve25519($sk) |
||
2000 | |||
2001 | /** |
||
2002 | * Cache-timing-safe implementation of hex2bin(). |
||
2003 | * |
||
2004 | * @param string $string Hexadecimal string |
||
2005 | * @return string Raw binary string |
||
2006 | * @throws TypeError |
||
2007 | */ |
||
2008 | View Code Duplication | public static function hex2bin($string) |
|
2021 | |||
2022 | /** |
||
2023 | * Increase a string (little endian) |
||
2024 | * |
||
2025 | * @param string $var |
||
2026 | * |
||
2027 | * @return void |
||
2028 | * @throws Error (Unless libsodium is installed) |
||
2029 | */ |
||
2030 | public static function increment(&$var) |
||
2056 | |||
2057 | /** |
||
2058 | * The equivalent to the libsodium minor version we aim to be compatible |
||
2059 | * with (sans pwhash and memzero). |
||
2060 | * |
||
2061 | * @return int |
||
2062 | */ |
||
2063 | public static function library_version_major() |
||
2073 | |||
2074 | /** |
||
2075 | * The equivalent to the libsodium minor version we aim to be compatible |
||
2076 | * with (sans pwhash and memzero). |
||
2077 | * |
||
2078 | * @return int |
||
2079 | */ |
||
2080 | public static function library_version_minor() |
||
2090 | |||
2091 | /** |
||
2092 | * Compare two strings. |
||
2093 | * |
||
2094 | * @param string $left |
||
2095 | * @param string $right |
||
2096 | * @return int |
||
2097 | * @throws TypeError |
||
2098 | */ |
||
2099 | public static function memcmp($left, $right) |
||
2110 | |||
2111 | /** |
||
2112 | * It's actually not possible to zero memory buffers in PHP. You need the |
||
2113 | * native library for that. |
||
2114 | * |
||
2115 | * @param string|null $var |
||
2116 | * |
||
2117 | * @return void |
||
2118 | * @throws Error (Unless libsodium is installed) |
||
2119 | */ |
||
2120 | View Code Duplication | public static function memzero(&$var) |
|
2138 | |||
2139 | /** |
||
2140 | * Generate a string of bytes from the kernel's CSPRNG. |
||
2141 | * Proudly uses /dev/urandom (if getrandom(2) is not available). |
||
2142 | * |
||
2143 | * @param int $numBytes |
||
2144 | * @return string |
||
2145 | * @throws TypeError |
||
2146 | */ |
||
2147 | View Code Duplication | public static function randombytes_buf($numBytes) |
|
2162 | |||
2163 | /** |
||
2164 | * Generate an integer between 0 and $range (non-inclusive). |
||
2165 | * |
||
2166 | * @param int $range |
||
2167 | * @return int |
||
2168 | * @throws TypeError |
||
2169 | */ |
||
2170 | View Code Duplication | public static function randombytes_uniform($range) |
|
2185 | |||
2186 | /** |
||
2187 | * Generate a random 16-bit integer. |
||
2188 | * |
||
2189 | * @return int |
||
2190 | */ |
||
2191 | public static function randombytes_random16() |
||
2198 | |||
2199 | /** |
||
2200 | * This emulates libsodium's version_string() function, except ours is |
||
2201 | * prefixed with 'polyfill-'. |
||
2202 | * |
||
2203 | * @return string |
||
2204 | */ |
||
2205 | public static function version_string() |
||
2215 | |||
2216 | /** |
||
2217 | * Should we use the libsodium core function instead? |
||
2218 | * This is always a good idea, if it's available. (Unless we're in the |
||
2219 | * middle of running our unit test suite.) |
||
2220 | * |
||
2221 | * If ext/libsodium is available, use it. Return TRUE. |
||
2222 | * Otherwise, we have to use the code provided herein. Return FALSE. |
||
2223 | * |
||
2224 | * @param string $sodium_func_name |
||
2225 | * |
||
2226 | * @return bool |
||
2227 | */ |
||
2228 | protected static function use_fallback($sodium_func_name = '') |
||
2264 | |||
2265 | /** |
||
2266 | * Libsodium as implemented in PHP 7.2 |
||
2267 | * |
||
2268 | * @ref https://wiki.php.net/rfc/libsodium |
||
2269 | * @return bool |
||
2270 | */ |
||
2271 | protected static function isPhp72OrGreater() |
||
2283 | } |
||
2284 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.