1

Not sure why this is failing..

./testme.sh
Before Loop
SSL1: /root/a.txt SHA256(/root/a.txt)= 92b165232fbd011da355eca0b033db22b934ba9af0145a437a832d27310b89f9
SSL2: /root/b.txt SHA256(/root/b.txt)= f0f9c277cf17429957daf6594714cc5470ac5c474ba5ac50319185477a174799
different


cp a.txt b.txt
./testme.sh
Before Loop
SSL1: /root/a.txt SHA256(/root/a.txt)= 92b165232fbd011da355eca0b033db22b934ba9af0145a437a832d27310b89f9
SSL2: /root/b.txt SHA256(/root/b.txt)= 92b165232fbd011da355eca0b033db22b934ba9af0145a437a832d27310b89f9
different

Checksums, and the same, but reported different :|

#!/bin/bash
#
# OPENSSL=/usr/bin/openssl
OPENSSL=/usr/local/openssl/bin/openssl
HOME=/root
ENCRYPT=sha256

SSL1=$($OPENSSL $ENCRYPT $HOME/a.txt)
SSL2=$($OPENSSL $ENCRYPT $HOME/b.txt)

## DEBUG
echo "SSL1: $HOME/a.txt $SSL1"
echo "SSL2: $HOME/b.txt $SSL2"


if [ "$SSL1" = "$SSL2" ]
 then
  echo "same"
 else
  echo "different"
fi

3 Answers 3

2

I was literally working on this today. Here's a one-liner tested on OSX

diff  <(openssl sha1 index.css | awk {'print $2'}) <(openssl sha1 original_source/index.css | awk {'print $2'})

No output on success; outputs both hashes on fail.

Sign up to request clarification or add additional context in comments.

Comments

0

$SSL1 contains more than just the hash of the file; it also contains the string SHA256(/root/a.txt)=, which includes the filename. And likewise for $SSL2. So naturally they're not equal, since they contain different filenames.

To get just the hash, there are a few approaches you can take. I think the simplest is just to pipe it to grep:

SSL1=$($OPENSSL $ENCRYPT $HOME/a.txt | grep -o '[0-9a-f]*$')
SSL2=$($OPENSSL $ENCRYPT $HOME/b.txt | grep -o '[0-9a-f]*$')

2 Comments

If I used sha256sum instead, would this need to change?
Ah got it, sha256sum file | cut -d' ' -f1
0

Look closer. The result includes the filename.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.