I want to store a password in a config file, something as below. I want my scripts to use it. But when The config file is opened using vi or more or cat, it should display as 'XXXXX' but not its actual value.

PASSWORD=XXXXXXXXXXXX
2

There are 2 best solutions below

0
Gilles Quénot On

A simple implementation:

Input file

LOGIN=foobar
PASSWORD=YouDontKnowMeIamSecret
lorem
ipsum 
doloris

Your cat implementation with required feature:

alias mycat="sed -E 's/^(PASSWORD=).*/\1XXXXXX/'"

then

mycat file

Output

LOGIN=foobar
PASSWORD=XXXXXX
lorem
ipsum 
doloris
5
Romeo Ninov On

You can follow the UNIX/Linux way. Which mean you can store the hash of the password in config file, something like:

LOGIN=foobar
PASSWORD=6b3a55e0261b0304143f805a24924d0c1c44524821305f31d9277843b8a10f4e
lorem
ipsum 
doloris

and when user/program want to use it provide the password password and a program, script do sha256 over this string and compare the result with the value, stored in config

This above is just a example and you should implement additional measures to avoid rainbow tables attack for example.