Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Generator hasła .htpasswd
Forum PHP.pl > Forum > Przedszkole
Fezq
Witam, mam pytanie czy da radę z poziomu PHP zakodować ciąg znaków w kodowaniu SHA-1, który zostanie użyty w pliku .htpasswd?

Próbowałem czegoś takiego, ale wygenerowane hasło nie działa przy autoryzacji.
  1. <?php
  2. $pass = 'mojehaslo';
  3. $password = sha1($pass);
  4. echo $password;
  5. // wynik: da9d5f87604556617257361689debc2de11946f7
  6. ?>


Szukałem w internecie i znalazłem dobry generator SHA-1 - http://aspirine.org/htpasswd_en.html. Wygenerowane hasło ma zupełnie inną postać (Nick:{SHA}isyqnGP2Lh6cLhRoUJRB94wvsfg=) i co najważniejsze działa poprawnie.

Moje pytanie brzmi czy da się osiągnąć podobny efekt za pomocą skryptu PHP?
Ghost_78
a nie lepiej to zakodowac w MD5 ?
Fezq
Też nad tym myślałem, ale tutaj jest identyczna sytuacja.

Moje wygenerowane hasło (md5('123')); - 202cb962ac59075b964b07152d234b70 - [nie działa]
Hasło wygenerowane online (też 123) - $apr1$di//V33z$nnDhMDlOYNlKmSUO6OwYE0 - [działa]
Ghost_78
to co generuje ci php jest ok - u mnie wychodzi to samo
mozesz pokazac kod w ktorym to sprawdzasz ?
Mephistofeles
Problem w tym, że htpasswd wymaga jakiegoś specjalnego algorytmu, prawdopodobnie z solą czy czymś podobnym, musisz poszukać.
Fifi209
ale w .htpasswd z tego co pamiętam nie używa się SHA-1

Użytkownika tworzysz tak:

apache/bin/htpasswd.exe -c .htpasswd nazwa_uzytkownika

klikasz enter
wpisujesz hasło, powtarzasz i masz ;]

W między czasie znalazłem Ci to
Ghost_78
a ja Ci dorzuce to: http://koivi.com/php-http-auth/ masz tu gotowa funkcje do sprawdzania
Fezq
Hmm, poszukałem jeszcze trochę ale dalej nic. Na razie będę korzystał z wiersza poleceń, nie jest to może szczególnie wygodne, ale przynajmniej działa poprawnie. Dzięki za wszystkie odpowiedzi, jednak cały czas czekam, może ktoś do czegoś ciekawego dojdzie winksmiley.jpg

