# perldoc > Digest::MD5

---
type: CommandReference
command: Digest::MD5
mode: perldoc
section: 
source: perldoc
---

## Quick Reference
- `md5_hex($data)` — return 32‑char hex digest
- `md5($data)` — return 16‑byte binary digest
- `md5_base64($data)` — return 22‑char base64 digest (not padded)
- `Digest::MD5->new->add($data)->hexdigest` — OO hex digest
- `Digest::MD5->new->addfile($fh)->hexdigest` — efficient file hex digest
- `$md5->clone->hexdigest` — intermediate digest without resetting state
- `encode_utf8($str)` then `md5_hex(...)` — correct digest for Unicode strings

## Name
Perl interface to the MD5 Message‑Digest Algorithm

## Synopsis
perl
# Functional style
use Digest::MD5 qw(md5 md5_hex md5_base64);

$digest = md5($data);
$digest = md5_hex($data);
$digest = md5_base64($data);

# Object‑oriented style
use Digest::MD5;

$ctx = Digest::MD5->new;
$ctx->add($data);
$ctx->addfile($file_handle);

$digest = $ctx->digest;
$digest = $ctx->hexdigest;
$digest = $ctx->b64digest;
## Functions
- `md5($data, ...)` — concatenates all arguments, returns 16‑byte binary digest.
- `md5_hex($data, ...)` — like `md5()` but returns 32‑char hexadecimal digest (0‑9, a‑f).
- `md5_base64($data, ...)` — like `md5()` but returns 22‑char base64 digest (no padding). Append "==" for interoperability with padded base64.

## Methods
- `new()` — constructs a new `Digest::MD5` object; when called as an instance method, resets the existing object.
- `reset()` — alias for `new()`.
- `clone()` — returns a copy of the object; useful for obtaining intermediate digests without resetting the original.
- `add($data, ...)` — appends data to the message; returns the object itself for chaining.
- `addfile($io_handle)` — reads the filehandle until EOF and appends its content; returns the object. Croaks on read failure. Use `binmode` on the handle.
- `add_bits($data, $nbits)` or `add_bits($bitstring)` — provided for compatibility; prefer `add()`.
- `digest()` — returns the 16‑byte binary digest and **resets** the object. Use `$md5->clone->digest` to avoid reset.
- `hexdigest()` — returns 32‑char hex digest; resets the object.
- `b64digest()` — returns 22‑char base64 digest (no padding); resets the object.
- `context()` — when called with no arguments, returns internal state (blocks, state buffer, unprocessed data). When called with that list, restores the state. Rarely needed.

## Examples
perl
# Functional hex digest of a string
use Digest::MD5 qw(md5_hex);
print "Digest is ", md5_hex("foobarbaz"), "\n";
# Output: Digest is 6df23dc03f9b54cc38a0fc1483df6e21

# OO hex digest, chunked
use Digest::MD5;
$md5 = Digest::MD5->new;
$md5->add('foo', 'bar');
$md5->add('baz');
print $md5->hexdigest, "\n";

# File checksum using addfile
use Digest::MD5;
my $filename = "/etc/passwd";
open my $fh, '<', $filename or die $!;
binmode($fh);
print Digest::MD5->new->addfile($fh)->hexdigest, " $filename\n";

# Handling Unicode strings via UTF-8
use Digest::MD5 qw(md5_hex);
use Encode qw(encode_utf8);
my $str = "abc\x{300}";
print md5_hex(encode_utf8($str)), "\n";   # 8c2d46911f3f5a326455f0ed7a8ed3b3
## See Also
- [Digest](https://metacpan.org/pod/Digest)
- [Digest::MD2](https://metacpan.org/pod/Digest::MD2)
- [Digest::SHA](https://metacpan.org/pod/Digest::SHA)
- [Digest::HMAC](https://metacpan.org/pod/Digest::HMAC)
- [RFC 1321](https://www.rfc-editor.org/rfc/rfc1321)
- [MD5 on Wikipedia](https://en.wikipedia.org/wiki/MD5)