Coder Social home page Coder Social logo

Comments (6)

delivrance avatar delivrance commented on June 19, 2024

@1pyxa1 What are you trying to do exactly? Remove the password? Change it? ...?

from pyrogram.

1pyxa1 avatar 1pyxa1 commented on June 19, 2024

@delivrance Trying to set new password first time.

from pyrogram.

1pyxa1 avatar 1pyxa1 commented on June 19, 2024

I found the solution. Tested it for setting new password first time and for changing it. Works great.
Here is my version:

def update_password_settings(client,                                                
                             new_password,                                          
                             current_password=None,                                 
                             email=None,                                            
                             hint='hint'):                                          
    r = client.send(functions.account.GetPassword())                                
    salt_random = os.urandom(8)                                                     
    salt = r.new_salt + salt_random                                                 
    if current_password:                                                            
        current_password = r.current_salt + current_password.encode() + \           
            r.current_salt                                                          
        current_password_hash = sha256(current_password).digest()                   
    else:                                                                           
        current_password_hash = salt                                                
    new_password = salt + new_password.encode('utf-8') + salt                       
    new_password_hash = sha256(new_password).digest()                               
    if email:                                                                       
        new_settings = types.account.PasswordInputSettings(                         
            new_salt=salt,                                                          
            new_password_hash=new_password_hash,                                    
            email=email,                                                            
            hint=hint)                                                              
    else:                                                                           
        new_settings = types.account.PasswordInputSettings(                         
            new_salt=salt,                                                          
            new_password_hash=new_password_hash,                                    
            hint=hint)                                                              
                                                                                 
    return client.send(functions.account.UpdatePasswordSettings(                        
        current_password_hash, new_settings=new_settings))    

Maybe it would be useful to add it to pyrogram/client/client.py for other users, what do you think?

from pyrogram.

delivrance avatar delivrance commented on June 19, 2024

@1pyxa1

Maybe it would be useful to add it to pyrogram/client/client.py for other users, what do you think?

Definitely! Being a pretty young project there's a lot to be added and suggestions are very welcome.

I was thinking about adding three different methods to create, change and remove the password, or maybe a single one that does everything? What do you think?

from pyrogram.

1pyxa1 avatar 1pyxa1 commented on June 19, 2024

Personally I would prefer a single one that does everything. Maybe put there little more explanation. Because the code for three different methods will be pretty much the same. But of course it's up to you. And i think it's not so important which way to choose.

Here is my final tested version with removing a password also:

def update_password_settings(client,                                             
                             new_password=None,                                  
                             current_password=None,                              
                             email=None,                                         
                             hint='hint'):                                       
                                                                                 
    if new_password is None and current_password is None:                        
        logging.warning("new_password is None and current_password is None.\     
                        Nothing to do.")                                         
        return False                                                             
                                                                                 
    r = client.send(functions.account.GetPassword())                             
    salt_random = os.urandom(8)                                                  
    salt = r.new_salt + salt_random                                              
                                                                                 
    if current_password:                                                         
        current_password = r.current_salt + current_password.encode() + \        
            r.current_salt                                                       
        current_password_hash = sha256(current_password).digest()                
    else:                                                                        
        current_password_hash = salt                                             
                                                                                 
    if new_password:                                                             
        new_password = salt + new_password.encode('utf-8') + salt                
        new_password_hash = sha256(new_password).digest()                        
    else:                                                                        
        return client.send(functions.account.UpdatePasswordSettings(             
            current_password_hash,                                               
            new_settings=types.account.PasswordInputSettings(                    
                new_salt=bytearray(),                                            
                new_password_hash=bytearray(),                                   
                hint=hint)))                                                     
                                                                                 
    if email:                                                                    
        new_settings = types.account.PasswordInputSettings(                      
            new_salt=salt,                                                       
            new_password_hash=new_password_hash,                                 
            email=email,                                                         
            hint=hint)                                                           
    else:                                                                        
        new_settings = types.account.PasswordInputSettings(                      
            new_salt=salt,                                                       
            new_password_hash=new_password_hash,                                 
            hint=hint)                                                           
                                                                                 
    return client.send(functions.account.UpdatePasswordSettings(                 
        current_password_hash, new_settings=new_settings))       

from pyrogram.

delivrance avatar delivrance commented on June 19, 2024

I've opted for three distinct methods, I think it's simpler to use this way: 8cc0fe1

from pyrogram.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.