How to encrypt password

88 views
Skip to first unread message

isi_jca

unread,
Jul 22, 2021, 1:47:08 PM7/22/21
to web2py-users
Hello!
I have a historical table where I save  n passwords by users so, I want to validate in the next change password that the user don't repeat the password. I need to encrypt the password and make a query in historical table.
How can I do to encrypt the password in Web2py?

Best regards.

Dave S

unread,
Jul 22, 2021, 7:06:23 PM7/22/21
to web2py-users
There are relevant validators in the FORM system.  Take a look at 
and see if they help.  The CRYPT() validator should do what you need, I think.

This is aside from any tools in your front end.
 
/dps

isi_jca

unread,
Jul 28, 2021, 3:36:08 PM7/28/21
to web2py-users


Hello!

I was doing a test.

    rows_password = db(db.thist_password.usrid == auth.user_id).select(db.thist_password.password)
    for j in rows_password:
        #Get digest_alg salt, hash and original hash
        (digest_alg, salt, save_hash) = j.password.split('$')
        
        #Encrypt password
        hash_password = str(CRYPT(digest_alg=digest_alg,key=new_password,salt=salt)('test')[0])
        (digest_alg, salt, new_hash) = hash_password.split('$')

        if new_hash ==  save_hash:
            form.errors.new_password = ('You can't repeat password')
            break

But they are different

save_hash: 3e7d5266c03b978652b572be1e781c1ec0c985e6
new_hash: 33c3e0c2606b92fadf1f7aac07f77981f5f254bd salt: b5e76116485249e1 digest_alg: pbkdf2(1000,20,sha512)

save_hash: 0faac8814c1d26f1456a736634b1666320a19753
new_hash: 18a42ab5ab333ab7e21eb7e58591dd5c62885d60 salt: 936f3f68de018365 digest_alg: pbkdf2(1000,20,sha512)

save_hash: e90398ceeaf18950bc04229fe3b2e73dc16f9896
new_hash: d9ca6c9d67702112b2ca5d3d344bab0aaadde276 salt: 98a1d6f146b1815b digest_alg: pbkdf2(1000,20,sha512)

Anybody have any idea what is wrong?

Regards.

isi_jca

unread,
Jul 30, 2021, 4:34:44 PM7/30/21
to web2py-users
Hi everybody:

I was able to solve my problem.

def validar_password(form):
    entered_password = str(form.vars.new_password)
    entered_password = entered_password.strip()
    new_password = request.vars.new_password

   #Retrieve passwords
    rows_password = db(db.thist_password.usrid == auth.user_id).select(db.thist_password.password)

    for j in rows_password:
        #Get digest_alg salt and el hash
        (digest_alg, salt, save_hash) = j.password.split('$')
        stored_password = j.password
        
        #Encrypt using the same salt
        new_hash = CRYPT(salt=salt)(new_password)[0]   

        if stored_password ==  new_hash :
            form.errors.new_password = ('You can not repeat password')
            break

Regards.

Dave S

unread,
Jul 30, 2021, 7:36:46 PM7/30/21
to web2py-users
On Friday, July 30, 2021 at 1:34:44 PM UTC-7 isi_jca wrote:
Hi everybody:

I was able to solve my problem.


Good to hear!

/dps
 
Reply all
Reply to author
Forward
0 new messages