do shell script Result Differs from Terminal Result

I'm attempting to write an AppleScript that needs to generate a SHA256 hash from a Variable.


Using do shell script, OpenSSL is given a string to hash. However the result given from AppleScript is different to the result given from Terminal.


AppleScript:

set myString to do shell script ("echo -n stringtohash | /usr/local/bin/openssl sha256 2>&1")

display dialog (myString)

User uploaded file

Terminal:

Computer:~ Matt$ echo -n stringtohash | /usr/local/bin/openssl sha256 2>&1

(stdin)= bc287cfa8c0c84f0b1489bc70ec1bdf75e593c13dc8b8a19bc2d0039966d69d1

MacBook Pro (Retina, 15-inch, Late 2013), macOS Sierra (10.12.5)

Posted on Sep 1, 2017 12:14 AM

Reply
Question marked as Top-ranking reply

Posted on Sep 1, 2017 1:28 AM

Found answer in previous discussion: "do shell script" returns different from Terminal. Why?


It looks like the shell built-in command uses a different encoding, so using printf or the standalone echo should give more consistent results:


do shell script "/bin/echo -n 'RolfAaberge'|openssl sha1"


Works for me:

do shell script ("/bin/echo -n stringtohash | /usr/local/bin/openssl sha256 2>&1")

1 reply
Question marked as Top-ranking reply

Sep 1, 2017 1:28 AM in response to mattchesters

Found answer in previous discussion: "do shell script" returns different from Terminal. Why?


It looks like the shell built-in command uses a different encoding, so using printf or the standalone echo should give more consistent results:


do shell script "/bin/echo -n 'RolfAaberge'|openssl sha1"


Works for me:

do shell script ("/bin/echo -n stringtohash | /usr/local/bin/openssl sha256 2>&1")

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

do shell script Result Differs from Terminal Result

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.