4

I am on Mac OS X 10.8.2, running a compiled copy of xxd v1.10 (source code) as well as the copy of xxd that comes preinstalled on OS X.

I am trying to generate a Base64-encoded SHA1 signature via a chain of piped commands in Terminal.

Normally I would do something like the following:

$ echo "foo" | openssl sha1 | xxd -p -r | base64 - > foo_sha1_signature

The file foo_sha1_signature normally contains a Base64-encoded SHA1 hash of the string foo.

The problem is that xxd -p -r does not return any data, so the file foo_sha1_signature is empty.

If I split the commands up to look at the output from xxd -r, I get a result (as printed below):

$ echo "foo" | openssl sha1 | xxd -p | xxd -r
7b36c94bcdf32bee$

But if I pipe standard output to a file, the file is empty:

$ echo "foo" | openssl sha1 | xxd -p | xxd -r > foo_sha1_bytes
$ ls -al foo_sha1_bytes 
-rw-r--r--  1 alexpreynolds  staff  0 Jan  2 23:02 foo_sha1_bytes

If I try piping standard error, the standard output shows the bytes and the file is still empty:

$ echo "foo" | openssl sha1 | xxd -p | xxd -r 2> foo_sha1_bytes
7b36c94bcdf32bee$

Finally, if I do all this on Linux, xxd works as expected (I get the signature in a file). So this seems to be a bug with how xxd works in OS X 10.8.2.

Is there an open-source alternative to xxd which works on Mac OS X and sends a byte-representation of standard input to standard output?

Alex Reynolds
  • 811
  • 1
  • 9
  • 21
  • I don't have a Mac so I can't say for sure but [od](http://linux.101hacks.com/unix/od-command-examples/) and [hexdump](http://www.novell.com/communities/node/6419/making-sense-hexdump) support the functionality you need. But you'll have to confirm for us if this works on a Mac. – dinesh Jan 03 '13 at 07:52
  • What options do I use with hexdump to get byte output? I can get hex output, but I don't see what option to use to get byte output identical to what xxd outputs. – Alex Reynolds Jan 03 '13 at 15:27
  • 1
    The first command works for me on 10.8.2. (Even though it's the hash of `$'foo\n'`.) openssl, xxd, and base64 are all preinstalled versions. – Lri Jan 03 '13 at 17:25
  • The first command works for me as well, but `xxd -p | xxd -r` does not, and should not be expected to (and is not at all the same as `xxd -p -r`). `xxd -p` does a plaintext hex dump (in this case, of something that is *already* a plaintext hex dump); `xxd -r` attempts to do a reverse (hex -> binary) conversion, but it expects a formatted (not plaintext) dump as input and will get confused when given a plaintext dump instead. – Gordon Davisson Jan 03 '13 at 18:23
  • It works for me on Linux, but not for Mac OS X 10.8.2 on my MacBook Air. Neither the pre-installed version of `xxd` nor the one I compiled. `xxd -p` works, but `xxd -p -r` gives a blank string. Is there an alternative to `xxd` that works the same as `xxd -p -r`? – Alex Reynolds Jan 03 '13 at 18:34
  • @AlexReynolds On a linux machine you can use the `-e` option to specify a format string. In your case, replacing the xxd command with `hexdump '8/1 "%02X"' should work. Check this [link](http://zagaeski.devio.us/0006.html) for more information about specifying format strings for hexdump. If this does work I can post more detailed information about the hexdump utility in an answer. Don't want to do this until we can confirm this works. – dinesh Jan 03 '13 at 21:39
  • On Linux, `echo "foo" | openssl sha1 | hexdump '8/1 "%02X"' -` gives error messages: `hexdump: 8/1 "%02X": No such file or directory`, `hexdump: -: No such file or directory`, and `hexdump: -: Bad file descriptor`. If I leave out the `stdin` descriptor, then I get `hexdump: 8/1 "%02X": No such file or directory` and `hexdump: 8/1 "%02X": Bad file descriptor` errors. I haven't tried this on Mac OS X, yet, but it does come with `hexdump`. Any thoughts? – Alex Reynolds Jan 03 '13 at 22:42
  • Also tried `echo "foo" | openssl sha1 | hexdump -v -e '8/1 "%02X"'`, but this gives the wrong answer, or at least an answer that differs from `xxd` on Linux. – Alex Reynolds Jan 03 '13 at 22:45

1 Answers1

2

You should try

xxd -b <file>

It will display the file in binary mode.

luiscabus
  • 21
  • 4