md5_bin#

One-shot MD5 digest, returned as 16 raw bytes.

Synopsis#

use Digest::MD5 qw(md5);
my $bin = md5($data);              # 16 bytes
my $bin = md5($a, $b, $c);         # md5($a.$b.$c)

What you get back#

A 16-byte Perl string holding the raw MD5 digest. All arguments are concatenated (in order) and the result is hashed, so md5("a", "b", "c") is the same as md5("abc"). The function does not allocate an object; it runs through a stack-local context, which makes it the cheapest way to hash something you already hold in memory.

Use md5_hex or md5_base64 when you need a printable form.

Examples#

Digest a string to raw bytes:

use Digest::MD5 qw(md5);
my $bin = md5("hello world");      # 16 bytes
printf "%v02x\n", $bin;

## 5e.b6.3b.bb.e0.1e.ee.d0.93.cb.22.bb.8f.5a.cd.c3

Concatenation is implicit:

my $a = md5("foo", "bar");
my $b = md5("foobar");

## $a eq $b

Pack into a binary record:

my $blob = pack("N a16", length($payload), md5($payload));

Edge cases#

  • md5() with no arguments returns the MD5 of the empty string (d41d8cd98f00b204e9800998ecf8427e in hex).

  • Wide-character arguments (code points above U+00FF) croak with Wide character in subroutine entry. Encode first: md5(encode_utf8($str)).

  • Called as a class or instance method (e.g. Digest::MD5->md5(...)), it warns about probable misuse but still returns a digest based on all arguments, including the invocant. Prefer the plain function call.

Differences from upstream#

Fully compatible with upstream Digest::MD5 2.58.

See also#

  • md5_hex — same digest, as 32 lowercase hex characters

  • md5_base64 — same digest, base64-encoded

  • digest — OO form; reuse one context across many inputs

  • addfile — for data too large to hold in memory at once