sodium_compat.php 24 KB

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