sjcl.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 (!(
  40. array_key_exists($k, $decoded) &&
  41. base64_decode($decoded[$k], $strict=true)
  42. )) return false;
  43. }
  44. // Make sure no additionnal keys were added.
  45. if (
  46. count(array_keys($decoded)) != count($accepted_keys)
  47. ) return false;
  48. // FIXME: Reject data if entropy is too low?
  49. // Make sure some fields have a reasonable size.
  50. if (strlen($decoded['iv']) > 24) return false;
  51. if (strlen($decoded['salt']) > 14) return false;
  52. return true;
  53. }
  54. }