Reputation: 3474
I am trying to generate an MD5 out of a string that is a hex byte array (like '2b5ed7db5cf98eda41d10585'). With a normal string I can run md5 -qs the_text
, but I have no idea on how to make the md5 command to read my byte array instead of reading it as plain text. I want to be able to run the command in a way that
echo a | md5 == echo 0x63 | md5
Any ideas?
Upvotes: 0
Views: 835
Reputation: 109
I assume you meant to compare 0x63 to character 'c' and not 'a' since that is the ascii encoding for hex value 63 confirmed here. You should try backslash notation in bash:
# first with plain text
$ echo -e c | md5
2cd6ee2c70b0bde53fbe6cac3c8b8bb1
# with single quotes
$ echo -e '\x63' | md5
2cd6ee2c70b0bde53fbe6cac3c8b8bb1
# or escaping the backslash
$ echo -e \\x63 | md5
2cd6ee2c70b0bde53fbe6cac3c8b8bb1
If you're using zsh then you don't need the -e
flag. If you have to use the Bourne shell 'sh' then you should use printf
. The caveat is that you have to append a newline \n
in order to get the same hash as echo
(since it adds it by default):
# using bash
$ printf 'c' | md5
4a8a08f09d37b73795649038408b5f33
$ printf 'c\n' | md5
2cd6ee2c70b0bde53fbe6cac3c8b8bb1
$ printf '\x63\n' | md5
2cd6ee2c70b0bde53fbe6cac3c8b8bb1
$ printf \\x63\\n | md5
2cd6ee2c70b0bde53fbe6cac3c8b8bb1
# using sh
sh-3.2$ printf '\x63\n' | md5
2cd6ee2c70b0bde53fbe6cac3c8b8bb1
sh-3.2$ printf \\x63\\n | md5
2cd6ee2c70b0bde53fbe6cac3c8b8bb1
As mentioned in the comments by @Ken, you can also use $'\x63'
in (ba)sh:
sh-3.2$ echo $'\x63' | md5
2cd6ee2c70b0bde53fbe6cac3c8b8bb1
Upvotes: 2