add_bits#

Add a bit-oriented quantum of data — but only in multiples of 8 bits.

Synopsis#

$ctx->add_bits($bitstring);        # "01101000..." form
$ctx->add_bits($data, $nbits);     # byte-string + bit count

What you get back#

The same context object, for chaining. MD5 is a byte-oriented algorithm, so add_bits exists purely for interface compatibility with other Digest::* modules that do hash at bit granularity. It accepts exactly the same two calling forms they do, but requires the bit count to be a whole multiple of 8 and croaks otherwise.

Unless you are writing code that must work across multiple digest backends, use add directly — it is clearer and has no restrictions.

Examples#

Bit-string form — a string of '0' and '1' characters:

$ctx->add_bits("0110100001101001");   # feeds bytes "hi"

Byte-string + bit count — takes the first $nbits / 8 bytes:

$ctx->add_bits("hello world", 40);    # feeds "hello"

Chain alongside add:

my $hex = Digest::MD5->new
                      ->add_bits("0110100001101001")   # "hi"
                      ->add(" there")
                      ->hexdigest;

Edge cases#

  • Croaks with "Number of bits must be multiple of 8" when the bit string’s length is not a multiple of 8.

  • Croaks with "Number of bits must be multiple of 8 in add_bits" when the explicit $nbits argument is not a multiple of 8.

  • In the two-argument form, $nbits larger than 8 * length($data) silently feeds nothing — the call is a no-op.

Differences from upstream#

Fully compatible with upstream Digest::MD5 2.58.

See also#

  • add — the direct byte-level input method

  • addfile — stream bytes from a file handle

  • new — typical predecessor in a chain

  • hexdigest — typical successor in a chain