Erlang Equivalent of C# Hash

Johan Bevemyr jb@REDACTED
Fri Dec 23 00:50:11 CET 2005


I use the following code on the C# side:

*using System;
using System.Security.Cryptography;
*
    private string mk_digest(string str) {
        byte[] bHashResult = new byte[21];
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        byte[] bMD5Hash = Encoding.GetEncoding("iso-8859-1").GetBytes(str);
        bHashResult = md5.ComputeHash(bMD5Hash);
        return Convert.ToBase64String(bHashResult);
    }

Works nicely with Jockes code below on the Erlang side.
-Johan

Joakim G. wrote:

> and yes
>
> To compare the digests you have to do something like this on
> the Erlang side:
>
>     Context = erlang:md5_init(),
>     NewContext = erlang:md5_update(Context, String),
>     base64:str_2_base64(binary_to_list(erlang:md5_final(NewContext)))
>
> /Jocke
>
> PS http://www.sics.se/~joe/bluetail/eol/base64.erl DS
>
> Joakim G. wrote:
>
>> Jing Ding wrote:
>>
>>> Hi there,
>>> I previously asked about how to do MD5. And I got answers.
>>> Thanks for your help. But the binary result is not the same as the 
>>> string generated from C# :
>>> ------------------------------------------------------
>>> byte[] a = md5Hash.ComputeHash(AE.GetBytes(UserID + value));
>>> PasswordHash = Convert.ToBase64String(a);
>>> ---------------------------------------------------
>>> Anyone else happen to came across this? Thanks.
>>
>>
>>
>> Do like this in C#:
>>
>> ==
>>
>>     byte[] buffer = System.Text.Encoding.Default.GetBytes(String);
>>     System.Security.Cryptography.MD5CryptoServiceProvider Check;
>>     Check = new System.Security.Cryptography.MD5CryptoServiceProvider();
>>     byte[] Hash = Check.ComputeHash(buffer);
>>     string HashString = "";
>>     foreach (byte a in Hash) {
>>         if (a < 16) {
>>             HashString += "0" + a.ToString("X");
>>         } else {
>>             HashString += a.ToString("X");
>>         }
>>     }
>>     return HashString.ToLower();
>>
>> ==
>>
>> and it works with Erlang's md5 engine and Perl's CPAN
>> md5_base64 (in Digest::MD5::Perl) out of the box.
>>
>> If you like a pragmatic answer. This is it. :-)
>>
>> Cheers
>> /Jocke
>
>
>




More information about the erlang-questions mailing list