php72compat.php 31 KB

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