No Description

sodium_compat.php 24KB

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