Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

Reproducing a hash code

瀏覽次數:76 次
跳到第一則未讀訊息

Steve Luttrell

未讀,
2007年11月5日 清晨5:03:222007/11/5
收件者:
I am trying to get results from Mathematica's Hash function (for hash type
"MD5") that are consistent with the results obtained using "openssl md5" in
Linux.

The particular example I would like to reproduce is on
http://wiki.secondlife.com/wiki/LlMD5String, where

"Hello, Avatar!:0"

hashes to (for hash type "MD5")

112abd47ceaae1c05a826828650434a6

In Mathematica 6 (Windows Vista, Intel hardware), doing the obvious

Hash["Hello, Avatar!:0", "MD5"] // BaseForm[#, 16] &

I get the following result

a8c2ab1662a37cb9f8ea5af154a48c52

This is not the same as expected. Does anyone know how to get consistent
results?

--
Steve Luttrell
West Malvern, UK


michael.p...@googlemail.com

未讀,
2007年11月6日 凌晨3:46:172007/11/6
收件者:
On 5 Nov, 10:03, "Steve Luttrell"

<steve@_removemefirst_luttrell.org.uk> wrote:
> I am trying to get results from Mathematica's Hash function (for hash type
> "MD5") that are consistent with the results obtained using "openssl md5" in
> Linux.
>
> The particular example I would like to reproduce is onhttp://wiki.secondlife.com/wiki/LlMD5String, where

>
> "Hello, Avatar!:0"
>
> hashes to (for hash type "MD5")
>
> 112abd47ceaae1c05a826828650434a6
>
> In Mathematica 6 (Windows Vista, Intel hardware), doing the obvious
>
> Hash["Hello, Avatar!:0", "MD5"] // BaseForm[#, 16] &
>
> I get the following result
>
> a8c2ab1662a37cb9f8ea5af154a48c52
>
> This is not the same as expected. Does anyone know how to get consistent
> results?
>
> --
> Steve Luttrell
> West Malvern, UK

Hi Steve

It seems that Mathematica includes the enclosing quotes "" when it
generate it's hash:

As you point out, in Mathematica 6 (on windows and Linux) if we do


Hash["Hello, Avatar!:0", "MD5"] // BaseForm[#, 16] &

we get
a8c2ab1662a37cb9f8ea5af154a48c52

Now onto our linux box - if we do (notice the extra quotes I have
added)

echo -n '"Hello, Avatar!:0"' | openssl md5

we get

a8c2ab1662a37cb9f8ea5af154a48c52

as before.
Hope this helps,
Mike


Mark Fisher

未讀,
2007年11月7日 清晨6:31:172007/11/7
收件者:

Now that Mike has diagnosed the problem, here's a solution:

StringHash[string_String, type_:"MD5"] :=
Module[{stream, file, hash},
stream = OpenWrite[];
WriteString[stream, string];
file = Close[stream];
hash = FileHash[file, type];
DeleteFile[file];
hash
]

Then

StringHash["Hello, Avatar!:0"] // BaseForm[#, 16] &

produces

112abd47ceaae1c05a826828650434a6

--Mark


Steve Luttrell

未讀,
2007年11月8日 清晨6:11:252007/11/8
收件者:
Excellent diagnosis and solution. Thank you both.

--
Steve Luttrell
West Malvern, UK

"Mark Fisher" <particl...@gmail.com> wrote in message
news:fgs7m5$3h5$1...@smc.vnet.net...

0 則新訊息