Login

MySQL django password function

Author:
mcosta
Posted:
June 1, 2009
Language:
SQL
Version:
Not specified
Tags:
function password mysq sha
Score:
2 (after 2 ratings)

This functions encodes a password in the same format as django. You can set the auth_user.password column with the result of this function:

update `auth_user`.`password`
set `password` = django_password('secret')
where id = 1234;
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
DELIMITER //
CREATE FUNCTION django_password(
  pass VARCHAR(32)
) RETURNS VARCHAR(128) 
DETERMINISTIC
BEGIN
	DECLARE salt char(5);
	DECLARE hash VARCHAR(40);
	SET salt = MID(RAND(), 3, 5);
	SET hash = SHA(CONCAT(salt, pass));
	RETURN CONCAT('sha1$', salt, '$', hash);
END//

More like this

  1. Bit.ly url shortener by BluePeppers 4 years, 8 months ago
  2. Instructions and code to use drupal 7 passwords by grillermo 2 years, 1 month ago
  3. create_model_instances management command by Lacrymology 2 years, 6 months ago
  4. reset django password by hugogee 3 years, 11 months ago
  5. use oldforms validators in newforms forms by garywilson 8 years, 3 months ago

Comments

mcosta (on June 1, 2009):

Note the deterministic behaviour from the manual:

A routine that contains the NOW() function (or its synonyms) or RAND() is non-deterministic, but it might still be replication-safe. For NOW(), the binary log includes the timestamp and replicates correctly. RAND() also replicates correctly as long as it is called only a single time during the execution of a routine. (You can consider the routine execution timestamp and random number seed as implicit inputs that are identical on the master and slave.)

#

gamesbook (on October 27, 2010):

How would you make use of this inside of Django (and Django Admin) i.e. where would you add the SQL?

#

Please login first before commenting.