php72compat.php 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  1. <?php
  2. /**
  3. * This file will monkey patch the pure-PHP implementation in place of the
  4. * PECL functions and constants, but only if they do not already exist.
  5. *
  6. * Thus, the functions or constants just proxy to the appropriate
  7. * ParagonIE_Sodium_Compat method or class constant, respectively.
  8. */
  9. foreach (array(
  10. 'CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES',
  11. 'CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES',
  12. 'CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES',
  13. 'CRYPTO_AEAD_CHACHA20POLY1305_ABYTES',
  14. 'CRYPTO_AEAD_AES256GCM_KEYBYTES',
  15. 'CRYPTO_AEAD_AES256GCM_NSECBYTES',
  16. 'CRYPTO_AEAD_AES256GCM_NPUBBYTES',
  17. 'CRYPTO_AEAD_AES256GCM_ABYTES',
  18. 'CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES',
  19. 'CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES',
  20. 'CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES',
  21. 'CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES',
  22. 'CRYPTO_AUTH_BYTES',
  23. 'CRYPTO_AUTH_KEYBYTES',
  24. 'CRYPTO_BOX_SEALBYTES',
  25. 'CRYPTO_BOX_SECRETKEYBYTES',
  26. 'CRYPTO_BOX_PUBLICKEYBYTES',
  27. 'CRYPTO_BOX_KEYPAIRBYTES',
  28. 'CRYPTO_BOX_MACBYTES',
  29. 'CRYPTO_BOX_NONCEBYTES',
  30. 'CRYPTO_BOX_SEEDBYTES',
  31. 'CRYPTO_KX_BYTES',
  32. 'CRYPTO_KX_SEEDBYTES',
  33. 'CRYPTO_KX_PUBLICKEYBYTES',
  34. 'CRYPTO_KX_SECRETKEYBYTES',
  35. 'CRYPTO_GENERICHASH_BYTES',
  36. 'CRYPTO_GENERICHASH_BYTES_MIN',
  37. 'CRYPTO_GENERICHASH_BYTES_MAX',
  38. 'CRYPTO_GENERICHASH_KEYBYTES',
  39. 'CRYPTO_GENERICHASH_KEYBYTES_MIN',
  40. 'CRYPTO_GENERICHASH_KEYBYTES_MAX',
  41. 'CRYPTO_PWHASH_SALTBYTES',
  42. 'CRYPTO_PWHASH_STRPREFIX',
  43. 'CRYPTO_PWHASH_ALG_ARGON2I13',
  44. 'CRYPTO_PWHASH_ALG_ARGON2ID13',
  45. 'CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE',
  46. 'CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE',
  47. 'CRYPTO_PWHASH_MEMLIMIT_MODERATE',
  48. 'CRYPTO_PWHASH_OPSLIMIT_MODERATE',
  49. 'CRYPTO_PWHASH_MEMLIMIT_SENSITIVE',
  50. 'CRYPTO_PWHASH_OPSLIMIT_SENSITIVE',
  51. 'CRYPTO_SCALARMULT_BYTES',
  52. 'CRYPTO_SCALARMULT_SCALARBYTES',
  53. 'CRYPTO_SHORTHASH_BYTES',
  54. 'CRYPTO_SHORTHASH_KEYBYTES',
  55. 'CRYPTO_SECRETBOX_KEYBYTES',
  56. 'CRYPTO_SECRETBOX_MACBYTES',
  57. 'CRYPTO_SECRETBOX_NONCEBYTES',
  58. 'CRYPTO_SIGN_BYTES',
  59. 'CRYPTO_SIGN_SEEDBYTES',
  60. 'CRYPTO_SIGN_PUBLICKEYBYTES',
  61. 'CRYPTO_SIGN_SECRETKEYBYTES',
  62. 'CRYPTO_SIGN_KEYPAIRBYTES',
  63. 'CRYPTO_STREAM_KEYBYTES',
  64. 'CRYPTO_STREAM_NONCEBYTES',
  65. 'LIBRARY_VERSION_MAJOR',
  66. 'LIBRARY_VERSION_MINOR',
  67. 'VERSION_STRING'
  68. ) as $constant
  69. ) {
  70. if (!defined("SODIUM_$constant")) {
  71. define("SODIUM_$constant", constant("ParagonIE_Sodium_Compat::$constant"));
  72. }
  73. }
  74. if (!is_callable('sodium_bin2hex')) {
  75. /**
  76. * @see ParagonIE_Sodium_Compat::hex2bin()
  77. * @param string $string
  78. * @return string
  79. * @throws SodiumException
  80. * @throws TypeError
  81. */
  82. function sodium_bin2hex($string)
  83. {
  84. return ParagonIE_Sodium_Compat::bin2hex($string);
  85. }
  86. }
  87. if (!is_callable('sodium_compare')) {
  88. /**
  89. * @see ParagonIE_Sodium_Compat::compare()
  90. * @param string $a
  91. * @param string $b
  92. * @return int
  93. * @throws SodiumException
  94. * @throws TypeError
  95. */
  96. function sodium_compare($a, $b)
  97. {
  98. return ParagonIE_Sodium_Compat::compare($a, $b);
  99. }
  100. }
  101. if (!is_callable('sodium_crypto_aead_aes256gcm_decrypt')) {
  102. /**
  103. * @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt()
  104. * @param string $message
  105. * @param string $assocData
  106. * @param string $nonce
  107. * @param string $key
  108. * @return string
  109. */
  110. function sodium_crypto_aead_aes256gcm_decrypt($message, $assocData, $nonce, $key)
  111. {
  112. try {
  113. return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt($message, $assocData, $nonce, $key);
  114. } catch (Error $ex) {
  115. return false;
  116. } catch (Exception $ex) {
  117. return false;
  118. }
  119. }
  120. }
  121. if (!is_callable('sodium_crypto_aead_aes256gcm_encrypt')) {
  122. /**
  123. * @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_encrypt()
  124. * @param string $message
  125. * @param string $assocData
  126. * @param string $nonce
  127. * @param string $key
  128. * @return string
  129. * @throws SodiumException
  130. * @throws TypeError
  131. */
  132. function sodium_crypto_aead_aes256gcm_encrypt($message, $assocData, $nonce, $key)
  133. {
  134. return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_encrypt($message, $assocData, $nonce, $key);
  135. }
  136. }
  137. if (!is_callable('sodium_crypto_aead_aes256gcm_is_available')) {
  138. /**
  139. * @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_is_available()
  140. * @return bool
  141. */
  142. function sodium_crypto_aead_aes256gcm_is_available()
  143. {
  144. return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_is_available();
  145. }
  146. }
  147. if (!is_callable('sodium_crypto_aead_chacha20poly1305_decrypt')) {
  148. /**
  149. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt()
  150. * @param string $message
  151. * @param string $assocData
  152. * @param string $nonce
  153. * @param string $key
  154. * @return string
  155. */
  156. function sodium_crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key)
  157. {
  158. try {
  159. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key);
  160. } catch (Error $ex) {
  161. return false;
  162. } catch (Exception $ex) {
  163. return false;
  164. }
  165. }
  166. }
  167. if (!is_callable('sodium_crypto_aead_chacha20poly1305_encrypt')) {
  168. /**
  169. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_encrypt()
  170. * @param string $message
  171. * @param string $assocData
  172. * @param string $nonce
  173. * @param string $key
  174. * @return string
  175. * @throws SodiumException
  176. * @throws TypeError
  177. */
  178. function sodium_crypto_aead_chacha20poly1305_encrypt($message, $assocData, $nonce, $key)
  179. {
  180. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_encrypt($message, $assocData, $nonce, $key);
  181. }
  182. }
  183. if (!is_callable('sodium_crypto_aead_chacha20poly1305_keygen')) {
  184. /**
  185. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_keygen()
  186. * @return string
  187. */
  188. function sodium_crypto_aead_chacha20poly1305_keygen()
  189. {
  190. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_keygen();
  191. }
  192. }
  193. if (!is_callable('sodium_crypto_aead_chacha20poly1305_ietf_decrypt')) {
  194. /**
  195. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt()
  196. * @param string $message
  197. * @param string $assocData
  198. * @param string $nonce
  199. * @param string $key
  200. * @return string
  201. */
  202. function sodium_crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key)
  203. {
  204. try {
  205. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key);
  206. } catch (Error $ex) {
  207. return false;
  208. } catch (Exception $ex) {
  209. return false;
  210. }
  211. }
  212. }
  213. if (!is_callable('sodium_crypto_aead_chacha20poly1305_ietf_encrypt')) {
  214. /**
  215. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt()
  216. * @param string $message
  217. * @param string $assocData
  218. * @param string $nonce
  219. * @param string $key
  220. * @return string
  221. * @throws SodiumException
  222. * @throws TypeError
  223. */
  224. function sodium_crypto_aead_chacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key)
  225. {
  226. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key);
  227. }
  228. }
  229. if (!is_callable('sodium_crypto_aead_chacha20poly1305_ietf_keygen')) {
  230. /**
  231. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_keygen()
  232. * @return string
  233. */
  234. function sodium_crypto_aead_chacha20poly1305_ietf_keygen()
  235. {
  236. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_keygen();
  237. }
  238. }
  239. if (!is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_decrypt')) {
  240. /**
  241. * @see ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_decrypt()
  242. * @param string $message
  243. * @param string $assocData
  244. * @param string $nonce
  245. * @param string $key
  246. * @return string
  247. */
  248. function sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key)
  249. {
  250. try {
  251. return ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key, true);
  252. } catch (Error $ex) {
  253. return false;
  254. } catch (Exception $ex) {
  255. return false;
  256. }
  257. }
  258. }
  259. if (!is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')) {
  260. /**
  261. * @see ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_encrypt()
  262. * @param string $message
  263. * @param string $assocData
  264. * @param string $nonce
  265. * @param string $key
  266. * @return string
  267. * @throws SodiumException
  268. * @throws TypeError
  269. */
  270. function sodium_crypto_aead_xchacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key)
  271. {
  272. return ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key, true);
  273. }
  274. }
  275. if (!is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_keygen')) {
  276. /**
  277. * @see ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_keygen()
  278. * @return string
  279. */
  280. function sodium_crypto_aead_xchacha20poly1305_ietf_keygen()
  281. {
  282. return ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_keygen();
  283. }
  284. }
  285. if (!is_callable('sodium_crypto_auth')) {
  286. /**
  287. * @see ParagonIE_Sodium_Compat::crypto_auth()
  288. * @param string $message
  289. * @param string $key
  290. * @return string
  291. * @throws SodiumException
  292. * @throws TypeError
  293. */
  294. function sodium_crypto_auth($message, $key)
  295. {
  296. return ParagonIE_Sodium_Compat::crypto_auth($message, $key);
  297. }
  298. }
  299. if (!is_callable('sodium_crypto_auth_keygen')) {
  300. /**
  301. * @see ParagonIE_Sodium_Compat::crypto_auth_keygen()
  302. * @return string
  303. */
  304. function sodium_crypto_auth_keygen()
  305. {
  306. return ParagonIE_Sodium_Compat::crypto_auth_keygen();
  307. }
  308. }
  309. if (!is_callable('sodium_crypto_auth_verify')) {
  310. /**
  311. * @see ParagonIE_Sodium_Compat::crypto_auth_verify()
  312. * @param string $mac
  313. * @param string $message
  314. * @param string $key
  315. * @return bool
  316. * @throws SodiumException
  317. * @throws TypeError
  318. */
  319. function sodium_crypto_auth_verify($mac, $message, $key)
  320. {
  321. return ParagonIE_Sodium_Compat::crypto_auth_verify($mac, $message, $key);
  322. }
  323. }
  324. if (!is_callable('sodium_crypto_box')) {
  325. /**
  326. * @see ParagonIE_Sodium_Compat::crypto_box()
  327. * @param string $message
  328. * @param string $nonce
  329. * @param string $kp
  330. * @return string
  331. * @throws SodiumException
  332. * @throws TypeError
  333. */
  334. function sodium_crypto_box($message, $nonce, $kp)
  335. {
  336. return ParagonIE_Sodium_Compat::crypto_box($message, $nonce, $kp);
  337. }
  338. }
  339. if (!is_callable('sodium_crypto_box_keypair')) {
  340. /**
  341. * @see ParagonIE_Sodium_Compat::crypto_box_keypair()
  342. * @return string
  343. * @throws SodiumException
  344. * @throws TypeError
  345. */
  346. function sodium_crypto_box_keypair()
  347. {
  348. return ParagonIE_Sodium_Compat::crypto_box_keypair();
  349. }
  350. }
  351. if (!is_callable('sodium_crypto_box_keypair_from_secretkey_and_publickey')) {
  352. /**
  353. * @see ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey()
  354. * @param string $sk
  355. * @param string $pk
  356. * @return string
  357. * @throws SodiumException
  358. * @throws TypeError
  359. */
  360. function sodium_crypto_box_keypair_from_secretkey_and_publickey($sk, $pk)
  361. {
  362. return ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey($sk, $pk);
  363. }
  364. }
  365. if (!is_callable('sodium_crypto_box_open')) {
  366. /**
  367. * @see ParagonIE_Sodium_Compat::crypto_box_open()
  368. * @param string $message
  369. * @param string $nonce
  370. * @param string $kp
  371. * @return string|bool
  372. */
  373. function sodium_crypto_box_open($message, $nonce, $kp)
  374. {
  375. try {
  376. return ParagonIE_Sodium_Compat::crypto_box_open($message, $nonce, $kp);
  377. } catch (Error $ex) {
  378. return false;
  379. } catch (Exception $ex) {
  380. return false;
  381. }
  382. }
  383. }
  384. if (!is_callable('sodium_crypto_box_publickey')) {
  385. /**
  386. * @see ParagonIE_Sodium_Compat::crypto_box_publickey()
  387. * @param string $keypair
  388. * @return string
  389. * @throws SodiumException
  390. * @throws TypeError
  391. */
  392. function sodium_crypto_box_publickey($keypair)
  393. {
  394. return ParagonIE_Sodium_Compat::crypto_box_publickey($keypair);
  395. }
  396. }
  397. if (!is_callable('sodium_crypto_box_publickey_from_secretkey')) {
  398. /**
  399. * @see ParagonIE_Sodium_Compat::crypto_box_publickey_from_secretkey()
  400. * @param string $sk
  401. * @return string
  402. * @throws SodiumException
  403. * @throws TypeError
  404. */
  405. function sodium_crypto_box_publickey_from_secretkey($sk)
  406. {
  407. return ParagonIE_Sodium_Compat::crypto_box_publickey_from_secretkey($sk);
  408. }
  409. }
  410. if (!is_callable('sodium_crypto_box_seal')) {
  411. /**
  412. * @see ParagonIE_Sodium_Compat::crypto_box_seal()
  413. * @param string $message
  414. * @param string $publicKey
  415. * @return string
  416. * @throws SodiumException
  417. * @throws TypeError
  418. */
  419. function sodium_crypto_box_seal($message, $publicKey)
  420. {
  421. return ParagonIE_Sodium_Compat::crypto_box_seal($message, $publicKey);
  422. }
  423. }
  424. if (!is_callable('sodium_crypto_box_seal_open')) {
  425. /**
  426. * @see ParagonIE_Sodium_Compat::crypto_box_seal_open()
  427. * @param string $message
  428. * @param string $kp
  429. * @return string|bool
  430. * @throws SodiumException
  431. */
  432. function sodium_crypto_box_seal_open($message, $kp)
  433. {
  434. try {
  435. return ParagonIE_Sodium_Compat::crypto_box_seal_open($message, $kp);
  436. } catch (SodiumException $ex) {
  437. if ($ex->getMessage() === 'Argument 2 must be CRYPTO_BOX_KEYPAIRBYTES long.') {
  438. throw $ex;
  439. }
  440. return false;
  441. }
  442. }
  443. }
  444. if (!is_callable('sodium_crypto_box_secretkey')) {
  445. /**
  446. * @see ParagonIE_Sodium_Compat::crypto_box_secretkey()
  447. * @param string $keypair
  448. * @return string
  449. * @throws SodiumException
  450. * @throws TypeError
  451. */
  452. function sodium_crypto_box_secretkey($keypair)
  453. {
  454. return ParagonIE_Sodium_Compat::crypto_box_secretkey($keypair);
  455. }
  456. }
  457. if (!is_callable('sodium_crypto_box_seed_keypair')) {
  458. /**
  459. * @see ParagonIE_Sodium_Compat::crypto_box_seed_keypair()
  460. * @param string $seed
  461. * @return string
  462. * @throws SodiumException
  463. * @throws TypeError
  464. */
  465. function sodium_crypto_box_seed_keypair($seed)
  466. {
  467. return ParagonIE_Sodium_Compat::crypto_box_seed_keypair($seed);
  468. }
  469. }
  470. if (!is_callable('sodium_crypto_generichash')) {
  471. /**
  472. * @see ParagonIE_Sodium_Compat::crypto_generichash()
  473. * @param string $message
  474. * @param string|null $key
  475. * @param int $outLen
  476. * @return string
  477. * @throws SodiumException
  478. * @throws TypeError
  479. */
  480. function sodium_crypto_generichash($message, $key = null, $outLen = 32)
  481. {
  482. return ParagonIE_Sodium_Compat::crypto_generichash($message, $key, $outLen);
  483. }
  484. }
  485. if (!is_callable('sodium_crypto_generichash_final')) {
  486. /**
  487. * @see ParagonIE_Sodium_Compat::crypto_generichash_final()
  488. * @param string|null $ctx
  489. * @param int $outputLength
  490. * @return string
  491. * @throws SodiumException
  492. * @throws TypeError
  493. */
  494. function sodium_crypto_generichash_final(&$ctx, $outputLength = 32)
  495. {
  496. return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength);
  497. }
  498. }
  499. if (!is_callable('sodium_crypto_generichash_init')) {
  500. /**
  501. * @see ParagonIE_Sodium_Compat::crypto_generichash_init()
  502. * @param string|null $key
  503. * @param int $outLen
  504. * @return string
  505. * @throws SodiumException
  506. * @throws TypeError
  507. */
  508. function sodium_crypto_generichash_init($key = null, $outLen = 32)
  509. {
  510. return ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outLen);
  511. }
  512. }
  513. if (!is_callable('sodium_crypto_generichash_keygen')) {
  514. /**
  515. * @see ParagonIE_Sodium_Compat::crypto_generichash_keygen()
  516. * @return string
  517. */
  518. function sodium_crypto_generichash_keygen()
  519. {
  520. return ParagonIE_Sodium_Compat::crypto_generichash_keygen();
  521. }
  522. }
  523. if (!is_callable('sodium_crypto_generichash_update')) {
  524. /**
  525. * @see ParagonIE_Sodium_Compat::crypto_generichash_update()
  526. * @param string|null $ctx
  527. * @param string $message
  528. * @return void
  529. * @throws SodiumException
  530. * @throws TypeError
  531. */
  532. function sodium_crypto_generichash_update(&$ctx, $message = '')
  533. {
  534. ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $message);
  535. }
  536. }
  537. if (!is_callable('sodium_crypto_kx')) {
  538. /**
  539. * @see ParagonIE_Sodium_Compat::crypto_kx()
  540. * @param string $my_secret
  541. * @param string $their_public
  542. * @param string $client_public
  543. * @param string $server_public
  544. * @return string
  545. * @throws SodiumException
  546. * @throws TypeError
  547. */
  548. function sodium_crypto_kx($my_secret, $their_public, $client_public, $server_public)
  549. {
  550. return ParagonIE_Sodium_Compat::crypto_kx(
  551. $my_secret,
  552. $their_public,
  553. $client_public,
  554. $server_public
  555. );
  556. }
  557. }
  558. if (!is_callable('sodium_crypto_pwhash')) {
  559. /**
  560. * @see ParagonIE_Sodium_Compat::crypto_pwhash()
  561. * @param int $outlen
  562. * @param string $passwd
  563. * @param string $salt
  564. * @param int $opslimit
  565. * @param int $memlimit
  566. * @param int|null $algo
  567. * @return string
  568. * @throws SodiumException
  569. * @throws TypeError
  570. */
  571. function sodium_crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit, $algo = null)
  572. {
  573. return ParagonIE_Sodium_Compat::crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit, $algo);
  574. }
  575. }
  576. if (!is_callable('sodium_crypto_pwhash_str')) {
  577. /**
  578. * @see ParagonIE_Sodium_Compat::crypto_pwhash_str()
  579. * @param string $passwd
  580. * @param int $opslimit
  581. * @param int $memlimit
  582. * @return string
  583. * @throws SodiumException
  584. * @throws TypeError
  585. */
  586. function sodium_crypto_pwhash_str($passwd, $opslimit, $memlimit)
  587. {
  588. return ParagonIE_Sodium_Compat::crypto_pwhash_str($passwd, $opslimit, $memlimit);
  589. }
  590. }
  591. if (!is_callable('sodium_crypto_pwhash_str_verify')) {
  592. /**
  593. * @see ParagonIE_Sodium_Compat::crypto_pwhash_str_verify()
  594. * @param string $passwd
  595. * @param string $hash
  596. * @return bool
  597. * @throws SodiumException
  598. * @throws TypeError
  599. */
  600. function sodium_crypto_pwhash_str_verify($passwd, $hash)
  601. {
  602. return ParagonIE_Sodium_Compat::crypto_pwhash_str_verify($passwd, $hash);
  603. }
  604. }
  605. if (!is_callable('sodium_crypto_pwhash_scryptsalsa208sha256')) {
  606. /**
  607. * @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256()
  608. * @param int $outlen
  609. * @param string $passwd
  610. * @param string $salt
  611. * @param int $opslimit
  612. * @param int $memlimit
  613. * @return string
  614. * @throws SodiumException
  615. * @throws TypeError
  616. */
  617. function sodium_crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit)
  618. {
  619. return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit);
  620. }
  621. }
  622. if (!is_callable('sodium_crypto_pwhash_scryptsalsa208sha256_str')) {
  623. /**
  624. * @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str()
  625. * @param string $passwd
  626. * @param int $opslimit
  627. * @param int $memlimit
  628. * @return string
  629. * @throws SodiumException
  630. * @throws TypeError
  631. */
  632. function sodium_crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit)
  633. {
  634. return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit);
  635. }
  636. }
  637. if (!is_callable('sodium_crypto_pwhash_scryptsalsa208sha256_str_verify')) {
  638. /**
  639. * @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str_verify()
  640. * @param string $passwd
  641. * @param string $hash
  642. * @return bool
  643. * @throws SodiumException
  644. * @throws TypeError
  645. */
  646. function sodium_crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash)
  647. {
  648. return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash);
  649. }
  650. }
  651. if (!is_callable('sodium_crypto_scalarmult')) {
  652. /**
  653. * @see ParagonIE_Sodium_Compat::crypto_scalarmult()
  654. * @param string $n
  655. * @param string $p
  656. * @return string
  657. * @throws SodiumException
  658. * @throws TypeError
  659. */
  660. function sodium_crypto_scalarmult($n, $p)
  661. {
  662. return ParagonIE_Sodium_Compat::crypto_scalarmult($n, $p);
  663. }
  664. }
  665. if (!is_callable('sodium_crypto_scalarmult_base')) {
  666. /**
  667. * @see ParagonIE_Sodium_Compat::crypto_scalarmult_base()
  668. * @param string $n
  669. * @return string
  670. * @throws SodiumException
  671. * @throws TypeError
  672. */
  673. function sodium_crypto_scalarmult_base($n)
  674. {
  675. return ParagonIE_Sodium_Compat::crypto_scalarmult_base($n);
  676. }
  677. }
  678. if (!is_callable('sodium_crypto_secretbox')) {
  679. /**
  680. * @see ParagonIE_Sodium_Compat::crypto_secretbox()
  681. * @param string $message
  682. * @param string $nonce
  683. * @param string $key
  684. * @return string
  685. * @throws SodiumException
  686. * @throws TypeError
  687. */
  688. function sodium_crypto_secretbox($message, $nonce, $key)
  689. {
  690. return ParagonIE_Sodium_Compat::crypto_secretbox($message, $nonce, $key);
  691. }
  692. }
  693. if (!is_callable('sodium_crypto_secretbox_keygen')) {
  694. /**
  695. * @see ParagonIE_Sodium_Compat::crypto_secretbox_keygen()
  696. * @return string
  697. */
  698. function sodium_crypto_secretbox_keygen()
  699. {
  700. return ParagonIE_Sodium_Compat::crypto_secretbox_keygen();
  701. }
  702. }
  703. if (!is_callable('sodium_crypto_secretbox_open')) {
  704. /**
  705. * @see ParagonIE_Sodium_Compat::crypto_secretbox_open()
  706. * @param string $message
  707. * @param string $nonce
  708. * @param string $key
  709. * @return string|bool
  710. */
  711. function sodium_crypto_secretbox_open($message, $nonce, $key)
  712. {
  713. try {
  714. return ParagonIE_Sodium_Compat::crypto_secretbox_open($message, $nonce, $key);
  715. } catch (Error $ex) {
  716. return false;
  717. } catch (Exception $ex) {
  718. return false;
  719. }
  720. }
  721. }
  722. if (!is_callable('sodium_crypto_shorthash')) {
  723. /**
  724. * @see ParagonIE_Sodium_Compat::crypto_shorthash()
  725. * @param string $message
  726. * @param string $key
  727. * @return string
  728. * @throws SodiumException
  729. * @throws TypeError
  730. */
  731. function sodium_crypto_shorthash($message, $key = '')
  732. {
  733. return ParagonIE_Sodium_Compat::crypto_shorthash($message, $key);
  734. }
  735. }
  736. if (!is_callable('sodium_crypto_shorthash_keygen')) {
  737. /**
  738. * @see ParagonIE_Sodium_Compat::crypto_shorthash_keygen()
  739. * @return string
  740. */
  741. function sodium_crypto_shorthash_keygen()
  742. {
  743. return ParagonIE_Sodium_Compat::crypto_shorthash_keygen();
  744. }
  745. }
  746. if (!is_callable('sodium_crypto_sign')) {
  747. /**
  748. * @see ParagonIE_Sodium_Compat::crypto_sign()
  749. * @param string $message
  750. * @param string $sk
  751. * @return string
  752. * @throws SodiumException
  753. * @throws TypeError
  754. */
  755. function sodium_crypto_sign($message, $sk)
  756. {
  757. return ParagonIE_Sodium_Compat::crypto_sign($message, $sk);
  758. }
  759. }
  760. if (!is_callable('sodium_crypto_sign_detached')) {
  761. /**
  762. * @see ParagonIE_Sodium_Compat::crypto_sign_detached()
  763. * @param string $message
  764. * @param string $sk
  765. * @return string
  766. * @throws SodiumException
  767. * @throws TypeError
  768. */
  769. function sodium_crypto_sign_detached($message, $sk)
  770. {
  771. return ParagonIE_Sodium_Compat::crypto_sign_detached($message, $sk);
  772. }
  773. }
  774. if (!is_callable('sodium_crypto_sign_keypair')) {
  775. /**
  776. * @see ParagonIE_Sodium_Compat::crypto_sign_keypair()
  777. * @return string
  778. * @throws SodiumException
  779. * @throws TypeError
  780. */
  781. function sodium_crypto_sign_keypair()
  782. {
  783. return ParagonIE_Sodium_Compat::crypto_sign_keypair();
  784. }
  785. }
  786. if (!is_callable('sodium_crypto_sign_open')) {
  787. /**
  788. * @see ParagonIE_Sodium_Compat::crypto_sign_open()
  789. * @param string $signedMessage
  790. * @param string $pk
  791. * @return string|bool
  792. */
  793. function sodium_crypto_sign_open($signedMessage, $pk)
  794. {
  795. try {
  796. return ParagonIE_Sodium_Compat::crypto_sign_open($signedMessage, $pk);
  797. } catch (Error $ex) {
  798. return false;
  799. } catch (Exception $ex) {
  800. return false;
  801. }
  802. }
  803. }
  804. if (!is_callable('sodium_crypto_sign_publickey')) {
  805. /**
  806. * @see ParagonIE_Sodium_Compat::crypto_sign_publickey()
  807. * @param string $keypair
  808. * @return string
  809. * @throws SodiumException
  810. * @throws TypeError
  811. */
  812. function sodium_crypto_sign_publickey($keypair)
  813. {
  814. return ParagonIE_Sodium_Compat::crypto_sign_publickey($keypair);
  815. }
  816. }
  817. if (!is_callable('sodium_crypto_sign_publickey_from_secretkey')) {
  818. /**
  819. * @see ParagonIE_Sodium_Compat::crypto_sign_publickey_from_secretkey()
  820. * @param string $sk
  821. * @return string
  822. * @throws SodiumException
  823. * @throws TypeError
  824. */
  825. function sodium_crypto_sign_publickey_from_secretkey($sk)
  826. {
  827. return ParagonIE_Sodium_Compat::crypto_sign_publickey_from_secretkey($sk);
  828. }
  829. }
  830. if (!is_callable('sodium_crypto_sign_secretkey')) {
  831. /**
  832. * @see ParagonIE_Sodium_Compat::crypto_sign_secretkey()
  833. * @param string $keypair
  834. * @return string
  835. * @throws SodiumException
  836. * @throws TypeError
  837. */
  838. function sodium_crypto_sign_secretkey($keypair)
  839. {
  840. return ParagonIE_Sodium_Compat::crypto_sign_secretkey($keypair);
  841. }
  842. }
  843. if (!is_callable('sodium_crypto_sign_seed_keypair')) {
  844. /**
  845. * @see ParagonIE_Sodium_Compat::crypto_sign_seed_keypair()
  846. * @param string $seed
  847. * @return string
  848. * @throws SodiumException
  849. * @throws TypeError
  850. */
  851. function sodium_crypto_sign_seed_keypair($seed)
  852. {
  853. return ParagonIE_Sodium_Compat::crypto_sign_seed_keypair($seed);
  854. }
  855. }
  856. if (!is_callable('sodium_crypto_sign_verify_detached')) {
  857. /**
  858. * @see ParagonIE_Sodium_Compat::crypto_sign_verify_detached()
  859. * @param string $signature
  860. * @param string $message
  861. * @param string $pk
  862. * @return bool
  863. * @throws SodiumException
  864. * @throws TypeError
  865. */
  866. function sodium_crypto_sign_verify_detached($signature, $message, $pk)
  867. {
  868. return ParagonIE_Sodium_Compat::crypto_sign_verify_detached($signature, $message, $pk);
  869. }
  870. }
  871. if (!is_callable('sodium_crypto_sign_ed25519_pk_to_curve25519')) {
  872. /**
  873. * @see ParagonIE_Sodium_Compat::crypto_sign_ed25519_pk_to_curve25519()
  874. * @param string $pk
  875. * @return string
  876. * @throws SodiumException
  877. * @throws TypeError
  878. */
  879. function sodium_crypto_sign_ed25519_pk_to_curve25519($pk)
  880. {
  881. return ParagonIE_Sodium_Compat::crypto_sign_ed25519_pk_to_curve25519($pk);
  882. }
  883. }
  884. if (!is_callable('sodium_crypto_sign_ed25519_sk_to_curve25519')) {
  885. /**
  886. * @see ParagonIE_Sodium_Compat::crypto_sign_ed25519_sk_to_curve25519()
  887. * @param string $sk
  888. * @return string
  889. * @throws SodiumException
  890. * @throws TypeError
  891. */
  892. function sodium_crypto_sign_ed25519_sk_to_curve25519($sk)
  893. {
  894. return ParagonIE_Sodium_Compat::crypto_sign_ed25519_sk_to_curve25519($sk);
  895. }
  896. }
  897. if (!is_callable('sodium_crypto_stream')) {
  898. /**
  899. * @see ParagonIE_Sodium_Compat::crypto_stream()
  900. * @param int $len
  901. * @param string $nonce
  902. * @param string $key
  903. * @return string
  904. * @throws SodiumException
  905. * @throws TypeError
  906. */
  907. function sodium_crypto_stream($len, $nonce, $key)
  908. {
  909. return ParagonIE_Sodium_Compat::crypto_stream($len, $nonce, $key);
  910. }
  911. }
  912. if (!is_callable('sodium_crypto_stream_keygen')) {
  913. /**
  914. * @see ParagonIE_Sodium_Compat::crypto_stream_keygen()
  915. * @return string
  916. */
  917. function sodium_crypto_stream_keygen()
  918. {
  919. return ParagonIE_Sodium_Compat::crypto_stream_keygen();
  920. }
  921. }
  922. if (!is_callable('sodium_crypto_stream_xor')) {
  923. /**
  924. * @see ParagonIE_Sodium_Compat::crypto_stream_xor()
  925. * @param string $message
  926. * @param string $nonce
  927. * @param string $key
  928. * @return string
  929. * @throws SodiumException
  930. * @throws TypeError
  931. */
  932. function sodium_crypto_stream_xor($message, $nonce, $key)
  933. {
  934. return ParagonIE_Sodium_Compat::crypto_stream_xor($message, $nonce, $key);
  935. }
  936. }
  937. if (!is_callable('sodium_hex2bin')) {
  938. /**
  939. * @see ParagonIE_Sodium_Compat::hex2bin()
  940. * @param string $string
  941. * @return string
  942. * @throws SodiumException
  943. * @throws TypeError
  944. */
  945. function sodium_hex2bin($string)
  946. {
  947. return ParagonIE_Sodium_Compat::hex2bin($string);
  948. }
  949. }
  950. if (!is_callable('sodium_increment')) {
  951. /**
  952. * @see ParagonIE_Sodium_Compat::increment()
  953. * @param &string $string
  954. * @return void
  955. * @throws SodiumException
  956. * @throws TypeError
  957. */
  958. function sodium_increment(&$string)
  959. {
  960. ParagonIE_Sodium_Compat::increment($string);
  961. }
  962. }
  963. if (!is_callable('sodium_library_version_major')) {
  964. /**
  965. * @see ParagonIE_Sodium_Compat::library_version_major()
  966. * @return int
  967. */
  968. function sodium_library_version_major()
  969. {
  970. return ParagonIE_Sodium_Compat::library_version_major();
  971. }
  972. }
  973. if (!is_callable('sodium_library_version_minor')) {
  974. /**
  975. * @see ParagonIE_Sodium_Compat::library_version_minor()
  976. * @return int
  977. */
  978. function sodium_library_version_minor()
  979. {
  980. return ParagonIE_Sodium_Compat::library_version_minor();
  981. }
  982. }
  983. if (!is_callable('sodium_version_string')) {
  984. /**
  985. * @see ParagonIE_Sodium_Compat::version_string()
  986. * @return string
  987. */
  988. function sodium_version_string()
  989. {
  990. return ParagonIE_Sodium_Compat::version_string();
  991. }
  992. }
  993. if (!is_callable('sodium_memcmp')) {
  994. /**
  995. * @see ParagonIE_Sodium_Compat::memcmp()
  996. * @param string $a
  997. * @param string $b
  998. * @return int
  999. * @throws SodiumException
  1000. * @throws TypeError
  1001. */
  1002. function sodium_memcmp($a, $b)
  1003. {
  1004. return ParagonIE_Sodium_Compat::memcmp($a, $b);
  1005. }
  1006. }
  1007. if (!is_callable('sodium_memzero')) {
  1008. /**
  1009. * @see ParagonIE_Sodium_Compat::memzero()
  1010. * @param string &$str
  1011. * @return void
  1012. * @throws SodiumException
  1013. * @throws TypeError
  1014. */
  1015. function sodium_memzero(&$str)
  1016. {
  1017. ParagonIE_Sodium_Compat::memzero($str);
  1018. }
  1019. }
  1020. if (!is_callable('sodium_randombytes_buf')) {
  1021. /**
  1022. * @see ParagonIE_Sodium_Compat::randombytes_buf()
  1023. * @param int $amount
  1024. * @return string
  1025. * @throws Exception
  1026. */
  1027. function sodium_randombytes_buf($amount)
  1028. {
  1029. return ParagonIE_Sodium_Compat::randombytes_buf($amount);
  1030. }
  1031. }
  1032. if (!is_callable('sodium_randombytes_uniform')) {
  1033. /**
  1034. * @see ParagonIE_Sodium_Compat::randombytes_uniform()
  1035. * @param int $upperLimit
  1036. * @return int
  1037. * @throws Exception
  1038. */
  1039. function sodium_randombytes_uniform($upperLimit)
  1040. {
  1041. return ParagonIE_Sodium_Compat::randombytes_uniform($upperLimit);
  1042. }
  1043. }
  1044. if (!is_callable('sodium_randombytes_random16')) {
  1045. /**
  1046. * @see ParagonIE_Sodium_Compat::randombytes_random16()
  1047. * @return int
  1048. */
  1049. function sodium_randombytes_random16()
  1050. {
  1051. return ParagonIE_Sodium_Compat::randombytes_random16();
  1052. }
  1053. }