使用Perl內建的md5sum,可以用來檢查檔案或產生一段md5 checksum。對檔案使用時,記得要先轉為binmode,這樣出來的結果才正確,不然應該釋文字模式的結果。範例如下:

use Digest::MD5 qw(md5 md5_hex md5_base64);

sub md5(){
    my $defaultHash = '202cb962ac59075b964b07152d234b70';
    open FILE, $_[0] || die "open file $!\n";
    binmode(FILE);
    my $hash = md5_hex(<FILE>);
    close FILE;
    say "default hash: ".$defaultHash;
    say "new hash:     ".$hash;
    if ($defaultHash eq $hash) {
        say "Check md5sum OK."
    } else {
        die "files not match!"
    }
}

arrow
arrow
    全站熱搜

    bagatelles 發表在 痞客邦 留言(0) 人氣()