-1

In my project, I have this java code I want the equivalent Perl program. Can anyone help me converting this code to Perl program. Below java code takes the input stream as an input and get the digested byte array and then it passes this digested byte array to hexadecimal notation and gives us the md5 hash out of it.

        public class Verifier {
            public static String DIGEST_ALGORITHM = "MD5";
            private static char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

            public static void main(String[] args) {
                verify(new File("C:/Users/kanna/Desktop/test/testfiles/Test.txt"));
            }

            static void verify(File f) {
                try {
                    FileInputStream fis = new FileInputStream(f);
                    String imageHash= hex(digest(new InputStream[] { fis }));
                    System.out.println(imageHash);
                } catch (Exception e) {
                    System.out.println("All gone Pete Tong on file " + f.getName());
                }
            }

            public static String hex(byte[] bytes) {
                char[] c = new char[bytes.length * 2];
                for (int i = 0; i < bytes.length; i++) {
                    int j = (bytes.length - i - 1) * 2;
                    c[(j + 0)] = HEX[(bytes[i] >> 4 & 0xF)];
                    c[(j + 1)] = HEX[(bytes[i] >> 0 & 0xF)];
                }
                return new String(c);
            }

            public static byte[] digest(InputStream[] in) throws IOException {
                try {
                    MessageDigest messageDigest = MessageDigest.getInstance(DIGEST_ALGORITHM);
                    byte[] b = new byte['?'];
                    try {
                        int l;
                        for (int i = 0; i < in.length; i++) {
                            for (l = 0; (l = in[i].read(b)) > 0;) {
                                messageDigest.update(b, 0, l);
                            }
                        }
                    } finally {

                        for (int i = 0; i < in.length; i++) {
                            try {
                                in[i].close();
                            } catch (Exception e) {
                            }
                        }
                    }
                    return messageDigest.digest();
                } catch (NoSuchAlgorithmException e) {
                    throw new RuntimeException(e);
                }
            }
        }

Tried below Perl code but it's not matching with the above hash.

                use Digest::MD5 qw(md5);
                my $ffname="C:/Users/yuvarar/Desktop/BMS/dcmfiles/Test.dcm";
                open FILE, "$ffname";
                my $ctx = Digest::MD5->new;
                $ctx->addfile (*FILE);
                my $hash = $ctx->hexdigest;
                close (FILE);
                printf("md5_hex:%s\n",$hash);
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • Why you reinvent the wheel? There a re perl modules which generates md5 – Jens May 28 '18 at 13:45
  • thanks for thinking through, i used libraries to generate md5 has but library generated md5 not matching with md5 generated by above java code. – Rajesh Kumar Yuvaraj May 28 '18 at 14:02
  • 1
    @RajeshKumarYuvaraj Most likely the manual MD5 generation is incorrect. I would doubt mostly about it rather than a bug in a cryptographic library. You can confirm that by running the hash in some online sites or some other library. – Alejandro May 28 '18 at 14:35
  • 2
    You have a bug in your code somewhere. You now have an excellent opportunity to debug it! –  May 28 '18 at 14:43
  • @Alejandro thanks for sharing thought. The same cryptographic logic is working from ages. They have costomized the logic for certain reasons and it's in Java. And I want that to be converted to PERL script – Rajesh Kumar Yuvaraj May 28 '18 at 16:51
  • @jdv can you please be more specific about the bug – Rajesh Kumar Yuvaraj May 28 '18 at 16:53
  • 2
    @RajeshKumarYuvaraj if your code does not match with well-tested and well-reviewed crypto code, then it is almost certain that you have a bug in your code (or the way you are comparing is flawed). You can compare with https://stackoverflow.com/a/415956/1531971 –  May 28 '18 at 17:09
  • 3
    @RajeshKumarYuvaraj It it has "customized the logic" then you're no longer calculating the MD5, but some custom made hash. And it's strongly adviced that [you should never, ever do that](https://security.stackexchange.com/questions/18197/why-shouldnt-we-roll-our-own). It's just better to use the normal, well-testted MD5 in both sides. – Alejandro May 28 '18 at 17:21
  • I debug the code and there is no issue with MD5 hashing, my code is doing additional steps like converting it to hexa decimal. Can some one help me with equivalent hexa decimal code in perl? – Rajesh Kumar Yuvaraj May 29 '18 at 11:27

1 Answers1

0

Could it be your hex function is wrong? It's creating the hex string from back to front.

So if you bytes are EF BE AD DE, the output will be DEADBEEF.

FYI: The hex function in perl (you asked for it elsewhere) would be:

sub hexer {
  return unpack "(H2)*", join '', map chr, reverse @_;
}
Skeeve
  • 7,188
  • 2
  • 16
  • 26