www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - MD5 Digest ubyte change to char[]

reply jicman <jicman_member pathlink.com> writes:
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ť
Feb 19 2005
parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
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ť
 
 

Feb 19 2005
parent reply jicman <jicman_member pathlink.com> writes:
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ť
 
 


Feb 19 2005
parent reply jicman <jicman_member pathlink.com> writes:
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ť
 
 



Feb 19 2005
parent reply "Unknown W. Brackets" <unknown simplemachines.org> writes:
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...

Feb 19 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
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
Feb 20 2005