Thread overview
MD5 Digest ubyte change to char[]
Feb 20, 2005
jicman
Feb 20, 2005
jicman
Feb 20, 2005
jicman
Feb 20, 2005
Regan Heath
February 20, 2005
Greetings!

I have this code,

char[] CalculateMD5(byte[] Buffer)
{
char[] ehash = "return";
ubyte digest[16];
MD5_CTX context;
context.start();
context.update(Buffer[0..length]);
context.finish(digest);
printDigest(digest);
for (int i=0; i<digest.length;i++)
writefln(digest[i]);
//ehash = digest;
return(ehash);
}

I need to change digest to be a char[].  I tried

std.string.toString(digest)

but I get this error:


21:38:36.05>dmd DeleteTokens.d ws2_32.lib
DeleteTokens.d(383): cannot implicitly convert expression digest of type
ubyte[16] to char[]

Any ideas?

thanks.

josé


February 20, 2005
You can try explicitly casting it... but, really, remember that you won't get a string per say, but rather bytes... it really *is* bytes.

You don't want to convert it to a hexadecimal string, do you?

-[Unknown]


> Greetings!
> 
> I have this code,
> 
> char[] CalculateMD5(byte[] Buffer)
> {
> char[] ehash = "return";
> ubyte digest[16];
> MD5_CTX context;
> context.start();
> context.update(Buffer[0..length]);
> context.finish(digest);
> printDigest(digest);
> for (int i=0; i<digest.length;i++)
> writefln(digest[i]);
> //ehash = digest;
> return(ehash);
> }
> 
> I need to change digest to be a char[].  I tried
> 
> std.string.toString(digest)
> 
> but I get this error:
> 
> 
> 21:38:36.05>dmd DeleteTokens.d ws2_32.lib
> DeleteTokens.d(383): cannot implicitly convert expression digest of type
> ubyte[16] to char[]
> 
> Any ideas?
> 
> thanks.
> 
> josé
> 
> 
February 20, 2005
Yeah, I do want it hex.  But, after posting, I did a search, :-) and guess what? I found the code:

char [] ChangeToChar(ubyte[] digest)
{
char[] strOut;
foreach (ubyte b; digest)
strOut ~= std.string.format("%02x",b);
return(strOut);
}

I posted there for easier search. :-)

thanks.


In article <cv9285$1l0p$1@digitaldaemon.com>, Unknown W. Brackets says...
>
>You can try explicitly casting it... but, really, remember that you won't get a string per say, but rather bytes... it really *is* bytes.
>
>You don't want to convert it to a hexadecimal string, do you?
>
>-[Unknown]
>
>
>> Greetings!
>> 
>> I have this code,
>> 
>> char[] CalculateMD5(byte[] Buffer)
>> {
>> char[] ehash = "return";
>> ubyte digest[16];
>> MD5_CTX context;
>> context.start();
>> context.update(Buffer[0..length]);
>> context.finish(digest);
>> printDigest(digest);
>> for (int i=0; i<digest.length;i++)
>> writefln(digest[i]);
>> //ehash = digest;
>> return(ehash);
>> }
>> 
>> I need to change digest to be a char[].  I tried
>> 
>> std.string.toString(digest)
>> 
>> but I get this error:
>> 
>> 
>> 21:38:36.05>dmd DeleteTokens.d ws2_32.lib
>> DeleteTokens.d(383): cannot implicitly convert expression digest of type
>> ubyte[16] to char[]
>> 
>> Any ideas?
>> 
>> thanks.
>> 
>> josé
>> 
>> 


February 20, 2005
I have a suggestion for the d folks, perhaps we could add a toString to the md5 library. So,

char[] hash = digest.toString();

or something like that.

just a thought...


In article <cv93de$1mdv$1@digitaldaemon.com>, jicman says...
>
>
>Yeah, I do want it hex.  But, after posting, I did a search, :-) and guess what? I found the code:
>
>char [] ChangeToChar(ubyte[] digest)
>{
>char[] strOut;
>foreach (ubyte b; digest)
>strOut ~= std.string.format("%02x",b);
>return(strOut);
>}
>
>I posted there for easier search. :-)
>
>thanks.
>
>
>In article <cv9285$1l0p$1@digitaldaemon.com>, Unknown W. Brackets says...
>>
>>You can try explicitly casting it... but, really, remember that you won't get a string per say, but rather bytes... it really *is* bytes.
>>
>>You don't want to convert it to a hexadecimal string, do you?
>>
>>-[Unknown]
>>
>>
>>> Greetings!
>>> 
>>> I have this code,
>>> 
>>> char[] CalculateMD5(byte[] Buffer)
>>> {
>>> char[] ehash = "return";
>>> ubyte digest[16];
>>> MD5_CTX context;
>>> context.start();
>>> context.update(Buffer[0..length]);
>>> context.finish(digest);
>>> printDigest(digest);
>>> for (int i=0; i<digest.length;i++)
>>> writefln(digest[i]);
>>> //ehash = digest;
>>> return(ehash);
>>> }
>>> 
>>> I need to change digest to be a char[].  I tried
>>> 
>>> std.string.toString(digest)
>>> 
>>> but I get this error:
>>> 
>>> 
>>> 21:38:36.05>dmd DeleteTokens.d ws2_32.lib
>>> DeleteTokens.d(383): cannot implicitly convert expression digest of type
>>> ubyte[16] to char[]
>>> 
>>> Any ideas?
>>> 
>>> thanks.
>>> 
>>> josé
>>> 
>>> 
>
>


February 20, 2005
I agree. I find the hexadecimal hash useful when dealing with non-bytesafe environments/connections...

-[Unknown]


> I have a suggestion for the d folks, perhaps we could add a toString to the md5
> library. So,
> 
> char[] hash = digest.toString();
> 
> or something like that.
> 
> just a thought...
February 20, 2005
On Sat, 19 Feb 2005 21:03:50 -0800, Unknown W. Brackets <unknown@simplemachines.org> wrote:
> I agree. I find the hexadecimal hash useful when dealing with non-bytesafe environments/connections..
>
> -[Unknown]
>
>
>> I have a suggestion for the d folks, perhaps we could add a toString to the md5
>> library. So,
>>  char[] hash = digest.toString();
>>  or something like that.
>>  just a thought...

Y'all might want to check out the Deimos library:
  http://www.dsource.org/projects/deimos/

I wrote some basic crypto functions for MD2, MD4, MD5, SHA0, SHA1, SHA256, SHA512, Tiger, including a templated function for producing a hex string of the various digests.

To see the source itself:
  http://svn.dsource.org/svn/projects/deimos/trunk/etc/crypto/hash/

Regan