code_junkie

Programmaticly building htpasswd

Nov 10th, 2011
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. foo:$apr1$y1cXxW5l$3vapv2yyCXaYz8zGoXj241
  2.  
  3. foo:{SHA}BW6v589SIg3i3zaEW47RcMZ+I+M=
  4.  
  5. <?php
  6.  
  7. $login = 'foo';
  8. $pass = 'pass';
  9. $hash = base64_encode(sha1($pass, true));
  10.  
  11. $contents = $login . ':{SHA1}' . $hash;
  12.  
  13. file_set_contents('.htpasswd', $contents);
  14.  
  15. ?>
  16.  
  17. <FORM METHOD="POST" ACTION="<? echo $_SERVER['PHP_SELF']; ?>" onSubmit='return ValidateForm()'>
  18. Username<br /><INPUT TYPE="TEXT" NAME="user[]"><br /><br />
  19. Password<br /><INPUT TYPE="PASSWORD" NAME="password1[]"><br />
  20. <INPUT TYPE="PASSWORD" NAME="password2[]"><br /><br />
  21. <INPUT type=submit name="submit" VALUE="Create .htpasswd entry" onclick="document.all.submit.style.visibility='hidden'">
  22. </FORM>
  23.  
  24. if ( isset($_POST['user']) && isset($_POST['password1'])) {
  25. if( $_POST['password1'] == $_POST['password2'] ) {
  26. $user = $_POST['user'];
  27. $password1 = $_POST['password1'];
  28. $htpasswd_text = "";
  29. for ($i = 0; $i < count ($user); $i++) {
  30. $htpasswd_text .= "$user[$i]:".crypt($password1[$i],CRYPT_STD_DES)."";
  31. }
  32. echo "<br />Copy this line to your .htpasswd file:";
  33. echo "<pre style="border-bottom-width:1px;border-bottom-style:solid;">";
  34. echo nl2br($htpasswd_text);
  35. echo "</pre><br />";
  36. } else {
  37. echo "<pre style="border-bottom-width:1px;border-bottom-style:solid;">Passwords do not match !</pre><br />";
  38. }
  39. }
  40.  
  41. <?php
  42.  
  43. // Set the password & username
  44. $username = 'user';
  45. $password = 'mypassword';
  46.  
  47. // Get the hash, letting the salt be automatically generated
  48. $hash = crypt($password);
  49.  
  50. // write to a file
  51. file_set_contents('.htpasswd', $username ':' . $contents);
  52.  
  53. ?>
Add Comment
Please, Sign In to add comment