开发者

AppleScript and MD5

I'm simply trying to compute the MD5 of a string using AppleScript:

set hash_string to do shell script "echo -n hi | md5"

In the code above, I've actually replaced the string passed to shell with "hi" as a test. AppleScript reports:

tell current application
    do shell script "echo -n hi | md5"
        --> "5ea3a74c5a5c60abd09da8c5f85fa0a5"
end tell

Running the same command directly in a terminal results in:

% echo -n hi | md5
49f68a5c8493ec2c0bf489821c21fc3b

Though the command is identical, the computed hash is different. Obviously, additional characters are bei开发者_Go百科ng passed in AppleScript, but I can't seem to figure out what they are. I appreciate any guidance.


You can see what is happening if you remove the | md5. In the AppleScript case, you'll see -n hi as output. That's because the POSIX sh echo, which is invoked by do shell script, does not support -n whereas the bash or csh echo builtins or the /bin/echo command all do support -n.

$ bash
$ echo -n hi
hi$ sh
$ echo -n hi
-n hi

See the Mac OS X echo man page or the POSIX documentation for echo for more info. Note, POSIX compliance requires that conforming implementations of echo must not support any options including -n; for compatibility across shells and systems, the POSIX editors suggest using printf to do things like suppressing newlines.

tell current application
    do shell script "printf '%s' hi | md5"
end tell
--> "49f68a5c8493ec2c0bf489821c21fc3b"


If you drop the -n from the echo command, then it should work fine. Not totally sure why... but that seems to fix it!


this works

set passwordPlain to "hi"
set foo to do shell script "/sbin/md5 -qs" & passwordPlain
return foo

this results and the same as

 echo -n hi | md5 

from command prompt

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