kunimihk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Java
--------------------------------------------
byte[] hash;
md5 = MessageDigest.getInstance("MD5");
hash = md5.digest("abcd1234").getBytes("UTF-8");
return new BingInteger(hash).abs();
--------------------------------------------

Perl
--------------------------------------------
use Digest::MD5 qw/md5 md5_hex/;
use Math::BigInt;
my $str = "abcd1234";
my $hex_form = md5_hex($str)
my $bigint = Math::BigInt->new( '0x'.$hex_form );
return $bigint;
--------------------------------------------

its have different values.
how can i do?
help me! any help is welcome!

Replies are listed 'Best First'.
Re: Converting Java to Perl (MD5)
by tobyink (Canon) on Feb 25, 2013 at 13:28 UTC

    I believe in the Java example you should be applying .getBytes("UTF-8") to the plaintext string; not to the digest. This is because Java strings are UTF-16.

    The following two programs both seem to output the same MD5 sum...

    use Digest::MD5 'md5_hex'; use Math::BigInt; my $plain = "abcd1234"; my $digest = Math::BigInt::->from_hex(md5_hex $plain); print $digest, "\n";

    (Run the above using perl hash.pl.)

    import java.security.MessageDigest; import java.math.BigInteger; public class Hash { public static void main( String[] args ) throws Exception { MessageDigest md5 = MessageDigest.getInstance("MD5"); String plain = "abcd1234"; BigInteger digest = new BigInteger(1, md5.digest(plain.getB +ytes("UTF-8"))); System.out.println( digest.abs() ); } }

    (Run the above using javac Hash.java && java -cp . Hash.)

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      Thank you! definitely two programs both seem to output the same MD5 sum
      -----------------------------------------------------------
      Java
      String plain = "abcd1234";
      BigInteger digest = new BigInteger(1, md5.digest(plain.getB +ytes("UTF-8")));

      Perl
      my $plain = "abcd1234";
      my $digest = Math::BigInt::->from_hex(md5_hex $plain);
      -----------------------------------------------------------

      But i want something different
      -----------------------------------------------------------
      Java
      String plain = "abcd1234";
      BigInteger digest = new BigInteger(md5.digest(plain.getB +ytes("UTF-8")));

      Perl
      my $plain = "abcd1234";
      my $digest = Math::BigInt::->from_hex(md5_hex $plain);
      -----------------------------------------------------------
      its two programs output diffrent MD5 sum.. my $digest = Math::BigInt::->from_hex(md5_hex $plain); <--
       

        The one argument form of the Java BigInteger constructor assumes you're passing in a binary two's complement integer.

        Math::BigInt doesn't have a two's complement constructor. You'd need to write one.

        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: Converting Java to Perl (MD5)
by 7stud (Deacon) on Feb 25, 2013 at 19:53 UTC
    ++ for remembering how to compile and run a java program.