That Newline Thing

October 8, 2009

I finally figured out why I was never able to create matching encrypted passwords for my PHP apps from the command line. For instance:

$ echo abc | shasum
03cfd743661f07975fa2f1220c5194cbaff48451  -
$ php -r 'echo sha1("abc");'

Not quite the same. The reason is of course totally obvious, just not visible - echo puts a newline after everything by default, so instead of the previous, one should use:

$ echo -n abc | shasum
a9993e364706816aba3e25717850c26c9cd0d89d  -

Duh. The same thing with md5(). I think I might have figured this out before but then forgot about it so hopefully it’ll stick better after posting this.