# Copyright 2002-2008 Josh Clark and Global Moxie, LLC. This code cannot be
# redistributed without permission from globalmoxie.com.  For more
# information, consult your Big Medium license.
#
# $Id: MD5.pm 3043 2008-03-31 14:00:38Z josh $

package BigMed::MD5;
use strict;
use warnings;
use utf8;
our @EXPORT_OK = qw(md5_hex);
use base 'Exporter';
use Digest::MD5;

my $PERL_5_8 = ( $] >= 5.008 );    #determines utf-8 handling

#This is a wrapper to Digest::MD5's md5_hex routine.
#That routine doesn't like utf8 strings as argument;
#this converts those strings to plain bytes first.
sub md5_hex {
    my $text = shift;
    if ($PERL_5_8) {
        require Encode;
        if ( Encode::is_utf8($text) ) {
            $text = Encode::encode_utf8($text);
        }
    }
    else {                         #perl 5.6.1
        $text = pack "C*", unpack "U0C*", $text;
    }
    return Digest::MD5::md5_hex($text);
}

1;
__END__
