sjcl.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * ZeroBin
  4. *
  5. * a zero-knowledge paste bin
  6. *
  7. * @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
  8. * @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
  9. * @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
  10. * @version 0.15
  11. */
  12. /**
  13. * sjcl
  14. *
  15. * Provides SJCL validation function.
  16. */
  17. class sjcl
  18. {
  19. /**
  20. * SJCL validator
  21. *
  22. * Checks if a json string is a proper SJCL encrypted message.
  23. *
  24. * @access public
  25. * @static
  26. * @param string $encoded JSON
  27. * @return bool
  28. */
  29. public static function isValid($encoded)
  30. {
  31. $accepted_keys = array('iv','salt','ct');
  32. // Make sure content is valid json
  33. $decoded = json_decode($encoded);
  34. if (is_null($decoded)) return false;
  35. $decoded = (array) $decoded;
  36. // Make sure required fields are present and contain base64 data.
  37. foreach($accepted_keys as $k)
  38. {
  39. if (!array_key_exists($k, $decoded)) return false;
  40. if (is_null(base64_decode($decoded[$k], $strict=true))) return false;
  41. }
  42. // Make sure no additionnal keys were added.
  43. if (
  44. count(
  45. array_intersect(
  46. array_keys($decoded),
  47. $accepted_keys
  48. )
  49. ) != 3
  50. ) return false;
  51. // FIXME: Reject data if entropy is too low?
  52. // Make sure some fields have a reasonable size.
  53. if (strlen($decoded['iv']) > 24) return false;
  54. if (strlen($decoded['salt']) > 14) return false;
  55. return true;
  56. }
  57. }