B.Ondrej
B.Ondrej

Reputation: 390

Piping sha256sum and base64

password="admin"
password_shasum=$(echo -n $password | shasum -a 256 | awk '{print $1}')
password_hashed=$(echo -n $password_shasum | openssl base64 -A)

gives expected correct RESULT: OGM2OTc2ZTViNTQxMDQxNWJkZTkwOGJkNGRlZTE1ZGZiMTY3YTljODczZmM0YmI4YTgxZjZmMmFiNDQ4YTkxOA==

OR

password="admin"
password_hashed=$(echo -n $password | shasum -a 256 | awk '{print $1}' | openssl base64 -A )

gives unexpected wrong RESULT:

OGM2OTc2ZTViNTQxMDQxNWJkZTkwOGJkNGRlZTE1ZGZiMTY3YTljODczZmM0YmI4YTgxZjZmMmFiNDQ4YTkxOAo=

i need to understand why Bash behaves this way

Upvotes: 2

Views: 1399

Answers (1)

pah
pah

Reputation: 4778

The awk ORS defaults to \n, which is what being included in the string you're encoding... you need to remove that.

If you really want to use print you need to change ORS to empty string:

password="admin"
password_hashed=$(echo -n "$password" | shasum -a 256 | awk 'BEGIN {ORS=""} {print $1}' | openssl base64 -A)

Or you can use printf instead:

password="admin"
password_hashed=$(echo -n "$password" | shasum -a 256 | awk '{printf "%s",$1}' | openssl base64 -A)

Upvotes: 2

Related Questions