elisp: Checksum/Hash
31.25 Checksum/Hash
===================
Emacs has built-in support for computing “cryptographic hashes”. A
cryptographic hash, or “checksum”, is a digital fingerprint of a piece
of data (e.g., a block of text) which can be used to check that you have
an unaltered copy of that data.
Emacs supports several common cryptographic hash algorithms: MD5,
SHA-1, SHA-2, SHA-224, SHA-256, SHA-384 and SHA-512. MD5 is the oldest
of these algorithms, and is commonly used in “message digests” to check
the integrity of messages transmitted over a network. MD5 is not
collision resistant (i.e., it is possible to deliberately design
different pieces of data which have the same MD5 hash), so you should
not used it for anything security-related. A similar theoretical
weakness also exists in SHA-1. Therefore, for security-related
applications you should use the other hash types, such as SHA-2.
-- Function: secure-hash algorithm object &optional start end binary
This function returns a hash for OBJECT. The argument ALGORITHM is
a symbol stating which hash to compute: one of ‘md5’, ‘sha1’,
‘sha224’, ‘sha256’, ‘sha384’ or ‘sha512’. The argument OBJECT
should be a buffer or a string.
The optional arguments START and END are character positions
specifying the portion of OBJECT to compute the message digest for.
If they are ‘nil’ or omitted, the hash is computed for the whole of
OBJECT.
If the argument BINARY is omitted or ‘nil’, the function returns
the “text form” of the hash, as an ordinary Lisp string. If BINARY
is non-‘nil’, it returns the hash in “binary form”, as a sequence
of bytes stored in a unibyte string.
This function does not compute the hash directly from the internal
representation of OBJECT’s text (Text Representations).
Instead, it encodes the text using a coding system (Coding
Systems), and computes the hash from that encoded text. If
OBJECT is a buffer, the coding system used is the one which would
be chosen by default for writing the text into a file. If OBJECT
is a string, the user’s preferred coding system is used (
(emacs)Recognize Coding).
-- Function: md5 object &optional start end coding-system noerror
This function returns an MD5 hash. It is semi-obsolete, since for
most purposes it is equivalent to calling ‘secure-hash’ with ‘md5’
as the ALGORITHM argument. The OBJECT, START and END arguments
have the same meanings as in ‘secure-hash’.
If CODING-SYSTEM is non-‘nil’, it specifies a coding system to use
to encode the text; if omitted or ‘nil’, the default coding system
is used, like in ‘secure-hash’.
Normally, ‘md5’ signals an error if the text can’t be encoded using
the specified or chosen coding system. However, if NOERROR is
non-‘nil’, it silently uses ‘raw-text’ coding instead.