How can I hide password in file using python

Viewed 26

File1 which has content

    Username = xyz
    password = 1234
    userId   =  abc
    sshkey   =  pqr1
    username = ghq
    userPassword = 456

File2 which I want to create and tranfer all the data from File1 to File2 but need to change all the password = ****, sskey = **** etc.

Coding I did:

list1 = ["password", "sshkey"]
new_list = []
content_str = ""
with open("file.txt") as content:
    contents =  content.readlines()
with open("file2.txt", "w") as f2:
    for l in contents:
        for data in list1:
            if data in l.lower():
                pas = l.split("=")
                pas = pas[1] + "=" +"***"
                new_list.append(pas)
            else:
                new_list.append(l)
   content_str = content_str.join(new_list)
   f2.write(content_str)

Now I want once I create new file so all the password or sshkey etc has to be ****

I tried a lot of different ways but didn't succeed.

1 Answers

f2.write("****") does the trick...

Nothing fancy about it. Of course you lose all the original data, you can't have it both ways.

That's the eternal problem storing secret information, at some point you must have a cleartext password of some sort to decrypt everything else.

Related