P.S No i jeszcze zostają mi generatory online oczywiście.
minolone
Sprawdz sobie jeszcze to
Cała klasa
  1. <?
  2.  
  3. /**
  4. * Class for handling htaccess of Apache with better htaccess editing
  5. * @author Sven Wagener <sven.wagener@intertribe.de>
  6. * @contributor Jan Matousek <seiffs@centrum.cz>
  7. * @copyright Intertribe - Internetservices Germany
  8. * @include Funktion:_include_
  9. */
  10.  
  11. class htmodel{
  12. var $fHtaccess = ""; // path and filename for htaccess file
  13. var $fHtgroup = ""; // path and filename for htgroup file
  14. var $fPasswd = ""; // path and filename for passwd file
  15. var $fDelimiter = "##AUTOMATED - DO NOT EDIT##";
  16.  
  17. var $authType="Basic"; // Default authentification type
  18. var $authName="Secured area"; // Default authentification name
  19.  
  20. /**
  21.   * constructor
  22.   */
  23. function __construct(){
  24. }
  25.  
  26. /**
  27.   * Initialising class htaccess
  28.   */
  29. function htaccess(){
  30. }
  31.  
  32. /**
  33.   * Sets the filename and path of .htaccess to work with
  34.   * @param string $filename the name of htaccess file
  35.   */
  36. function setFHtaccess($filename){
  37. $this->fHtaccess=$filename;
  38. }
  39.  
  40. /**
  41.   * Sets the filename and path of the htgroup file for the htaccess file
  42.   * @param string $filename the name of htgroup file
  43.   */
  44. function setFHtgroup($filename){
  45. $this->fHtgroup=$filename;
  46. }
  47.  
  48. /**
  49.   * Sets the filename and path of the password file for the htaccess file
  50.   * @param string $filename the name of htgroup file
  51.   */
  52. function setFHtpasswd($filename){
  53. $this->fPasswd=$filename;
  54. }
  55.  
  56. /**
  57.   * Adds a user to the password file
  58.   * @param string $username Username
  59.   * @param string $password Password for Username
  60.   * @param string $group Groupname for User (optional)
  61.   * @return boolean $created Returns true if user have been created otherwise false
  62.   */
  63. function addUser($username,$password,$group=""){
  64. // checking if user already exists
  65. $file=@fopen($this->fPasswd,"r");
  66. $isAlready=false;
  67. while($line=@fgets($file,200)){
  68. $lineArr=explode(":",$line);
  69. if($username==$lineArr[0]){
  70. $isAlready=true;
  71. }
  72. }
  73.  
  74. if($isAlready==false){
  75. $file=fopen($this->fPasswd,"a");
  76. $password=crypt($password);
  77. $newLine=$username.":".$password."\n";
  78.  
  79. fputs($file,$newLine);
  80. fclose($file);
  81. return true;
  82. }else{
  83. return false;
  84. }
  85. }
  86.  
  87. /**
  88.   * Adds a group to the htgroup file
  89.   * @param string $groupname Groupname
  90.   */
  91. function addGroup($groupname){
  92. $file=fopen($this->fHtgroup,"a");
  93. fclose($file);
  94. }
  95.  
  96. /**
  97.   * Deletes a user in the password file
  98.   * @param string $username Username to delete
  99.   * @return boolean $deleted Returns true if user have been deleted otherwise false
  100.   */
  101. function delUser($username){
  102. // Reading names from file
  103. $file=fopen($path.$this->fPasswd,"r");
  104. $i=0;
  105. while($line=fgets($file,200)){
  106. $lineArr=explode(":",$line);
  107. if($username!=$lineArr[0]){
  108. $newUserlist[$i][0]=$lineArr[0];
  109. $newUserlist[$i][1]=$lineArr[1];
  110. $i++;
  111. }else{
  112. $deleted=true;
  113. }
  114. }
  115. fclose($file);
  116.  
  117. // Writing names back to file (without the user to delete)
  118. $file=fopen($path.$this->fPasswd,"w");
  119. for($i=0;$i<count($newUserlist);$i++){
  120. fputs($file,$newUserlist[$i][0].":".$newUserlist[$i][0]."\n");
  121. }
  122. fclose($file);
  123.  
  124. if($deleted==true){
  125. return true;
  126. }else{
  127. return false;
  128. }
  129. }
  130.  
  131. /**
  132.   * Returns an array of all users in a password file
  133.   * @return array $users All usernames of a password file in an array
  134.   */
  135. function getUsers() {
  136. $file=fopen($this->fPasswd,"r");
  137. for($i=0;$line=fgets($file,200);$i++) {
  138. $lineArr=explode(":",$line);
  139. if($lineArr[0]!="") {
  140. $userlist[$i]['jmeno']=$lineArr[0];
  141. $userlist[$i]['id']=$lineArr[0];
  142. $userlist[$i]['heslo']=$lineArr[1];
  143. }
  144. }
  145. fclose($file);
  146.  
  147. // sort users
  148. if(!empty($userlist)){
  149. array_multisort($userlist);
  150. return $userlist;
  151. }
  152. }
  153.  
  154. /**
  155.   * Sets a password to the given username
  156.   * @param string $username The name of the User for changing password
  157.   * @param string $password New Password for the User
  158.   * @return boolean $isSet Returns true if password have been set
  159.   */
  160. function setPasswd($username,$new_password){
  161. // Reading names from file
  162. $newUserlist="";
  163.  
  164. $file=fopen($this->fPasswd,"r");
  165. $x=0;
  166. for($i=0;$line=fgets($file,200);$i++){
  167. $lineArr=explode(":",$line);
  168. if($username!=$lineArr[0] && $lineArr[0]!="" && $lineArr[1]!=""){
  169. $newUserlist[$i][0]=$lineArr[0];
  170. $newUserlist[$i][1]=$lineArr[1];
  171. $x++;
  172. }else if($lineArr[0]!="" && $lineArr[1]!=""){
  173. $newUserlist[$i][0]=$lineArr[0];
  174. $newUserlist[$i][1]=crypt($new_password)."\n";
  175. $isSet=true;
  176. $x++;
  177. }
  178. }
  179. fclose($file);
  180.  
  181. unlink($this->fPasswd);
  182.  
  183. /// Writing names back to file (with new password)
  184. $file=fopen($this->fPasswd,"w");
  185. for($i=0;$i<count($newUserlist);$i++){
  186. $content=$newUserlist[$i][0].":".$newUserlist[$i][1];
  187. fputs($file,$content);
  188. }
  189. fclose($file);
  190.  
  191. if($isSet==true){
  192. return true;
  193. }else{
  194. return false;
  195. }
  196. }
  197.  
  198. /**
  199.   * Sets the Authentification type for Login
  200.   * @param string $authtype Authentification type as string
  201.   */
  202. function setAuthType($authtype){
  203. $this->authType=$authtype;
  204. }
  205.  
  206. /**
  207.   * Sets the Authentification Name (Name of the login area)
  208.   * @param string $authname Name of the login area
  209.   */
  210. function setAuthName($authname){
  211. $this->authName=$authname;
  212. }
  213.  
  214. /**
  215.   * adds the automated lines only to the .htaccess file
  216.   * @see setFhtaccess()
  217.   */
  218. function enableLogin(){
  219. $file=fopen($this->fHtaccess,"r");
  220. $string = fread($file,filesize($this->fHtaccess));
  221. fclose($file);
  222.  
  223. $file=fopen($this->fHtaccess,"w");
  224. if($file){
  225. fputs($file,$string);
  226. fputs($file,"\n".$this->fDelimiter."\n");
  227. fputs($file,"AuthType ".$this->authType."\n");
  228. fputs($file,"AuthUserFile ".$this->fPasswd."\n");
  229. fputs($file,"AuthName \"".$this->authName."\"\n");
  230. fputs($file,"Require valid-user\n");
  231. fclose($file);
  232. }
  233. }
  234.  
  235. /**
  236.   * removes the automated lines only from the .htaccess file
  237.   */
  238. function disableLogin(){
  239. $file=fopen($this->fHtaccess,"r");
  240. $string = fread($file,filesize($this->fHtaccess));
  241. fclose($file);
  242.  
  243. $strarray = explode("\n",$string);
  244. foreach($strarray as $key => $line)
  245. if($line == $this->fDelimiter)
  246. $breaker = $key;
  247. array_splice($strarray,$breaker);
  248. $string = '';
  249. foreach($strarray as $key => $line)
  250. $string .= $line."\n";
  251. $file1=fopen($this->fHtaccess,"w");
  252. fputs($file1,$string);
  253. fclose($file1);
  254. }
  255.  
  256. /**
  257.   * checks if login is enabled in the htaccess file
  258.   * @return boolean $isSet Returns true if login is enabled
  259.   */
  260. function isLogin(){
  261. $file=fopen($this->fHtaccess,"r");
  262. $string = fread($file,filesize($this->fHtaccess));
  263. fclose($file);
  264.  
  265. $strarray = explode("\n",$string);
  266. foreach($strarray as $key => $line)
  267. if($line == $this->fDelimiter)
  268. $breaker = $key;
  269. if(isset($breaker))
  270. return true;
  271. return false;
  272. }
  273. }
  274. ?>
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.