将Java MD5转换为Perl MD5

时间:2018-05-28 13:43:00

标签: perl

在我的项目中,我有这个java代码,我想要等效的Perl程序。任何人都可以帮助我将此代码转换为Perl程序。 下面的java代码将输入流作为输入并获取消化的字节数组,然后将这个消化的字节数组传递给十六进制表示法,并为其提供md5哈希值。

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

在Perl代码下面尝试但是它与上面的哈希不匹配。

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

1 个答案:

答案 0 :(得分:0)

你的十六进制功能是不是错了?它从后到前创建了十六进制字符串。

因此,如果您的字节是EF BE AD DE,则输出将为DEADBEEF。

仅供参考:perl中的hex函数(你在其他地方要求)将是:

sub hexer {
  return unpack "(H2)*", join '', map chr, reverse @_;
}