How to hash passwords in MySQL?

前端 未结 6 789
死守一世寂寞
死守一世寂寞 2020-12-25 08:18

How I will make every password in my user table encrypted(md5()) except one particular row using a single query?

相关标签:
6条回答
  • 2020-12-25 08:53

    When hashing passwords, do not forget to salt them, so that same passwords do not yield same hashes:

    SET @salt := CONV(FLOOR(RAND() * 0x100000000), 10, 16)
    
    UPDATE  passwords
    SET     password = CONCAT(@salt, SHA(CONCAT(@salt, @typed_password)))
    
    SELECT  1
    FROM    passwords
    WHERE   SHA(CONCAT(SUBSTRING(password, 1, 8), @typed_password)) = SUBSTRING(password, 9, 40)
    
    0 讨论(0)
  • 2020-12-25 08:57

    Edited in response to edit in OP.

    UPDATE userTable
    SET password = MD5(password)
    WHERE NOT (<criteria to identify row to exclude>)
    
    0 讨论(0)
  • 2020-12-25 09:03

    Concerning you edit: do you have an ID or username that identifies this row?

    UPDATE mytable
    SET password = MD5(password)
    WHERE id <> 123
    
    0 讨论(0)
  • 2020-12-25 09:11
    UPDATE table SET Password = MD5(Password)
    

    I will say though that MD5 isn't a very good level of encryption and you should consider something stronger such as ENCRYPT with a custom salt. Read about it here

    EDIT: Looks like the original question changed. Here's the altered query to accomodate

    UPDATE table SET Password = MD5(Password) WHERE ID!=[specified index]
    

    EDIT: Worth noting

    MD5 Encryption Hacked

    0 讨论(0)
  • 2020-12-25 09:15

    I think it is a little bit more update

    SET PASSWORD FOR 'existinguser'@'localhost' = PASSWORD('newpass');
    

    or

    UPDATE user SET  password = PASSWORD('newpass');
    

    Hope this help

    0 讨论(0)
  • 2020-12-25 09:19

    Hash Functions in MySQL

    There are a lot more hash functions than MD5 to use for storing passwords in you MySQL database.
    You can find a list of them on MySQL :: 11.10.2. Encryption and Compression Functions.

    Save Password (hash):

    UPDATE users SET password = SHA('secret_password') WHERE ....;
    

    Check Password:

    SELECT COUNT(*) FROM users WHERE name = 'username' && password = SHA('typed_password');
    

    If the result is > 0, the user provided the correct password.

    0 讨论(0)
提交回复
热议问题