Function: buffer-hash

buffer-hash is a function defined in fns.c.

Signature

(buffer-hash &optional BUFFER-OR-NAME)

Documentation

Return a hash of the contents of BUFFER-OR-NAME.

This hash is performed on the raw internal format of the buffer, disregarding any coding systems. If nil, use the current buffer.

This function is useful for comparing two buffers running in the same Emacs, but is not guaranteed to return the same hash between different Emacs versions. It should be somewhat more efficient on larger buffers than secure-hash is, and should not allocate more memory.

It should not be used for anything security-related. See secure-hash for these applications.

View in manual

Probably introduced at or before Emacs version 26.1.

Aliases

org-buffer-hash

Source Code

// Defined in /usr/src/emacs/src/fns.c
{
  Lisp_Object buffer;
  struct buffer *b;
  struct sha1_ctx ctx;

  if (NILP (buffer_or_name))
    buffer = Fcurrent_buffer ();
  else
    buffer = Fget_buffer (buffer_or_name);
  if (NILP (buffer))
    nsberror (buffer_or_name);

  b = XBUFFER (buffer);
  sha1_init_ctx (&ctx);

  /* Process the first part of the buffer. */
  sha1_process_bytes (BUF_BEG_ADDR (b),
		      BUF_GPT_BYTE (b) - BUF_BEG_BYTE (b),
		      &ctx);

  /* If the gap is before the end of the buffer, process the last half
     of the buffer. */
  if (BUF_GPT_BYTE (b) < BUF_Z_BYTE (b))
    sha1_process_bytes (BUF_GAP_END_ADDR (b),
			BUF_Z_ADDR (b) - BUF_GAP_END_ADDR (b),
			&ctx);

  Lisp_Object digest = make_uninit_string (SHA1_DIGEST_SIZE * 2);
  sha1_finish_ctx (&ctx, SSDATA (digest));
  return make_digest_string (digest, SHA1_DIGEST_SIZE);
}