Hey everyone! I'm diving into bash scripting and hit a little snag. I know how to use OpenSSL for encryption and decryption, like this:
```bash
# Encrypt
echo "secret" | openssl enc -aes-256-cbc -md sha512 -a -pbkdf2 -iter 100000 -salt -pass pass:somePASSWD
# Decrypt
echo "" | openssl enc -d -aes-256-cbc -md sha512 -a -pbkdf2 -iter 100000 -salt -pass pass:somePASSWD
```
But now I'm looking for a one-way encryption method, something that just encrypts the data so I can verify whether user input matches the encrypted value. For example:
```bash
#!/usr/bin/env bash
ORIGINAL=$(echo "sponge-bob" | one-way-encrypt-command)
read -rp "What is the secret?" ANSWER
if [ "$(echo $ANSWER | one-way-encrypt-command)" = "$ORIGINAL" ]; then
echo "Yes, you're right!"
else
echo "Wrong!"
fi
```
How can I achieve that? Any advice would be greatly appreciated!
1 Answer
It sounds like you need a hashing function for one-way encryption. A common choice is SHA-256. You can use `sha256sum` to hash your input. For example, to hash a string, you can do `echo -n "sponge-bob" | sha256sum`. Just store the hash and compare it like you described in your script!
Thanks for the tip! Is there a specific command I should run to make sure I'm using SHA-256 correctly?