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
$ 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?