I'm trying to apply SHA256 and then Base64 encode a string inside a shell script. Got it working with PHP:
php -r 'echo base64_encode(hash("sha256", "asdasd", false));'
. But I'm trying to get rid of the PHP dependency.
Got this line that works well in the terminal (using the fish
shell):
$ echo -n "asdasd" | shasum -a 256 | cut -d " " -f 1 | xxd -r -p | base64
X9kkYl9qsWoZzJgHx8UGrhgTSQ5LpnX4Q9WhDguqzbg=
But when I put it inside a shell script, the result differs:
$ cat foo.sh
#!/bin/sh
echo -n "asdasd" | shasum -a 256 | cut -d " " -f 1 | xxd -r -p | base64
$ ./foo.sh
IzoDcfWvzNTZi62OfVm7DBfYrU9WiSdNyZIQhb7vZ0w=
How can I make it produce expected result? My guess is that it's because of how binary strings are handled?
$ bash
) and run the script the output is the same though. – Niklas Berglund Dec 09 '15 at 10:47readlink -f /bin/sh
? What shell is your sh? – terdon Dec 09 '15 at 11:02greadlink -f /bin/sh
is saying /bin/sh – Niklas Berglund Dec 09 '15 at 11:20sh
implementation that OSX ships with has anecho
that doesn't understand-n
. Unlike your defaultfish
shell's. – terdon Dec 09 '15 at 11:22