php72compat.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433
  1. <?php
  2. require_once dirname(dirname(__FILE__)) . '/autoload.php';
  3. /**
  4. * This file will monkey patch the pure-PHP implementation in place of the
  5. * PECL functions and constants, but only if they do not already exist.
  6. *
  7. * Thus, the functions or constants just proxy to the appropriate
  8. * ParagonIE_Sodium_Compat method or class constant, respectively.
  9. */
  10. foreach (array(
  11. 'BASE64_VARIANT_ORIGINAL',
  12. 'BASE64_VARIANT_ORIGINAL_NO_PADDING',
  13. 'BASE64_VARIANT_URLSAFE',
  14. 'BASE64_VARIANT_URLSAFE_NO_PADDING',
  15. 'CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES',
  16. 'CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES',
  17. 'CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES',
  18. 'CRYPTO_AEAD_CHACHA20POLY1305_ABYTES',
  19. 'CRYPTO_AEAD_AES256GCM_KEYBYTES',
  20. 'CRYPTO_AEAD_AES256GCM_NSECBYTES',
  21. 'CRYPTO_AEAD_AES256GCM_NPUBBYTES',
  22. 'CRYPTO_AEAD_AES256GCM_ABYTES',
  23. 'CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES',
  24. 'CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES',
  25. 'CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES',
  26. 'CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES',
  27. 'CRYPTO_AEAD_XCHACHA20POLY1305_IETF_KEYBYTES',
  28. 'CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NSECBYTES',
  29. 'CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES',
  30. 'CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES',
  31. 'CRYPTO_AUTH_BYTES',
  32. 'CRYPTO_AUTH_KEYBYTES',
  33. 'CRYPTO_BOX_SEALBYTES',
  34. 'CRYPTO_BOX_SECRETKEYBYTES',
  35. 'CRYPTO_BOX_PUBLICKEYBYTES',
  36. 'CRYPTO_BOX_KEYPAIRBYTES',
  37. 'CRYPTO_BOX_MACBYTES',
  38. 'CRYPTO_BOX_NONCEBYTES',
  39. 'CRYPTO_BOX_SEEDBYTES',
  40. 'CRYPTO_KDF_BYTES_MIN',
  41. 'CRYPTO_KDF_BYTES_MAX',
  42. 'CRYPTO_KDF_CONTEXTBYTES',
  43. 'CRYPTO_KDF_KEYBYTES',
  44. 'CRYPTO_KX_BYTES',
  45. 'CRYPTO_KX_KEYPAIRBYTES',
  46. 'CRYPTO_KX_PRIMITIVE',
  47. 'CRYPTO_KX_SEEDBYTES',
  48. 'CRYPTO_KX_PUBLICKEYBYTES',
  49. 'CRYPTO_KX_SECRETKEYBYTES',
  50. 'CRYPTO_KX_SESSIONKEYBYTES',
  51. 'CRYPTO_GENERICHASH_BYTES',
  52. 'CRYPTO_GENERICHASH_BYTES_MIN',
  53. 'CRYPTO_GENERICHASH_BYTES_MAX',
  54. 'CRYPTO_GENERICHASH_KEYBYTES',
  55. 'CRYPTO_GENERICHASH_KEYBYTES_MIN',
  56. 'CRYPTO_GENERICHASH_KEYBYTES_MAX',
  57. 'CRYPTO_PWHASH_SALTBYTES',
  58. 'CRYPTO_PWHASH_STRPREFIX',
  59. 'CRYPTO_PWHASH_ALG_ARGON2I13',
  60. 'CRYPTO_PWHASH_ALG_ARGON2ID13',
  61. 'CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE',
  62. 'CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE',
  63. 'CRYPTO_PWHASH_MEMLIMIT_MODERATE',
  64. 'CRYPTO_PWHASH_OPSLIMIT_MODERATE',
  65. 'CRYPTO_PWHASH_MEMLIMIT_SENSITIVE',
  66. 'CRYPTO_PWHASH_OPSLIMIT_SENSITIVE',
  67. 'CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES',
  68. 'CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX',
  69. 'CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_INTERACTIVE',
  70. 'CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_INTERACTIVE',
  71. 'CRYPTO_PWHASH_SCRYPTSALSA208SHA256_MEMLIMIT_SENSITIVE',
  72. 'CRYPTO_PWHASH_SCRYPTSALSA208SHA256_OPSLIMIT_SENSITIVE',
  73. 'CRYPTO_SCALARMULT_BYTES',
  74. 'CRYPTO_SCALARMULT_SCALARBYTES',
  75. 'CRYPTO_SHORTHASH_BYTES',
  76. 'CRYPTO_SHORTHASH_KEYBYTES',
  77. 'CRYPTO_SECRETBOX_KEYBYTES',
  78. 'CRYPTO_SECRETBOX_MACBYTES',
  79. 'CRYPTO_SECRETBOX_NONCEBYTES',
  80. 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_ABYTES',
  81. 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_HEADERBYTES',
  82. 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_KEYBYTES',
  83. 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PUSH',
  84. 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_PULL',
  85. 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_REKEY',
  86. 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_TAG_FINAL',
  87. 'CRYPTO_SECRETSTREAM_XCHACHA20POLY1305_MESSAGEBYTES_MAX',
  88. 'CRYPTO_SIGN_BYTES',
  89. 'CRYPTO_SIGN_SEEDBYTES',
  90. 'CRYPTO_SIGN_PUBLICKEYBYTES',
  91. 'CRYPTO_SIGN_SECRETKEYBYTES',
  92. 'CRYPTO_SIGN_KEYPAIRBYTES',
  93. 'CRYPTO_STREAM_KEYBYTES',
  94. 'CRYPTO_STREAM_NONCEBYTES',
  95. 'CRYPTO_STREAM_XCHACHA20_KEYBYTES',
  96. 'CRYPTO_STREAM_XCHACHA20_NONCEBYTES',
  97. 'LIBRARY_MAJOR_VERSION',
  98. 'LIBRARY_MINOR_VERSION',
  99. 'LIBRARY_VERSION_MAJOR',
  100. 'LIBRARY_VERSION_MINOR',
  101. 'VERSION_STRING'
  102. ) as $constant
  103. ) {
  104. if (!defined("SODIUM_$constant") && defined("ParagonIE_Sodium_Compat::$constant")) {
  105. define("SODIUM_$constant", constant("ParagonIE_Sodium_Compat::$constant"));
  106. }
  107. }
  108. if (!is_callable('sodium_add')) {
  109. /**
  110. * @see ParagonIE_Sodium_Compat::add()
  111. * @param string $string1
  112. * @param string $string2
  113. * @return void
  114. * @throws SodiumException
  115. */
  116. function sodium_add(&$string1, $string2)
  117. {
  118. ParagonIE_Sodium_Compat::add($string1, $string2);
  119. }
  120. }
  121. if (!is_callable('sodium_base642bin')) {
  122. /**
  123. * @see ParagonIE_Sodium_Compat::bin2base64()
  124. * @param string $string
  125. * @param int $variant
  126. * @param string $ignore
  127. * @return string
  128. * @throws SodiumException
  129. * @throws TypeError
  130. */
  131. function sodium_base642bin($string, $variant, $ignore ='')
  132. {
  133. return ParagonIE_Sodium_Compat::base642bin($string, $variant, $ignore);
  134. }
  135. }
  136. if (!is_callable('sodium_bin2base64')) {
  137. /**
  138. * @see ParagonIE_Sodium_Compat::bin2base64()
  139. * @param string $string
  140. * @param int $variant
  141. * @return string
  142. * @throws SodiumException
  143. * @throws TypeError
  144. */
  145. function sodium_bin2base64($string, $variant)
  146. {
  147. return ParagonIE_Sodium_Compat::bin2base64($string, $variant);
  148. }
  149. }
  150. if (!is_callable('sodium_bin2hex')) {
  151. /**
  152. * @see ParagonIE_Sodium_Compat::hex2bin()
  153. * @param string $string
  154. * @return string
  155. * @throws SodiumException
  156. * @throws TypeError
  157. */
  158. function sodium_bin2hex($string)
  159. {
  160. return ParagonIE_Sodium_Compat::bin2hex($string);
  161. }
  162. }
  163. if (!is_callable('sodium_compare')) {
  164. /**
  165. * @see ParagonIE_Sodium_Compat::compare()
  166. * @param string $string1
  167. * @param string $string2
  168. * @return int
  169. * @throws SodiumException
  170. * @throws TypeError
  171. */
  172. function sodium_compare($string1, $string2)
  173. {
  174. return ParagonIE_Sodium_Compat::compare($string1, $string2);
  175. }
  176. }
  177. if (!is_callable('sodium_crypto_aead_aes256gcm_decrypt')) {
  178. /**
  179. * @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt()
  180. * @param string $ciphertext
  181. * @param string $additional_data
  182. * @param string $nonce
  183. * @param string $key
  184. * @return string|bool
  185. */
  186. function sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $additional_data, $nonce, $key)
  187. {
  188. try {
  189. return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt(
  190. $ciphertext,
  191. $additional_data,
  192. $nonce,
  193. $key
  194. );
  195. } catch (Error $ex) {
  196. return false;
  197. } catch (Exception $ex) {
  198. if (($ex instanceof SodiumException) && ($ex->getMessage() === 'AES-256-GCM is not available')) {
  199. throw $ex;
  200. }
  201. return false;
  202. }
  203. }
  204. }
  205. if (!is_callable('sodium_crypto_aead_aes256gcm_encrypt')) {
  206. /**
  207. * @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_encrypt()
  208. * @param string $message
  209. * @param string $additional_data
  210. * @param string $nonce
  211. * @param string $key
  212. * @return string
  213. * @throws SodiumException
  214. * @throws TypeError
  215. */
  216. function sodium_crypto_aead_aes256gcm_encrypt($message, $additional_data, $nonce, $key)
  217. {
  218. return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_encrypt($message, $additional_data, $nonce, $key);
  219. }
  220. }
  221. if (!is_callable('sodium_crypto_aead_aes256gcm_is_available')) {
  222. /**
  223. * @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_is_available()
  224. * @return bool
  225. */
  226. function sodium_crypto_aead_aes256gcm_is_available()
  227. {
  228. return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_is_available();
  229. }
  230. }
  231. if (!is_callable('sodium_crypto_aead_chacha20poly1305_decrypt')) {
  232. /**
  233. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt()
  234. * @param string $ciphertext
  235. * @param string $additional_data
  236. * @param string $nonce
  237. * @param string $key
  238. * @return string|bool
  239. */
  240. function sodium_crypto_aead_chacha20poly1305_decrypt($ciphertext, $additional_data, $nonce, $key)
  241. {
  242. try {
  243. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt(
  244. $ciphertext,
  245. $additional_data,
  246. $nonce,
  247. $key
  248. );
  249. } catch (Error $ex) {
  250. return false;
  251. } catch (Exception $ex) {
  252. return false;
  253. }
  254. }
  255. }
  256. if (!is_callable('sodium_crypto_aead_chacha20poly1305_encrypt')) {
  257. /**
  258. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_encrypt()
  259. * @param string $message
  260. * @param string $additional_data
  261. * @param string $nonce
  262. * @param string $key
  263. * @return string
  264. * @throws SodiumException
  265. * @throws TypeError
  266. */
  267. function sodium_crypto_aead_chacha20poly1305_encrypt($message, $additional_data, $nonce, $key)
  268. {
  269. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_encrypt(
  270. $message,
  271. $additional_data,
  272. $nonce,
  273. $key
  274. );
  275. }
  276. }
  277. if (!is_callable('sodium_crypto_aead_chacha20poly1305_keygen')) {
  278. /**
  279. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_keygen()
  280. * @return string
  281. * @throws Exception
  282. */
  283. function sodium_crypto_aead_chacha20poly1305_keygen()
  284. {
  285. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_keygen();
  286. }
  287. }
  288. if (!is_callable('sodium_crypto_aead_chacha20poly1305_ietf_decrypt')) {
  289. /**
  290. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt()
  291. * @param string $message
  292. * @param string $additional_data
  293. * @param string $nonce
  294. * @param string $key
  295. * @return string|bool
  296. */
  297. function sodium_crypto_aead_chacha20poly1305_ietf_decrypt($message, $additional_data, $nonce, $key)
  298. {
  299. try {
  300. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt(
  301. $message,
  302. $additional_data,
  303. $nonce,
  304. $key
  305. );
  306. } catch (Error $ex) {
  307. return false;
  308. } catch (Exception $ex) {
  309. return false;
  310. }
  311. }
  312. }
  313. if (!is_callable('sodium_crypto_aead_chacha20poly1305_ietf_encrypt')) {
  314. /**
  315. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt()
  316. * @param string $message
  317. * @param string $additional_data
  318. * @param string $nonce
  319. * @param string $key
  320. * @return string
  321. * @throws SodiumException
  322. * @throws TypeError
  323. */
  324. function sodium_crypto_aead_chacha20poly1305_ietf_encrypt($message, $additional_data, $nonce, $key)
  325. {
  326. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt(
  327. $message,
  328. $additional_data,
  329. $nonce,
  330. $key
  331. );
  332. }
  333. }
  334. if (!is_callable('sodium_crypto_aead_chacha20poly1305_ietf_keygen')) {
  335. /**
  336. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_keygen()
  337. * @return string
  338. * @throws Exception
  339. */
  340. function sodium_crypto_aead_chacha20poly1305_ietf_keygen()
  341. {
  342. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_keygen();
  343. }
  344. }
  345. if (!is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_decrypt')) {
  346. /**
  347. * @see ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_decrypt()
  348. * @param string $ciphertext
  349. * @param string $additional_data
  350. * @param string $nonce
  351. * @param string $key
  352. * @return string|bool
  353. */
  354. function sodium_crypto_aead_xchacha20poly1305_ietf_decrypt($ciphertext, $additional_data, $nonce, $key)
  355. {
  356. try {
  357. return ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_decrypt(
  358. $ciphertext,
  359. $additional_data,
  360. $nonce,
  361. $key,
  362. true
  363. );
  364. } catch (Error $ex) {
  365. return false;
  366. } catch (Exception $ex) {
  367. return false;
  368. }
  369. }
  370. }
  371. if (!is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')) {
  372. /**
  373. * @see ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_encrypt()
  374. * @param string $message
  375. * @param string $additional_data
  376. * @param string $nonce
  377. * @param string $key
  378. * @return string
  379. * @throws SodiumException
  380. * @throws TypeError
  381. */
  382. function sodium_crypto_aead_xchacha20poly1305_ietf_encrypt(
  383. $message,
  384. $additional_data,
  385. $nonce,
  386. $key
  387. ) {
  388. return ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_encrypt(
  389. $message,
  390. $additional_data,
  391. $nonce,
  392. $key,
  393. true
  394. );
  395. }
  396. }
  397. if (!is_callable('sodium_crypto_aead_xchacha20poly1305_ietf_keygen')) {
  398. /**
  399. * @see ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_keygen()
  400. * @return string
  401. * @throws Exception
  402. */
  403. function sodium_crypto_aead_xchacha20poly1305_ietf_keygen()
  404. {
  405. return ParagonIE_Sodium_Compat::crypto_aead_xchacha20poly1305_ietf_keygen();
  406. }
  407. }
  408. if (!is_callable('sodium_crypto_auth')) {
  409. /**
  410. * @see ParagonIE_Sodium_Compat::crypto_auth()
  411. * @param string $message
  412. * @param string $key
  413. * @return string
  414. * @throws SodiumException
  415. * @throws TypeError
  416. */
  417. function sodium_crypto_auth($message, $key)
  418. {
  419. return ParagonIE_Sodium_Compat::crypto_auth($message, $key);
  420. }
  421. }
  422. if (!is_callable('sodium_crypto_auth_keygen')) {
  423. /**
  424. * @see ParagonIE_Sodium_Compat::crypto_auth_keygen()
  425. * @return string
  426. * @throws Exception
  427. */
  428. function sodium_crypto_auth_keygen()
  429. {
  430. return ParagonIE_Sodium_Compat::crypto_auth_keygen();
  431. }
  432. }
  433. if (!is_callable('sodium_crypto_auth_verify')) {
  434. /**
  435. * @see ParagonIE_Sodium_Compat::crypto_auth_verify()
  436. * @param string $mac
  437. * @param string $message
  438. * @param string $key
  439. * @return bool
  440. * @throws SodiumException
  441. * @throws TypeError
  442. */
  443. function sodium_crypto_auth_verify($mac, $message, $key)
  444. {
  445. return ParagonIE_Sodium_Compat::crypto_auth_verify($mac, $message, $key);
  446. }
  447. }
  448. if (!is_callable('sodium_crypto_box')) {
  449. /**
  450. * @see ParagonIE_Sodium_Compat::crypto_box()
  451. * @param string $message
  452. * @param string $nonce
  453. * @param string $key_pair
  454. * @return string
  455. * @throws SodiumException
  456. * @throws TypeError
  457. */
  458. function sodium_crypto_box($message, $nonce, $key_pair)
  459. {
  460. return ParagonIE_Sodium_Compat::crypto_box($message, $nonce, $key_pair);
  461. }
  462. }
  463. if (!is_callable('sodium_crypto_box_keypair')) {
  464. /**
  465. * @see ParagonIE_Sodium_Compat::crypto_box_keypair()
  466. * @return string
  467. * @throws SodiumException
  468. * @throws TypeError
  469. */
  470. function sodium_crypto_box_keypair()
  471. {
  472. return ParagonIE_Sodium_Compat::crypto_box_keypair();
  473. }
  474. }
  475. if (!is_callable('sodium_crypto_box_keypair_from_secretkey_and_publickey')) {
  476. /**
  477. * @see ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey()
  478. * @param string $secret_key
  479. * @param string $public_key
  480. * @return string
  481. * @throws SodiumException
  482. * @throws TypeError
  483. */
  484. function sodium_crypto_box_keypair_from_secretkey_and_publickey($secret_key, $public_key)
  485. {
  486. return ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey($secret_key, $public_key);
  487. }
  488. }
  489. if (!is_callable('sodium_crypto_box_open')) {
  490. /**
  491. * @see ParagonIE_Sodium_Compat::crypto_box_open()
  492. * @param string $ciphertext
  493. * @param string $nonce
  494. * @param string $key_pair
  495. * @return string|bool
  496. */
  497. function sodium_crypto_box_open($ciphertext, $nonce, $key_pair)
  498. {
  499. try {
  500. return ParagonIE_Sodium_Compat::crypto_box_open($ciphertext, $nonce, $key_pair);
  501. } catch (Error $ex) {
  502. return false;
  503. } catch (Exception $ex) {
  504. return false;
  505. }
  506. }
  507. }
  508. if (!is_callable('sodium_crypto_box_publickey')) {
  509. /**
  510. * @see ParagonIE_Sodium_Compat::crypto_box_publickey()
  511. * @param string $key_pair
  512. * @return string
  513. * @throws SodiumException
  514. * @throws TypeError
  515. */
  516. function sodium_crypto_box_publickey($key_pair)
  517. {
  518. return ParagonIE_Sodium_Compat::crypto_box_publickey($key_pair);
  519. }
  520. }
  521. if (!is_callable('sodium_crypto_box_publickey_from_secretkey')) {
  522. /**
  523. * @see ParagonIE_Sodium_Compat::crypto_box_publickey_from_secretkey()
  524. * @param string $secret_key
  525. * @return string
  526. * @throws SodiumException
  527. * @throws TypeError
  528. */
  529. function sodium_crypto_box_publickey_from_secretkey($secret_key)
  530. {
  531. return ParagonIE_Sodium_Compat::crypto_box_publickey_from_secretkey($secret_key);
  532. }
  533. }
  534. if (!is_callable('sodium_crypto_box_seal')) {
  535. /**
  536. * @see ParagonIE_Sodium_Compat::crypto_box_seal()
  537. * @param string $message
  538. * @param string $public_key
  539. * @return string
  540. * @throws SodiumException
  541. * @throws TypeError
  542. */
  543. function sodium_crypto_box_seal($message, $public_key)
  544. {
  545. return ParagonIE_Sodium_Compat::crypto_box_seal($message, $public_key);
  546. }
  547. }
  548. if (!is_callable('sodium_crypto_box_seal_open')) {
  549. /**
  550. * @see ParagonIE_Sodium_Compat::crypto_box_seal_open()
  551. * @param string $message
  552. * @param string $key_pair
  553. * @return string|bool
  554. * @throws SodiumException
  555. */
  556. function sodium_crypto_box_seal_open($message, $key_pair)
  557. {
  558. try {
  559. return ParagonIE_Sodium_Compat::crypto_box_seal_open($message, $key_pair);
  560. } catch (SodiumException $ex) {
  561. if ($ex->getMessage() === 'Argument 2 must be CRYPTO_BOX_KEYPAIRBYTES long.') {
  562. throw $ex;
  563. }
  564. return false;
  565. }
  566. }
  567. }
  568. if (!is_callable('sodium_crypto_box_secretkey')) {
  569. /**
  570. * @see ParagonIE_Sodium_Compat::crypto_box_secretkey()
  571. * @param string $key_pair
  572. * @return string
  573. * @throws SodiumException
  574. * @throws TypeError
  575. */
  576. function sodium_crypto_box_secretkey($key_pair)
  577. {
  578. return ParagonIE_Sodium_Compat::crypto_box_secretkey($key_pair);
  579. }
  580. }
  581. if (!is_callable('sodium_crypto_box_seed_keypair')) {
  582. /**
  583. * @see ParagonIE_Sodium_Compat::crypto_box_seed_keypair()
  584. * @param string $seed
  585. * @return string
  586. * @throws SodiumException
  587. * @throws TypeError
  588. */
  589. function sodium_crypto_box_seed_keypair($seed)
  590. {
  591. return ParagonIE_Sodium_Compat::crypto_box_seed_keypair($seed);
  592. }
  593. }
  594. if (!is_callable('sodium_crypto_generichash')) {
  595. /**
  596. * @see ParagonIE_Sodium_Compat::crypto_generichash()
  597. * @param string $message
  598. * @param string|null $key
  599. * @param int $length
  600. * @return string
  601. * @throws SodiumException
  602. * @throws TypeError
  603. */
  604. function sodium_crypto_generichash($message, $key = null, $length = 32)
  605. {
  606. return ParagonIE_Sodium_Compat::crypto_generichash($message, $key, $length);
  607. }
  608. }
  609. if (!is_callable('sodium_crypto_generichash_final')) {
  610. /**
  611. * @see ParagonIE_Sodium_Compat::crypto_generichash_final()
  612. * @param string|null $state
  613. * @param int $outputLength
  614. * @return string
  615. * @throws SodiumException
  616. * @throws TypeError
  617. */
  618. function sodium_crypto_generichash_final(&$state, $outputLength = 32)
  619. {
  620. return ParagonIE_Sodium_Compat::crypto_generichash_final($state, $outputLength);
  621. }
  622. }
  623. if (!is_callable('sodium_crypto_generichash_init')) {
  624. /**
  625. * @see ParagonIE_Sodium_Compat::crypto_generichash_init()
  626. * @param string|null $key
  627. * @param int $length
  628. * @return string
  629. * @throws SodiumException
  630. * @throws TypeError
  631. */
  632. function sodium_crypto_generichash_init($key = null, $length = 32)
  633. {
  634. return ParagonIE_Sodium_Compat::crypto_generichash_init($key, $length);
  635. }
  636. }
  637. if (!is_callable('sodium_crypto_generichash_keygen')) {
  638. /**
  639. * @see ParagonIE_Sodium_Compat::crypto_generichash_keygen()
  640. * @return string
  641. * @throws Exception
  642. */
  643. function sodium_crypto_generichash_keygen()
  644. {
  645. return ParagonIE_Sodium_Compat::crypto_generichash_keygen();
  646. }
  647. }
  648. if (!is_callable('sodium_crypto_generichash_update')) {
  649. /**
  650. * @see ParagonIE_Sodium_Compat::crypto_generichash_update()
  651. * @param string|null $state
  652. * @param string $message
  653. * @return void
  654. * @throws SodiumException
  655. * @throws TypeError
  656. */
  657. function sodium_crypto_generichash_update(&$state, $message = '')
  658. {
  659. ParagonIE_Sodium_Compat::crypto_generichash_update($state, $message);
  660. }
  661. }
  662. if (!is_callable('sodium_crypto_kdf_keygen')) {
  663. /**
  664. * @see ParagonIE_Sodium_Compat::crypto_kdf_keygen()
  665. * @return string
  666. * @throws Exception
  667. */
  668. function sodium_crypto_kdf_keygen()
  669. {
  670. return ParagonIE_Sodium_Compat::crypto_kdf_keygen();
  671. }
  672. }
  673. if (!is_callable('sodium_crypto_kdf_derive_from_key')) {
  674. /**
  675. * @see ParagonIE_Sodium_Compat::crypto_kdf_derive_from_key()
  676. * @param int $subkey_length
  677. * @param int $subkey_id
  678. * @param string $context
  679. * @param string $key
  680. * @return string
  681. * @throws Exception
  682. */
  683. function sodium_crypto_kdf_derive_from_key($subkey_length, $subkey_id, $context, $key)
  684. {
  685. return ParagonIE_Sodium_Compat::crypto_kdf_derive_from_key(
  686. $subkey_length,
  687. $subkey_id,
  688. $context,
  689. $key
  690. );
  691. }
  692. }
  693. if (!is_callable('sodium_crypto_kx')) {
  694. /**
  695. * @see ParagonIE_Sodium_Compat::crypto_kx()
  696. * @param string $my_secret
  697. * @param string $their_public
  698. * @param string $client_public
  699. * @param string $server_public
  700. * @return string
  701. * @throws SodiumException
  702. * @throws TypeError
  703. */
  704. function sodium_crypto_kx($my_secret, $their_public, $client_public, $server_public)
  705. {
  706. return ParagonIE_Sodium_Compat::crypto_kx(
  707. $my_secret,
  708. $their_public,
  709. $client_public,
  710. $server_public
  711. );
  712. }
  713. }
  714. if (!is_callable('sodium_crypto_kx_seed_keypair')) {
  715. /**
  716. * @param string $seed
  717. * @return string
  718. * @throws Exception
  719. */
  720. function sodium_crypto_kx_seed_keypair($seed)
  721. {
  722. return ParagonIE_Sodium_Compat::crypto_kx_seed_keypair($seed);
  723. }
  724. }
  725. if (!is_callable('sodium_crypto_kx_keypair')) {
  726. /**
  727. * @return string
  728. * @throws Exception
  729. */
  730. function sodium_crypto_kx_keypair()
  731. {
  732. return ParagonIE_Sodium_Compat::crypto_kx_keypair();
  733. }
  734. }
  735. if (!is_callable('sodium_crypto_kx_client_session_keys')) {
  736. /**
  737. * @param string $client_key_pair
  738. * @param string $server_key
  739. * @return array{0: string, 1: string}
  740. * @throws SodiumException
  741. */
  742. function sodium_crypto_kx_client_session_keys($client_key_pair, $server_key)
  743. {
  744. return ParagonIE_Sodium_Compat::crypto_kx_client_session_keys($client_key_pair, $server_key);
  745. }
  746. }
  747. if (!is_callable('sodium_crypto_kx_server_session_keys')) {
  748. /**
  749. * @param string $server_key_pair
  750. * @param string $client_key
  751. * @return array{0: string, 1: string}
  752. * @throws SodiumException
  753. */
  754. function sodium_crypto_kx_server_session_keys($server_key_pair, $client_key)
  755. {
  756. return ParagonIE_Sodium_Compat::crypto_kx_server_session_keys($server_key_pair, $client_key);
  757. }
  758. }
  759. if (!is_callable('sodium_crypto_kx_secretkey')) {
  760. /**
  761. * @param string $key_pair
  762. * @return string
  763. * @throws Exception
  764. */
  765. function sodium_crypto_kx_secretkey($key_pair)
  766. {
  767. return ParagonIE_Sodium_Compat::crypto_kx_secretkey($key_pair);
  768. }
  769. }
  770. if (!is_callable('sodium_crypto_kx_publickey')) {
  771. /**
  772. * @param string $key_pair
  773. * @return string
  774. * @throws Exception
  775. */
  776. function sodium_crypto_kx_publickey($key_pair)
  777. {
  778. return ParagonIE_Sodium_Compat::crypto_kx_publickey($key_pair);
  779. }
  780. }
  781. if (!is_callable('sodium_crypto_pwhash')) {
  782. /**
  783. * @see ParagonIE_Sodium_Compat::crypto_pwhash()
  784. * @param int $length
  785. * @param string $passwd
  786. * @param string $salt
  787. * @param int $opslimit
  788. * @param int $memlimit
  789. * @param int|null $algo
  790. * @return string
  791. * @throws SodiumException
  792. * @throws TypeError
  793. */
  794. function sodium_crypto_pwhash($length, $passwd, $salt, $opslimit, $memlimit, $algo = null)
  795. {
  796. return ParagonIE_Sodium_Compat::crypto_pwhash($length, $passwd, $salt, $opslimit, $memlimit, $algo);
  797. }
  798. }
  799. if (!is_callable('sodium_crypto_pwhash_str')) {
  800. /**
  801. * @see ParagonIE_Sodium_Compat::crypto_pwhash_str()
  802. * @param string $passwd
  803. * @param int $opslimit
  804. * @param int $memlimit
  805. * @return string
  806. * @throws SodiumException
  807. * @throws TypeError
  808. */
  809. function sodium_crypto_pwhash_str($passwd, $opslimit, $memlimit)
  810. {
  811. return ParagonIE_Sodium_Compat::crypto_pwhash_str($passwd, $opslimit, $memlimit);
  812. }
  813. }
  814. if (!is_callable('sodium_crypto_pwhash_str_needs_rehash')) {
  815. /**
  816. * @see ParagonIE_Sodium_Compat::crypto_pwhash_str_needs_rehash()
  817. * @param string $hash
  818. * @param int $opslimit
  819. * @param int $memlimit
  820. * @return bool
  821. *
  822. * @throws SodiumException
  823. */
  824. function sodium_crypto_pwhash_str_needs_rehash($hash, $opslimit, $memlimit)
  825. {
  826. return ParagonIE_Sodium_Compat::crypto_pwhash_str_needs_rehash($hash, $opslimit, $memlimit);
  827. }
  828. }
  829. if (!is_callable('sodium_crypto_pwhash_str_verify')) {
  830. /**
  831. * @see ParagonIE_Sodium_Compat::crypto_pwhash_str_verify()
  832. * @param string $passwd
  833. * @param string $hash
  834. * @return bool
  835. * @throws SodiumException
  836. * @throws TypeError
  837. */
  838. function sodium_crypto_pwhash_str_verify($passwd, $hash)
  839. {
  840. return ParagonIE_Sodium_Compat::crypto_pwhash_str_verify($passwd, $hash);
  841. }
  842. }
  843. if (!is_callable('sodium_crypto_pwhash_scryptsalsa208sha256')) {
  844. /**
  845. * @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256()
  846. * @param int $length
  847. * @param string $passwd
  848. * @param string $salt
  849. * @param int $opslimit
  850. * @param int $memlimit
  851. * @return string
  852. * @throws SodiumException
  853. * @throws TypeError
  854. */
  855. function sodium_crypto_pwhash_scryptsalsa208sha256($length, $passwd, $salt, $opslimit, $memlimit)
  856. {
  857. return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256(
  858. $length,
  859. $passwd,
  860. $salt,
  861. $opslimit,
  862. $memlimit
  863. );
  864. }
  865. }
  866. if (!is_callable('sodium_crypto_pwhash_scryptsalsa208sha256_str')) {
  867. /**
  868. * @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str()
  869. * @param string $passwd
  870. * @param int $opslimit
  871. * @param int $memlimit
  872. * @return string
  873. * @throws SodiumException
  874. * @throws TypeError
  875. */
  876. function sodium_crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit)
  877. {
  878. return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit);
  879. }
  880. }
  881. if (!is_callable('sodium_crypto_pwhash_scryptsalsa208sha256_str_verify')) {
  882. /**
  883. * @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str_verify()
  884. * @param string $passwd
  885. * @param string $hash
  886. * @return bool
  887. * @throws SodiumException
  888. * @throws TypeError
  889. */
  890. function sodium_crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash)
  891. {
  892. return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash);
  893. }
  894. }
  895. if (!is_callable('sodium_crypto_scalarmult')) {
  896. /**
  897. * @see ParagonIE_Sodium_Compat::crypto_scalarmult()
  898. * @param string $n
  899. * @param string $p
  900. * @return string
  901. * @throws SodiumException
  902. * @throws TypeError
  903. */
  904. function sodium_crypto_scalarmult($n, $p)
  905. {
  906. return ParagonIE_Sodium_Compat::crypto_scalarmult($n, $p);
  907. }
  908. }
  909. if (!is_callable('sodium_crypto_scalarmult_base')) {
  910. /**
  911. * @see ParagonIE_Sodium_Compat::crypto_scalarmult_base()
  912. * @param string $n
  913. * @return string
  914. * @throws SodiumException
  915. * @throws TypeError
  916. */
  917. function sodium_crypto_scalarmult_base($n)
  918. {
  919. return ParagonIE_Sodium_Compat::crypto_scalarmult_base($n);
  920. }
  921. }
  922. if (!is_callable('sodium_crypto_secretbox')) {
  923. /**
  924. * @see ParagonIE_Sodium_Compat::crypto_secretbox()
  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_secretbox($message, $nonce, $key)
  933. {
  934. return ParagonIE_Sodium_Compat::crypto_secretbox($message, $nonce, $key);
  935. }
  936. }
  937. if (!is_callable('sodium_crypto_secretbox_keygen')) {
  938. /**
  939. * @see ParagonIE_Sodium_Compat::crypto_secretbox_keygen()
  940. * @return string
  941. * @throws Exception
  942. */
  943. function sodium_crypto_secretbox_keygen()
  944. {
  945. return ParagonIE_Sodium_Compat::crypto_secretbox_keygen();
  946. }
  947. }
  948. if (!is_callable('sodium_crypto_secretbox_open')) {
  949. /**
  950. * @see ParagonIE_Sodium_Compat::crypto_secretbox_open()
  951. * @param string $ciphertext
  952. * @param string $nonce
  953. * @param string $key
  954. * @return string|bool
  955. */
  956. function sodium_crypto_secretbox_open($ciphertext, $nonce, $key)
  957. {
  958. try {
  959. return ParagonIE_Sodium_Compat::crypto_secretbox_open($ciphertext, $nonce, $key);
  960. } catch (Error $ex) {
  961. return false;
  962. } catch (Exception $ex) {
  963. return false;
  964. }
  965. }
  966. }
  967. if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_init_push')) {
  968. /**
  969. * @param string $key
  970. * @return array<int, string>
  971. * @throws SodiumException
  972. */
  973. function sodium_crypto_secretstream_xchacha20poly1305_init_push($key)
  974. {
  975. return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_init_push($key);
  976. }
  977. }
  978. if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_push')) {
  979. /**
  980. * @param string $state
  981. * @param string $message
  982. * @param string $additional_data
  983. * @param int $tag
  984. * @return string
  985. * @throws SodiumException
  986. */
  987. function sodium_crypto_secretstream_xchacha20poly1305_push(
  988. &$state,
  989. $message,
  990. $additional_data = '',
  991. $tag = 0
  992. ) {
  993. return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_push(
  994. $state,
  995. $message,
  996. $additional_data,
  997. $tag
  998. );
  999. }
  1000. }
  1001. if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_init_pull')) {
  1002. /**
  1003. * @param string $header
  1004. * @param string $key
  1005. * @return string
  1006. * @throws Exception
  1007. */
  1008. function sodium_crypto_secretstream_xchacha20poly1305_init_pull($header, $key)
  1009. {
  1010. return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_init_pull($header, $key);
  1011. }
  1012. }
  1013. if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_pull')) {
  1014. /**
  1015. * @param string $state
  1016. * @param string $ciphertext
  1017. * @param string $additional_data
  1018. * @return bool|array{0: string, 1: int}
  1019. * @throws SodiumException
  1020. */
  1021. function sodium_crypto_secretstream_xchacha20poly1305_pull(&$state, $ciphertext, $additional_data = '')
  1022. {
  1023. return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_pull(
  1024. $state,
  1025. $ciphertext,
  1026. $additional_data
  1027. );
  1028. }
  1029. }
  1030. if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_rekey')) {
  1031. /**
  1032. * @param string $state
  1033. * @return void
  1034. * @throws SodiumException
  1035. */
  1036. function sodium_crypto_secretstream_xchacha20poly1305_rekey(&$state)
  1037. {
  1038. ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_rekey($state);
  1039. }
  1040. }
  1041. if (!is_callable('sodium_crypto_secretstream_xchacha20poly1305_keygen')) {
  1042. /**
  1043. * @return string
  1044. * @throws Exception
  1045. */
  1046. function sodium_crypto_secretstream_xchacha20poly1305_keygen()
  1047. {
  1048. return ParagonIE_Sodium_Compat::crypto_secretstream_xchacha20poly1305_keygen();
  1049. }
  1050. }
  1051. if (!is_callable('sodium_crypto_shorthash')) {
  1052. /**
  1053. * @see ParagonIE_Sodium_Compat::crypto_shorthash()
  1054. * @param string $message
  1055. * @param string $key
  1056. * @return string
  1057. * @throws SodiumException
  1058. * @throws TypeError
  1059. */
  1060. function sodium_crypto_shorthash($message, $key = '')
  1061. {
  1062. return ParagonIE_Sodium_Compat::crypto_shorthash($message, $key);
  1063. }
  1064. }
  1065. if (!is_callable('sodium_crypto_shorthash_keygen')) {
  1066. /**
  1067. * @see ParagonIE_Sodium_Compat::crypto_shorthash_keygen()
  1068. * @return string
  1069. * @throws Exception
  1070. */
  1071. function sodium_crypto_shorthash_keygen()
  1072. {
  1073. return ParagonIE_Sodium_Compat::crypto_shorthash_keygen();
  1074. }
  1075. }
  1076. if (!is_callable('sodium_crypto_sign')) {
  1077. /**
  1078. * @see ParagonIE_Sodium_Compat::crypto_sign()
  1079. * @param string $message
  1080. * @param string $secret_key
  1081. * @return string
  1082. * @throws SodiumException
  1083. * @throws TypeError
  1084. */
  1085. function sodium_crypto_sign($message, $secret_key)
  1086. {
  1087. return ParagonIE_Sodium_Compat::crypto_sign($message, $secret_key);
  1088. }
  1089. }
  1090. if (!is_callable('sodium_crypto_sign_detached')) {
  1091. /**
  1092. * @see ParagonIE_Sodium_Compat::crypto_sign_detached()
  1093. * @param string $message
  1094. * @param string $secret_key
  1095. * @return string
  1096. * @throws SodiumException
  1097. * @throws TypeError
  1098. */
  1099. function sodium_crypto_sign_detached($message, $secret_key)
  1100. {
  1101. return ParagonIE_Sodium_Compat::crypto_sign_detached($message, $secret_key);
  1102. }
  1103. }
  1104. if (!is_callable('sodium_crypto_sign_keypair_from_secretkey_and_publickey')) {
  1105. /**
  1106. * @see ParagonIE_Sodium_Compat::crypto_sign_keypair_from_secretkey_and_publickey()
  1107. * @param string $secret_key
  1108. * @param string $public_key
  1109. * @return string
  1110. * @throws SodiumException
  1111. * @throws TypeError
  1112. */
  1113. function sodium_crypto_sign_keypair_from_secretkey_and_publickey($secret_key, $public_key)
  1114. {
  1115. return ParagonIE_Sodium_Compat::crypto_sign_keypair_from_secretkey_and_publickey($secret_key, $public_key);
  1116. }
  1117. }
  1118. if (!is_callable('sodium_crypto_sign_keypair')) {
  1119. /**
  1120. * @see ParagonIE_Sodium_Compat::crypto_sign_keypair()
  1121. * @return string
  1122. * @throws SodiumException
  1123. * @throws TypeError
  1124. */
  1125. function sodium_crypto_sign_keypair()
  1126. {
  1127. return ParagonIE_Sodium_Compat::crypto_sign_keypair();
  1128. }
  1129. }
  1130. if (!is_callable('sodium_crypto_sign_open')) {
  1131. /**
  1132. * @see ParagonIE_Sodium_Compat::crypto_sign_open()
  1133. * @param string $signedMessage
  1134. * @param string $public_key
  1135. * @return string|bool
  1136. */
  1137. function sodium_crypto_sign_open($signedMessage, $public_key)
  1138. {
  1139. try {
  1140. return ParagonIE_Sodium_Compat::crypto_sign_open($signedMessage, $public_key);
  1141. } catch (Error $ex) {
  1142. return false;
  1143. } catch (Exception $ex) {
  1144. return false;
  1145. }
  1146. }
  1147. }
  1148. if (!is_callable('sodium_crypto_sign_publickey')) {
  1149. /**
  1150. * @see ParagonIE_Sodium_Compat::crypto_sign_publickey()
  1151. * @param string $key_pair
  1152. * @return string
  1153. * @throws SodiumException
  1154. * @throws TypeError
  1155. */
  1156. function sodium_crypto_sign_publickey($key_pair)
  1157. {
  1158. return ParagonIE_Sodium_Compat::crypto_sign_publickey($key_pair);
  1159. }
  1160. }
  1161. if (!is_callable('sodium_crypto_sign_publickey_from_secretkey')) {
  1162. /**
  1163. * @see ParagonIE_Sodium_Compat::crypto_sign_publickey_from_secretkey()
  1164. * @param string $secret_key
  1165. * @return string
  1166. * @throws SodiumException
  1167. * @throws TypeError
  1168. */
  1169. function sodium_crypto_sign_publickey_from_secretkey($secret_key)
  1170. {
  1171. return ParagonIE_Sodium_Compat::crypto_sign_publickey_from_secretkey($secret_key);
  1172. }
  1173. }
  1174. if (!is_callable('sodium_crypto_sign_secretkey')) {
  1175. /**
  1176. * @see ParagonIE_Sodium_Compat::crypto_sign_secretkey()
  1177. * @param string $key_pair
  1178. * @return string
  1179. * @throws SodiumException
  1180. * @throws TypeError
  1181. */
  1182. function sodium_crypto_sign_secretkey($key_pair)
  1183. {
  1184. return ParagonIE_Sodium_Compat::crypto_sign_secretkey($key_pair);
  1185. }
  1186. }
  1187. if (!is_callable('sodium_crypto_sign_seed_keypair')) {
  1188. /**
  1189. * @see ParagonIE_Sodium_Compat::crypto_sign_seed_keypair()
  1190. * @param string $seed
  1191. * @return string
  1192. * @throws SodiumException
  1193. * @throws TypeError
  1194. */
  1195. function sodium_crypto_sign_seed_keypair($seed)
  1196. {
  1197. return ParagonIE_Sodium_Compat::crypto_sign_seed_keypair($seed);
  1198. }
  1199. }
  1200. if (!is_callable('sodium_crypto_sign_verify_detached')) {
  1201. /**
  1202. * @see ParagonIE_Sodium_Compat::crypto_sign_verify_detached()
  1203. * @param string $signature
  1204. * @param string $message
  1205. * @param string $public_key
  1206. * @return bool
  1207. * @throws SodiumException
  1208. * @throws TypeError
  1209. */
  1210. function sodium_crypto_sign_verify_detached($signature, $message, $public_key)
  1211. {
  1212. return ParagonIE_Sodium_Compat::crypto_sign_verify_detached($signature, $message, $public_key);
  1213. }
  1214. }
  1215. if (!is_callable('sodium_crypto_sign_ed25519_pk_to_curve25519')) {
  1216. /**
  1217. * @see ParagonIE_Sodium_Compat::crypto_sign_ed25519_pk_to_curve25519()
  1218. * @param string $public_key
  1219. * @return string
  1220. * @throws SodiumException
  1221. * @throws TypeError
  1222. */
  1223. function sodium_crypto_sign_ed25519_pk_to_curve25519($public_key)
  1224. {
  1225. return ParagonIE_Sodium_Compat::crypto_sign_ed25519_pk_to_curve25519($public_key);
  1226. }
  1227. }
  1228. if (!is_callable('sodium_crypto_sign_ed25519_sk_to_curve25519')) {
  1229. /**
  1230. * @see ParagonIE_Sodium_Compat::crypto_sign_ed25519_sk_to_curve25519()
  1231. * @param string $secret_key
  1232. * @return string
  1233. * @throws SodiumException
  1234. * @throws TypeError
  1235. */
  1236. function sodium_crypto_sign_ed25519_sk_to_curve25519($secret_key)
  1237. {
  1238. return ParagonIE_Sodium_Compat::crypto_sign_ed25519_sk_to_curve25519($secret_key);
  1239. }
  1240. }
  1241. if (!is_callable('sodium_crypto_stream')) {
  1242. /**
  1243. * @see ParagonIE_Sodium_Compat::crypto_stream()
  1244. * @param int $length
  1245. * @param string $nonce
  1246. * @param string $key
  1247. * @return string
  1248. * @throws SodiumException
  1249. * @throws TypeError
  1250. */
  1251. function sodium_crypto_stream($length, $nonce, $key)
  1252. {
  1253. return ParagonIE_Sodium_Compat::crypto_stream($length, $nonce, $key);
  1254. }
  1255. }
  1256. if (!is_callable('sodium_crypto_stream_keygen')) {
  1257. /**
  1258. * @see ParagonIE_Sodium_Compat::crypto_stream_keygen()
  1259. * @return string
  1260. * @throws Exception
  1261. */
  1262. function sodium_crypto_stream_keygen()
  1263. {
  1264. return ParagonIE_Sodium_Compat::crypto_stream_keygen();
  1265. }
  1266. }
  1267. if (!is_callable('sodium_crypto_stream_xor')) {
  1268. /**
  1269. * @see ParagonIE_Sodium_Compat::crypto_stream_xor()
  1270. * @param string $message
  1271. * @param string $nonce
  1272. * @param string $key
  1273. * @return string
  1274. * @throws SodiumException
  1275. * @throws TypeError
  1276. */
  1277. function sodium_crypto_stream_xor($message, $nonce, $key)
  1278. {
  1279. return ParagonIE_Sodium_Compat::crypto_stream_xor($message, $nonce, $key);
  1280. }
  1281. }
  1282. require_once dirname(__FILE__) . '/stream-xchacha20.php';
  1283. if (!is_callable('sodium_hex2bin')) {
  1284. /**
  1285. * @see ParagonIE_Sodium_Compat::hex2bin()
  1286. * @param string $string
  1287. * @param string $ignore
  1288. * @return string
  1289. * @throws SodiumException
  1290. * @throws TypeError
  1291. */
  1292. function sodium_hex2bin($string, $ignore = '')
  1293. {
  1294. return ParagonIE_Sodium_Compat::hex2bin($string, $ignore);
  1295. }
  1296. }
  1297. if (!is_callable('sodium_increment')) {
  1298. /**
  1299. * @see ParagonIE_Sodium_Compat::increment()
  1300. * @param string $string
  1301. * @return void
  1302. * @throws SodiumException
  1303. * @throws TypeError
  1304. */
  1305. function sodium_increment(&$string)
  1306. {
  1307. ParagonIE_Sodium_Compat::increment($string);
  1308. }
  1309. }
  1310. if (!is_callable('sodium_library_version_major')) {
  1311. /**
  1312. * @see ParagonIE_Sodium_Compat::library_version_major()
  1313. * @return int
  1314. */
  1315. function sodium_library_version_major()
  1316. {
  1317. return ParagonIE_Sodium_Compat::library_version_major();
  1318. }
  1319. }
  1320. if (!is_callable('sodium_library_version_minor')) {
  1321. /**
  1322. * @see ParagonIE_Sodium_Compat::library_version_minor()
  1323. * @return int
  1324. */
  1325. function sodium_library_version_minor()
  1326. {
  1327. return ParagonIE_Sodium_Compat::library_version_minor();
  1328. }
  1329. }
  1330. if (!is_callable('sodium_version_string')) {
  1331. /**
  1332. * @see ParagonIE_Sodium_Compat::version_string()
  1333. * @return string
  1334. */
  1335. function sodium_version_string()
  1336. {
  1337. return ParagonIE_Sodium_Compat::version_string();
  1338. }
  1339. }
  1340. if (!is_callable('sodium_memcmp')) {
  1341. /**
  1342. * @see ParagonIE_Sodium_Compat::memcmp()
  1343. * @param string $string1
  1344. * @param string $string2
  1345. * @return int
  1346. * @throws SodiumException
  1347. * @throws TypeError
  1348. */
  1349. function sodium_memcmp($string1, $string2)
  1350. {
  1351. return ParagonIE_Sodium_Compat::memcmp($string1, $string2);
  1352. }
  1353. }
  1354. if (!is_callable('sodium_memzero')) {
  1355. /**
  1356. * @see ParagonIE_Sodium_Compat::memzero()
  1357. * @param string $string
  1358. * @return void
  1359. * @throws SodiumException
  1360. * @throws TypeError
  1361. */
  1362. function sodium_memzero(&$string)
  1363. {
  1364. ParagonIE_Sodium_Compat::memzero($string);
  1365. }
  1366. }
  1367. if (!is_callable('sodium_pad')) {
  1368. /**
  1369. * @see ParagonIE_Sodium_Compat::pad()
  1370. * @param string $unpadded
  1371. * @param int $block_size
  1372. * @return string
  1373. * @throws SodiumException
  1374. * @throws TypeError
  1375. */
  1376. function sodium_pad($unpadded, $block_size)
  1377. {
  1378. return ParagonIE_Sodium_Compat::pad($unpadded, $block_size, true);
  1379. }
  1380. }
  1381. if (!is_callable('sodium_unpad')) {
  1382. /**
  1383. * @see ParagonIE_Sodium_Compat::pad()
  1384. * @param string $padded
  1385. * @param int $block_size
  1386. * @return string
  1387. * @throws SodiumException
  1388. * @throws TypeError
  1389. */
  1390. function sodium_unpad($padded, $block_size)
  1391. {
  1392. return ParagonIE_Sodium_Compat::unpad($padded, $block_size, true);
  1393. }
  1394. }
  1395. if (!is_callable('sodium_randombytes_buf')) {
  1396. /**
  1397. * @see ParagonIE_Sodium_Compat::randombytes_buf()
  1398. * @param int $amount
  1399. * @return string
  1400. * @throws Exception
  1401. */
  1402. function sodium_randombytes_buf($amount)
  1403. {
  1404. return ParagonIE_Sodium_Compat::randombytes_buf($amount);
  1405. }
  1406. }
  1407. if (!is_callable('sodium_randombytes_uniform')) {
  1408. /**
  1409. * @see ParagonIE_Sodium_Compat::randombytes_uniform()
  1410. * @param int $upperLimit
  1411. * @return int
  1412. * @throws Exception
  1413. */
  1414. function sodium_randombytes_uniform($upperLimit)
  1415. {
  1416. return ParagonIE_Sodium_Compat::randombytes_uniform($upperLimit);
  1417. }
  1418. }
  1419. if (!is_callable('sodium_randombytes_random16')) {
  1420. /**
  1421. * @see ParagonIE_Sodium_Compat::randombytes_random16()
  1422. * @return int
  1423. * @throws Exception
  1424. */
  1425. function sodium_randombytes_random16()
  1426. {
  1427. return ParagonIE_Sodium_Compat::randombytes_random16();
  1428. }
  1429. }