sodium_compat.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. <?php
  2. namespace Sodium;
  3. use ParagonIE_Sodium_Compat;
  4. /**
  5. * This file will monkey patch the pure-PHP implementation in place of the
  6. * PECL functions, but only if they do not already exist.
  7. *
  8. * Thus, the functions just proxy to the appropriate ParagonIE_Sodium_Compat
  9. * method.
  10. */
  11. if (!is_callable('\\Sodium\\bin2hex')) {
  12. /**
  13. * @see ParagonIE_Sodium_Compat::bin2hex()
  14. * @param string $string
  15. * @return string
  16. * @throws \SodiumException
  17. * @throws \TypeError
  18. */
  19. function bin2hex($string)
  20. {
  21. return ParagonIE_Sodium_Compat::bin2hex($string);
  22. }
  23. }
  24. if (!is_callable('\\Sodium\\compare')) {
  25. /**
  26. * @see ParagonIE_Sodium_Compat::compare()
  27. * @param string $a
  28. * @param string $b
  29. * @return int
  30. * @throws \SodiumException
  31. * @throws \TypeError
  32. */
  33. function compare($a, $b)
  34. {
  35. return ParagonIE_Sodium_Compat::compare($a, $b);
  36. }
  37. }
  38. if (!is_callable('\\Sodium\\crypto_aead_aes256gcm_decrypt')) {
  39. /**
  40. * @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt()
  41. * @param string $message
  42. * @param string $assocData
  43. * @param string $nonce
  44. * @param string $key
  45. * @return string|bool
  46. * @throws \SodiumException
  47. * @throws \TypeError
  48. */
  49. function crypto_aead_aes256gcm_decrypt($message, $assocData, $nonce, $key)
  50. {
  51. try {
  52. return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_decrypt($message, $assocData, $nonce, $key);
  53. } catch (Error $ex) {
  54. return false;
  55. } catch (Exception $ex) {
  56. return false;
  57. }
  58. }
  59. }
  60. if (!is_callable('\\Sodium\\crypto_aead_aes256gcm_encrypt')) {
  61. /**
  62. * @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_encrypt()
  63. * @param string $message
  64. * @param string $assocData
  65. * @param string $nonce
  66. * @param string $key
  67. * @return string
  68. * @throws \SodiumException
  69. * @throws \TypeError
  70. */
  71. function crypto_aead_aes256gcm_encrypt($message, $assocData, $nonce, $key)
  72. {
  73. return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_encrypt($message, $assocData, $nonce, $key);
  74. }
  75. }
  76. if (!is_callable('\\Sodium\\crypto_aead_aes256gcm_is_available')) {
  77. /**
  78. * @see ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_is_available()
  79. * @return bool
  80. */
  81. function crypto_aead_aes256gcm_is_available()
  82. {
  83. return ParagonIE_Sodium_Compat::crypto_aead_aes256gcm_is_available();
  84. }
  85. }
  86. if (!is_callable('\\Sodium\\crypto_aead_chacha20poly1305_decrypt')) {
  87. /**
  88. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt()
  89. * @param string $message
  90. * @param string $assocData
  91. * @param string $nonce
  92. * @param string $key
  93. * @return string|bool
  94. * @throws \SodiumException
  95. * @throws \TypeError
  96. */
  97. function crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key)
  98. {
  99. try {
  100. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_decrypt($message, $assocData, $nonce, $key);
  101. } catch (Error $ex) {
  102. return false;
  103. } catch (Exception $ex) {
  104. return false;
  105. }
  106. }
  107. }
  108. if (!is_callable('\\Sodium\\crypto_aead_chacha20poly1305_encrypt')) {
  109. /**
  110. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_encrypt()
  111. * @param string $message
  112. * @param string $assocData
  113. * @param string $nonce
  114. * @param string $key
  115. * @return string
  116. * @throws \SodiumException
  117. * @throws \TypeError
  118. */
  119. function crypto_aead_chacha20poly1305_encrypt($message, $assocData, $nonce, $key)
  120. {
  121. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_encrypt($message, $assocData, $nonce, $key);
  122. }
  123. }
  124. if (!is_callable('\\Sodium\\crypto_aead_chacha20poly1305_ietf_decrypt')) {
  125. /**
  126. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt()
  127. * @param string $message
  128. * @param string $assocData
  129. * @param string $nonce
  130. * @param string $key
  131. * @return string|bool
  132. * @throws \SodiumException
  133. * @throws \TypeError
  134. */
  135. function crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key)
  136. {
  137. try {
  138. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_decrypt($message, $assocData, $nonce, $key);
  139. } catch (Error $ex) {
  140. return false;
  141. } catch (Exception $ex) {
  142. return false;
  143. }
  144. }
  145. }
  146. if (!is_callable('\\Sodium\\crypto_aead_chacha20poly1305_ietf_encrypt')) {
  147. /**
  148. * @see ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt()
  149. * @param string $message
  150. * @param string $assocData
  151. * @param string $nonce
  152. * @param string $key
  153. * @return string
  154. * @throws \SodiumException
  155. * @throws \TypeError
  156. */
  157. function crypto_aead_chacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key)
  158. {
  159. return ParagonIE_Sodium_Compat::crypto_aead_chacha20poly1305_ietf_encrypt($message, $assocData, $nonce, $key);
  160. }
  161. }
  162. if (!is_callable('\\Sodium\\crypto_auth')) {
  163. /**
  164. * @see ParagonIE_Sodium_Compat::crypto_auth()
  165. * @param string $message
  166. * @param string $key
  167. * @return string
  168. * @throws \SodiumException
  169. * @throws \TypeError
  170. */
  171. function crypto_auth($message, $key)
  172. {
  173. return ParagonIE_Sodium_Compat::crypto_auth($message, $key);
  174. }
  175. }
  176. if (!is_callable('\\Sodium\\crypto_auth_verify')) {
  177. /**
  178. * @see ParagonIE_Sodium_Compat::crypto_auth_verify()
  179. * @param string $mac
  180. * @param string $message
  181. * @param string $key
  182. * @return bool
  183. * @throws \SodiumException
  184. * @throws \TypeError
  185. */
  186. function crypto_auth_verify($mac, $message, $key)
  187. {
  188. return ParagonIE_Sodium_Compat::crypto_auth_verify($mac, $message, $key);
  189. }
  190. }
  191. if (!is_callable('\\Sodium\\crypto_box')) {
  192. /**
  193. * @see ParagonIE_Sodium_Compat::crypto_box()
  194. * @param string $message
  195. * @param string $nonce
  196. * @param string $kp
  197. * @return string
  198. * @throws \SodiumException
  199. * @throws \TypeError
  200. */
  201. function crypto_box($message, $nonce, $kp)
  202. {
  203. return ParagonIE_Sodium_Compat::crypto_box($message, $nonce, $kp);
  204. }
  205. }
  206. if (!is_callable('\\Sodium\\crypto_box_keypair')) {
  207. /**
  208. * @see ParagonIE_Sodium_Compat::crypto_box_keypair()
  209. * @return string
  210. * @throws \SodiumException
  211. * @throws \TypeError
  212. */
  213. function crypto_box_keypair()
  214. {
  215. return ParagonIE_Sodium_Compat::crypto_box_keypair();
  216. }
  217. }
  218. if (!is_callable('\\Sodium\\crypto_box_keypair_from_secretkey_and_publickey')) {
  219. /**
  220. * @see ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey()
  221. * @param string $sk
  222. * @param string $pk
  223. * @return string
  224. * @throws \SodiumException
  225. * @throws \TypeError
  226. */
  227. function crypto_box_keypair_from_secretkey_and_publickey($sk, $pk)
  228. {
  229. return ParagonIE_Sodium_Compat::crypto_box_keypair_from_secretkey_and_publickey($sk, $pk);
  230. }
  231. }
  232. if (!is_callable('\\Sodium\\crypto_box_open')) {
  233. /**
  234. * @see ParagonIE_Sodium_Compat::crypto_box_open()
  235. * @param string $message
  236. * @param string $nonce
  237. * @param string $kp
  238. * @return string|bool
  239. * @throws \SodiumException
  240. * @throws \TypeError
  241. */
  242. function crypto_box_open($message, $nonce, $kp)
  243. {
  244. try {
  245. return ParagonIE_Sodium_Compat::crypto_box_open($message, $nonce, $kp);
  246. } catch (Error $ex) {
  247. return false;
  248. } catch (Exception $ex) {
  249. return false;
  250. }
  251. }
  252. }
  253. if (!is_callable('\\Sodium\\crypto_box_publickey')) {
  254. /**
  255. * @see ParagonIE_Sodium_Compat::crypto_box_publickey()
  256. * @param string $keypair
  257. * @return string
  258. * @throws \SodiumException
  259. * @throws \TypeError
  260. */
  261. function crypto_box_publickey($keypair)
  262. {
  263. return ParagonIE_Sodium_Compat::crypto_box_publickey($keypair);
  264. }
  265. }
  266. if (!is_callable('\\Sodium\\crypto_box_publickey_from_secretkey')) {
  267. /**
  268. * @see ParagonIE_Sodium_Compat::crypto_box_publickey_from_secretkey()
  269. * @param string $sk
  270. * @return string
  271. * @throws \SodiumException
  272. * @throws \TypeError
  273. */
  274. function crypto_box_publickey_from_secretkey($sk)
  275. {
  276. return ParagonIE_Sodium_Compat::crypto_box_publickey_from_secretkey($sk);
  277. }
  278. }
  279. if (!is_callable('\\Sodium\\crypto_box_seal')) {
  280. /**
  281. * @see ParagonIE_Sodium_Compat::crypto_box_seal_open()
  282. * @param string $message
  283. * @param string $publicKey
  284. * @return string
  285. * @throws \SodiumException
  286. * @throws \TypeError
  287. */
  288. function crypto_box_seal($message, $publicKey)
  289. {
  290. return ParagonIE_Sodium_Compat::crypto_box_seal($message, $publicKey);
  291. }
  292. }
  293. if (!is_callable('\\Sodium\\crypto_box_seal_open')) {
  294. /**
  295. * @see ParagonIE_Sodium_Compat::crypto_box_seal_open()
  296. * @param string $message
  297. * @param string $kp
  298. * @return string|bool
  299. * @throws \TypeError
  300. */
  301. function crypto_box_seal_open($message, $kp)
  302. {
  303. try {
  304. return ParagonIE_Sodium_Compat::crypto_box_seal_open($message, $kp);
  305. } catch (\Error $ex) {
  306. return false;
  307. } catch (\Exception $ex) {
  308. return false;
  309. }
  310. }
  311. }
  312. if (!is_callable('\\Sodium\\crypto_box_secretkey')) {
  313. /**
  314. * @see ParagonIE_Sodium_Compat::crypto_box_secretkey()
  315. * @param string $keypair
  316. * @return string
  317. * @throws \SodiumException
  318. * @throws \TypeError
  319. */
  320. function crypto_box_secretkey($keypair)
  321. {
  322. return ParagonIE_Sodium_Compat::crypto_box_secretkey($keypair);
  323. }
  324. }
  325. if (!is_callable('\\Sodium\\crypto_generichash')) {
  326. /**
  327. * @see ParagonIE_Sodium_Compat::crypto_generichash()
  328. * @param string $message
  329. * @param string|null $key
  330. * @param int $outLen
  331. * @return string
  332. * @throws \SodiumException
  333. * @throws \TypeError
  334. */
  335. function crypto_generichash($message, $key = null, $outLen = 32)
  336. {
  337. return ParagonIE_Sodium_Compat::crypto_generichash($message, $key, $outLen);
  338. }
  339. }
  340. if (!is_callable('\\Sodium\\crypto_generichash_final')) {
  341. /**
  342. * @see ParagonIE_Sodium_Compat::crypto_generichash_final()
  343. * @param string|null $ctx
  344. * @param int $outputLength
  345. * @return string
  346. * @throws \SodiumException
  347. * @throws \TypeError
  348. */
  349. function crypto_generichash_final(&$ctx, $outputLength = 32)
  350. {
  351. return ParagonIE_Sodium_Compat::crypto_generichash_final($ctx, $outputLength);
  352. }
  353. }
  354. if (!is_callable('\\Sodium\\crypto_generichash_init')) {
  355. /**
  356. * @see ParagonIE_Sodium_Compat::crypto_generichash_init()
  357. * @param string|null $key
  358. * @param int $outLen
  359. * @return string
  360. * @throws \SodiumException
  361. * @throws \TypeError
  362. */
  363. function crypto_generichash_init($key = null, $outLen = 32)
  364. {
  365. return ParagonIE_Sodium_Compat::crypto_generichash_init($key, $outLen);
  366. }
  367. }
  368. if (!is_callable('\\Sodium\\crypto_generichash_update')) {
  369. /**
  370. * @see ParagonIE_Sodium_Compat::crypto_generichash_update()
  371. * @param string|null $ctx
  372. * @param string $message
  373. * @return void
  374. * @throws \SodiumException
  375. * @throws \TypeError
  376. */
  377. function crypto_generichash_update(&$ctx, $message = '')
  378. {
  379. ParagonIE_Sodium_Compat::crypto_generichash_update($ctx, $message);
  380. }
  381. }
  382. if (!is_callable('\\Sodium\\crypto_kx')) {
  383. /**
  384. * @see ParagonIE_Sodium_Compat::crypto_kx()
  385. * @param string $my_secret
  386. * @param string $their_public
  387. * @param string $client_public
  388. * @param string $server_public
  389. * @return string
  390. * @throws \SodiumException
  391. * @throws \TypeError
  392. */
  393. function crypto_kx($my_secret, $their_public, $client_public, $server_public)
  394. {
  395. return ParagonIE_Sodium_Compat::crypto_kx(
  396. $my_secret,
  397. $their_public,
  398. $client_public,
  399. $server_public
  400. );
  401. }
  402. }
  403. if (!is_callable('\\Sodium\\crypto_pwhash')) {
  404. /**
  405. * @see ParagonIE_Sodium_Compat::crypto_pwhash()
  406. * @param int $outlen
  407. * @param string $passwd
  408. * @param string $salt
  409. * @param int $opslimit
  410. * @param int $memlimit
  411. * @return string
  412. * @throws \SodiumException
  413. * @throws \TypeError
  414. */
  415. function crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit)
  416. {
  417. return ParagonIE_Sodium_Compat::crypto_pwhash($outlen, $passwd, $salt, $opslimit, $memlimit);
  418. }
  419. }
  420. if (!is_callable('\\Sodium\\crypto_pwhash_str')) {
  421. /**
  422. * @see ParagonIE_Sodium_Compat::crypto_pwhash_str()
  423. * @param string $passwd
  424. * @param int $opslimit
  425. * @param int $memlimit
  426. * @return string
  427. * @throws \SodiumException
  428. * @throws \TypeError
  429. */
  430. function crypto_pwhash_str($passwd, $opslimit, $memlimit)
  431. {
  432. return ParagonIE_Sodium_Compat::crypto_pwhash_str($passwd, $opslimit, $memlimit);
  433. }
  434. }
  435. if (!is_callable('\\Sodium\\crypto_pwhash_str_verify')) {
  436. /**
  437. * @see ParagonIE_Sodium_Compat::crypto_pwhash_str_verify()
  438. * @param string $passwd
  439. * @param string $hash
  440. * @return bool
  441. * @throws \SodiumException
  442. * @throws \TypeError
  443. */
  444. function crypto_pwhash_str_verify($passwd, $hash)
  445. {
  446. return ParagonIE_Sodium_Compat::crypto_pwhash_str_verify($passwd, $hash);
  447. }
  448. }
  449. if (!is_callable('\\Sodium\\crypto_pwhash_scryptsalsa208sha256')) {
  450. /**
  451. * @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256()
  452. * @param int $outlen
  453. * @param string $passwd
  454. * @param string $salt
  455. * @param int $opslimit
  456. * @param int $memlimit
  457. * @return string
  458. * @throws \SodiumException
  459. * @throws \TypeError
  460. */
  461. function crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit)
  462. {
  463. return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256($outlen, $passwd, $salt, $opslimit, $memlimit);
  464. }
  465. }
  466. if (!is_callable('\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str')) {
  467. /**
  468. * @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str()
  469. * @param string $passwd
  470. * @param int $opslimit
  471. * @param int $memlimit
  472. * @return string
  473. * @throws \SodiumException
  474. * @throws \TypeError
  475. */
  476. function crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit)
  477. {
  478. return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str($passwd, $opslimit, $memlimit);
  479. }
  480. }
  481. if (!is_callable('\\Sodium\\crypto_pwhash_scryptsalsa208sha256_str_verify')) {
  482. /**
  483. * @see ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str_verify()
  484. * @param string $passwd
  485. * @param string $hash
  486. * @return bool
  487. * @throws \SodiumException
  488. * @throws \TypeError
  489. */
  490. function crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash)
  491. {
  492. return ParagonIE_Sodium_Compat::crypto_pwhash_scryptsalsa208sha256_str_verify($passwd, $hash);
  493. }
  494. }
  495. if (!is_callable('\\Sodium\\crypto_scalarmult')) {
  496. /**
  497. * @see ParagonIE_Sodium_Compat::crypto_scalarmult()
  498. * @param string $n
  499. * @param string $p
  500. * @return string
  501. * @throws \SodiumException
  502. * @throws \TypeError
  503. */
  504. function crypto_scalarmult($n, $p)
  505. {
  506. return ParagonIE_Sodium_Compat::crypto_scalarmult($n, $p);
  507. }
  508. }
  509. if (!is_callable('\\Sodium\\crypto_scalarmult_base')) {
  510. /**
  511. * @see ParagonIE_Sodium_Compat::crypto_scalarmult_base()
  512. * @param string $n
  513. * @return string
  514. * @throws \SodiumException
  515. * @throws \TypeError
  516. */
  517. function crypto_scalarmult_base($n)
  518. {
  519. return ParagonIE_Sodium_Compat::crypto_scalarmult_base($n);
  520. }
  521. }
  522. if (!is_callable('\\Sodium\\crypto_secretbox')) {
  523. /**
  524. * @see ParagonIE_Sodium_Compat::crypto_secretbox()
  525. * @param string $message
  526. * @param string $nonce
  527. * @param string $key
  528. * @return string
  529. * @throws \SodiumException
  530. * @throws \TypeError
  531. */
  532. function crypto_secretbox($message, $nonce, $key)
  533. {
  534. return ParagonIE_Sodium_Compat::crypto_secretbox($message, $nonce, $key);
  535. }
  536. }
  537. if (!is_callable('\\Sodium\\crypto_secretbox_open')) {
  538. /**
  539. * @see ParagonIE_Sodium_Compat::crypto_secretbox_open()
  540. * @param string $message
  541. * @param string $nonce
  542. * @param string $key
  543. * @return string|bool
  544. * @throws \SodiumException
  545. * @throws \TypeError
  546. */
  547. function crypto_secretbox_open($message, $nonce, $key)
  548. {
  549. try {
  550. return ParagonIE_Sodium_Compat::crypto_secretbox_open($message, $nonce, $key);
  551. } catch (Error $ex) {
  552. return false;
  553. } catch (Exception $ex) {
  554. return false;
  555. }
  556. }
  557. }
  558. if (!is_callable('\\Sodium\\crypto_shorthash')) {
  559. /**
  560. * @see ParagonIE_Sodium_Compat::crypto_shorthash()
  561. * @param string $message
  562. * @param string $key
  563. * @return string
  564. * @throws \SodiumException
  565. * @throws \TypeError
  566. */
  567. function crypto_shorthash($message, $key = '')
  568. {
  569. return ParagonIE_Sodium_Compat::crypto_shorthash($message, $key);
  570. }
  571. }
  572. if (!is_callable('\\Sodium\\crypto_sign')) {
  573. /**
  574. * @see ParagonIE_Sodium_Compat::crypto_sign()
  575. * @param string $message
  576. * @param string $sk
  577. * @return string
  578. * @throws \SodiumException
  579. * @throws \TypeError
  580. */
  581. function crypto_sign($message, $sk)
  582. {
  583. return ParagonIE_Sodium_Compat::crypto_sign($message, $sk);
  584. }
  585. }
  586. if (!is_callable('\\Sodium\\crypto_sign_detached')) {
  587. /**
  588. * @see ParagonIE_Sodium_Compat::crypto_sign_detached()
  589. * @param string $message
  590. * @param string $sk
  591. * @return string
  592. * @throws \SodiumException
  593. * @throws \TypeError
  594. */
  595. function crypto_sign_detached($message, $sk)
  596. {
  597. return ParagonIE_Sodium_Compat::crypto_sign_detached($message, $sk);
  598. }
  599. }
  600. if (!is_callable('\\Sodium\\crypto_sign_keypair')) {
  601. /**
  602. * @see ParagonIE_Sodium_Compat::crypto_sign_keypair()
  603. * @return string
  604. * @throws \SodiumException
  605. * @throws \TypeError
  606. */
  607. function crypto_sign_keypair()
  608. {
  609. return ParagonIE_Sodium_Compat::crypto_sign_keypair();
  610. }
  611. }
  612. if (!is_callable('\\Sodium\\crypto_sign_open')) {
  613. /**
  614. * @see ParagonIE_Sodium_Compat::crypto_sign_open()
  615. * @param string $signedMessage
  616. * @param string $pk
  617. * @return string|bool
  618. */
  619. function crypto_sign_open($signedMessage, $pk)
  620. {
  621. try {
  622. return ParagonIE_Sodium_Compat::crypto_sign_open($signedMessage, $pk);
  623. } catch (\Error $ex) {
  624. return false;
  625. } catch (\Exception $ex) {
  626. return false;
  627. }
  628. }
  629. }
  630. if (!is_callable('\\Sodium\\crypto_sign_publickey')) {
  631. /**
  632. * @see ParagonIE_Sodium_Compat::crypto_sign_publickey()
  633. * @param string $keypair
  634. * @return string
  635. * @throws \SodiumException
  636. * @throws \TypeError
  637. */
  638. function crypto_sign_publickey($keypair)
  639. {
  640. return ParagonIE_Sodium_Compat::crypto_sign_publickey($keypair);
  641. }
  642. }
  643. if (!is_callable('\\Sodium\\crypto_sign_publickey_from_secretkey')) {
  644. /**
  645. * @see ParagonIE_Sodium_Compat::crypto_sign_publickey_from_secretkey()
  646. * @param string $sk
  647. * @return string
  648. * @throws \SodiumException
  649. * @throws \TypeError
  650. */
  651. function crypto_sign_publickey_from_secretkey($sk)
  652. {
  653. return ParagonIE_Sodium_Compat::crypto_sign_publickey_from_secretkey($sk);
  654. }
  655. }
  656. if (!is_callable('\\Sodium\\crypto_sign_secretkey')) {
  657. /**
  658. * @see ParagonIE_Sodium_Compat::crypto_sign_secretkey()
  659. * @param string $keypair
  660. * @return string
  661. * @throws \SodiumException
  662. * @throws \TypeError
  663. */
  664. function crypto_sign_secretkey($keypair)
  665. {
  666. return ParagonIE_Sodium_Compat::crypto_sign_secretkey($keypair);
  667. }
  668. }
  669. if (!is_callable('\\Sodium\\crypto_sign_seed_keypair')) {
  670. /**
  671. * @see ParagonIE_Sodium_Compat::crypto_sign_seed_keypair()
  672. * @param string $seed
  673. * @return string
  674. * @throws \SodiumException
  675. * @throws \TypeError
  676. */
  677. function crypto_sign_seed_keypair($seed)
  678. {
  679. return ParagonIE_Sodium_Compat::crypto_sign_seed_keypair($seed);
  680. }
  681. }
  682. if (!is_callable('\\Sodium\\crypto_sign_verify_detached')) {
  683. /**
  684. * @see ParagonIE_Sodium_Compat::crypto_sign_verify_detached()
  685. * @param string $signature
  686. * @param string $message
  687. * @param string $pk
  688. * @return bool
  689. * @throws \SodiumException
  690. * @throws \TypeError
  691. */
  692. function crypto_sign_verify_detached($signature, $message, $pk)
  693. {
  694. return ParagonIE_Sodium_Compat::crypto_sign_verify_detached($signature, $message, $pk);
  695. }
  696. }
  697. if (!is_callable('\\Sodium\\crypto_sign_ed25519_pk_to_curve25519')) {
  698. /**
  699. * @see ParagonIE_Sodium_Compat::crypto_sign_ed25519_pk_to_curve25519()
  700. * @param string $pk
  701. * @return string
  702. * @throws \SodiumException
  703. * @throws \TypeError
  704. */
  705. function crypto_sign_ed25519_pk_to_curve25519($pk)
  706. {
  707. return ParagonIE_Sodium_Compat::crypto_sign_ed25519_pk_to_curve25519($pk);
  708. }
  709. }
  710. if (!is_callable('\\Sodium\\crypto_sign_ed25519_sk_to_curve25519')) {
  711. /**
  712. * @see ParagonIE_Sodium_Compat::crypto_sign_ed25519_sk_to_curve25519()
  713. * @param string $sk
  714. * @return string
  715. * @throws \SodiumException
  716. * @throws \TypeError
  717. */
  718. function crypto_sign_ed25519_sk_to_curve25519($sk)
  719. {
  720. return ParagonIE_Sodium_Compat::crypto_sign_ed25519_sk_to_curve25519($sk);
  721. }
  722. }
  723. if (!is_callable('\\Sodium\\crypto_stream')) {
  724. /**
  725. * @see ParagonIE_Sodium_Compat::crypto_stream()
  726. * @param int $len
  727. * @param string $nonce
  728. * @param string $key
  729. * @return string
  730. * @throws \SodiumException
  731. * @throws \TypeError
  732. */
  733. function crypto_stream($len, $nonce, $key)
  734. {
  735. return ParagonIE_Sodium_Compat::crypto_stream($len, $nonce, $key);
  736. }
  737. }
  738. if (!is_callable('\\Sodium\\crypto_stream_xor')) {
  739. /**
  740. * @see ParagonIE_Sodium_Compat::crypto_stream_xor()
  741. * @param string $message
  742. * @param string $nonce
  743. * @param string $key
  744. * @return string
  745. * @throws \SodiumException
  746. * @throws \TypeError
  747. */
  748. function crypto_stream_xor($message, $nonce, $key)
  749. {
  750. return ParagonIE_Sodium_Compat::crypto_stream_xor($message, $nonce, $key);
  751. }
  752. }
  753. if (!is_callable('\\Sodium\\hex2bin')) {
  754. /**
  755. * @see ParagonIE_Sodium_Compat::hex2bin()
  756. * @param string $string
  757. * @return string
  758. * @throws \SodiumException
  759. * @throws \TypeError
  760. */
  761. function hex2bin($string)
  762. {
  763. return ParagonIE_Sodium_Compat::hex2bin($string);
  764. }
  765. }
  766. if (!is_callable('\\Sodium\\memcmp')) {
  767. /**
  768. * @see ParagonIE_Sodium_Compat::memcmp()
  769. * @param string $a
  770. * @param string $b
  771. * @return int
  772. * @throws \SodiumException
  773. * @throws \TypeError
  774. */
  775. function memcmp($a, $b)
  776. {
  777. return ParagonIE_Sodium_Compat::memcmp($a, $b);
  778. }
  779. }
  780. if (!is_callable('\\Sodium\\memzero')) {
  781. /**
  782. * @see ParagonIE_Sodium_Compat::memzero()
  783. * @param string $str
  784. * @return void
  785. * @throws \SodiumException
  786. * @throws \TypeError
  787. */
  788. function memzero(&$str)
  789. {
  790. ParagonIE_Sodium_Compat::memzero($str);
  791. }
  792. }
  793. if (!is_callable('\\Sodium\\randombytes_buf')) {
  794. /**
  795. * @see ParagonIE_Sodium_Compat::randombytes_buf()
  796. * @param int $amount
  797. * @return string
  798. * @throws \TypeError
  799. */
  800. function randombytes_buf($amount)
  801. {
  802. return ParagonIE_Sodium_Compat::randombytes_buf($amount);
  803. }
  804. }
  805. if (!is_callable('\\Sodium\\randombytes_uniform')) {
  806. /**
  807. * @see ParagonIE_Sodium_Compat::randombytes_uniform()
  808. * @param int $upperLimit
  809. * @return int
  810. * @throws \Exception
  811. * @throws \Error
  812. */
  813. function randombytes_uniform($upperLimit)
  814. {
  815. return ParagonIE_Sodium_Compat::randombytes_uniform($upperLimit);
  816. }
  817. }
  818. if (!is_callable('\\Sodium\\randombytes_random16')) {
  819. /**
  820. * @see ParagonIE_Sodium_Compat::randombytes_random16()
  821. * @return int
  822. */
  823. function randombytes_random16()
  824. {
  825. return ParagonIE_Sodium_Compat::randombytes_random16();
  826. }
  827. }
  828. if (!defined('\\Sodium\\CRYPTO_AUTH_BYTES')) {
  829. require_once dirname(__FILE__) . '/constants.php';
  830. }