Convert PHP code into Ruby!
RSS icon Email icon Home icon
  • md5_file

    Posted on May 14th, 2009 RubyLove No comments

    The md5_file() function calculates the md5 hash of a file.

    PHP

    $my_file = '/home/ali/article.txt';
     
    echo md5_file($my_file);
     
    // => 745d3dfb68af1b1384aea0125aae5c3f

    MD5 encryption can be done using the Digest::MD5 class in Ruby, but to generate a hash for a file, we need to read it’s contents into a variable (string) first.

    Ruby

    require 'digest/md5';
     
    my_string = File.read('/home/ali/article.txt');
     
    print Digest::MD5.hexdigest(my_string);
     
    # => 745d3dfb68af1b1384aea0125aae5c3f