• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Hash value different than Java Hash value

New Here ,
Sep 21, 2007 Sep 21, 2007

Copy link to clipboard

Copied

If I hash "test" using Java, I get the following:
"e0ee22535e7b9035397d9425d9d533ca"

if i pass "test" to the coldfusion Hash function i get the following:
"098F6BCD4621D373CADE4E832627B4F6"

Can anybody tell me, why's that?


TOPICS
Advanced techniques

Views

587

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 21, 2007 Sep 21, 2007

Copy link to clipboard

Copied

> If I hash "test" using Java, I get the following

What java code are you using?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 21, 2007 Sep 21, 2007

Copy link to clipboard

Copied

private String hashDocument( String fileName )
{
byte[ ] fileBytes = new byte[ 256 ];
int bytesRead = 0;
String checksumString = "";

try
{
MessageDigest digest = MessageDigest.getInstance( "MD5" );

FileInputStream fis = new FileInputStream( tempPath + fileName );

bytesRead = fis.read( fileBytes );

while ( bytesRead > 0 )
{
bytesRead = fis.read( fileBytes );

digest.update( fileBytes, 0, bytesRead );
}

byte[ ] checksum = digest.digest( );
StringBuffer hexChecksum = new StringBuffer( );

for ( int i = 0; i < checksum.length; i++ )
{
// Convert to a positive value between 0 and 255
int intVal = (int) ( 0x000000ff & checksum[ i ] );

hexChecksum.append( hexCodes[ intVal ] );
}

checksumString = hexChecksum.toString( );
}
catch ( Exception e )
{
log.warn( "Failed to calculate MD5 checksum for document: " + fileName + ".", e );
}

return checksumString;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 21, 2007 Sep 21, 2007

Copy link to clipboard

Copied

That's operating on the contents of a file, not a simple string like "test".

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 21, 2007 Sep 21, 2007

Copy link to clipboard

Copied

Correct, but what I'm saying is the Java hashing produces a different hash than what the Coldfusion hash function does. I was using "test" as an example but get the same kind of results using a file, which is what I'm actually trying to do.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Sep 21, 2007 Sep 21, 2007

Copy link to clipboard

Copied

LATEST
I can't see where all of the variables are defined. For example: hexCodes. Take a look at the public class MD5Util here. The method accepts a string, but the concept is the same.
http://site.gravatar.com/site/implement

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation