简体   繁体   中英

how do I save a password on a file using UNIX/LINUX?

I only know how to read a password. But I'm having a problem on how to set a password inputted by the user for a file. I've only gone this far. Please help:

if [ -d "$1" ]
then
    #reads password
    read -s -p "Enter Password: " password
    stty -echo
    stty echo
    echo ""
    echo ""
    #checks whether the password is valid or not
    echo $mypassword" == "$PASS" ] && echo "Password accepted" || echo "Access denied"
else
    echo "Directory not found."
fi

If I don't get you wrong, try something like this:

#!/bin/bash

EXPECTED_PASS="FooBar"

if [ -d "$1" ]
then
    read -s -p "Enter Password: " password
    stty -echo
    stty echo
    echo ""
    echo ""

    #Save password to a temporary file
    echo "${password}" > /tmp/password.txt

    #Load password from file
    password=`cat /tmp/password.txt`

    if [ "${password}" == "${EXPECTED_PASS}" ]
    then
        echo "Access granted"
    else
        echo "Access denied"
    fi


else
    echo "Directory not found."
fi

Usually it's not a good idea to store passwords as plain text. It's better to encrypt them or at least perform a hash operation over them:

EXPECTED_PASS=`echo "FooBar" | md5sum | cut -f1 -d" "`
...
#Save password to a temporary file
echo "${password}"  | md5sum | cut -f1 -d" " > /tmp/password.txt

#Load password from file
password=`cat /tmp/password.txt`

Hope it helps.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM