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);