Guest User

Untitled

a guest
Feb 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. <?php
  2. /**
  3. * Get a key from a secret.
  4. * What we do is create two different hashes from the secret, combine them
  5. * and pick out the number of characters we need.
  6. * We used the raw binary output of the hash function for maximum
  7. * bit strength (we have 255 chars to choose from, instead of 16).
  8. *
  9. * @param string $secret
  10. * The secret to generate a key from.
  11. *
  12. * @param integer $ks
  13. * The key size.
  14. *
  15. * @return binary
  16. * Key created from secret.
  17. */
  18. private static function getKey($secret, $ks) {
  19. /* Split the secret into two parts. */
  20. $secretSplit = floor(strlen($secret));
  21. $secret1 = substr($secret, 0, $secretSplit);
  22. $secret2 = substr($secret, $secretSplit);
  23.  
  24. /* Hash the two parts seperatly and return the result in raw format. */
  25. $key1 = hash('sha256', $secret1, true);
  26. $key2 = hash('sha256', $secret2, true);
  27.  
  28. /* Return the part of the key we need. */
  29. return substr($key2.$key1, 0, $ks);
  30. }
Add Comment
Please, Sign In to add comment