1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<h3 class="section">Checksum/Hash</h3> <p>Emacs has built-in support for computing <em>cryptographic hashes</em>. A cryptographic hash, or <em>checksum</em>, 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. </p> <p>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 <em>message digests</em> to check the integrity of messages transmitted over a network. MD5 and SHA-1 are not collision resistant (i.e., it is possible to deliberately design different pieces of data which have the same MD5 or SHA-1 hash), so you should not use them for anything security-related. For security-related applications you should use the other hash types, such as SHA-2 (e.g. <code>sha256</code> or <code>sha512</code>). </p> <dl> <dt id="secure-hash-algorithms">Function: <strong>secure-hash-algorithms</strong>
</dt> <dd><p>This function returns a list of symbols representing algorithms that <code>secure-hash</code> can use. </p></dd>
</dl> <dl> <dt id="secure-hash">Function: <strong>secure-hash</strong> <em>algorithm object &optional start end binary</em>
</dt> <dd>
<p>This function returns a hash for <var>object</var>. The argument <var>algorithm</var> is a symbol stating which hash to compute: one of <code>md5</code>, <code>sha1</code>, <code>sha224</code>, <code>sha256</code>, <code>sha384</code> or <code>sha512</code>. The argument <var>object</var> should be a buffer or a string. </p> <p>The optional arguments <var>start</var> and <var>end</var> are character positions specifying the portion of <var>object</var> to compute the message digest for. If they are <code>nil</code> or omitted, the hash is computed for the whole of <var>object</var>. </p> <p>If the argument <var>binary</var> is omitted or <code>nil</code>, the function returns the <em>text form</em> of the hash, as an ordinary Lisp string. If <var>binary</var> is non-<code>nil</code>, it returns the hash in <em>binary form</em>, as a sequence of bytes stored in a unibyte string. </p> <p>This function does not compute the hash directly from the internal representation of <var>object</var>’s text (see <a href="text-representations">Text Representations</a>). Instead, it encodes the text using a coding system (see <a href="coding-systems">Coding Systems</a>), and computes the hash from that encoded text. If <var>object</var> is a buffer, the coding system used is the one which would be chosen by default for writing the text into a file. If <var>object</var> is a string, the user’s preferred coding system is used (see <a href="https://www.gnu.org/software/emacs/manual/html_node/emacs/Recognize-Coding.html#Recognize-Coding">Recognize Coding</a> in <cite>GNU Emacs Manual</cite>). </p>
</dd>
</dl> <dl> <dt id="md5">Function: <strong>md5</strong> <em>object &optional start end coding-system noerror</em>
</dt> <dd>
<p>This function returns an MD5 hash. It is semi-obsolete, since for most purposes it is equivalent to calling <code>secure-hash</code> with <code>md5</code> as the <var>algorithm</var> argument. The <var>object</var>, <var>start</var> and <var>end</var> arguments have the same meanings as in <code>secure-hash</code>. </p> <p>If <var>coding-system</var> is non-<code>nil</code>, it specifies a coding system to use to encode the text; if omitted or <code>nil</code>, the default coding system is used, like in <code>secure-hash</code>. </p> <p>Normally, <code>md5</code> signals an error if the text can’t be encoded using the specified or chosen coding system. However, if <var>noerror</var> is non-<code>nil</code>, it silently uses <code>raw-text</code> coding instead. </p>
</dd>
</dl> <dl> <dt id="buffer-hash">Function: <strong>buffer-hash</strong> <em>&optional buffer-or-name</em>
</dt> <dd><p>Return a hash of <var>buffer-or-name</var>. If <code>nil</code>, this defaults to the current buffer. As opposed to <code>secure-hash</code>, this function computes the hash based on the internal representation of the buffer, disregarding any coding systems. It’s therefore only useful when comparing two buffers running in the same Emacs, and is not guaranteed to return the same hash between different Emacs versions. It should be somewhat more efficient on larger buffers than <code>secure-hash</code> is, and should not allocate more memory. </p></dd>
</dl><div class="_attribution">
<p class="_attribution-p">
Copyright © 1990-1996, 1998-2022 Free Software Foundation, Inc. <br>Licensed under the GNU GPL license.<br>
<a href="https://www.gnu.org/software/emacs/manual/html_node/elisp/Checksum_002fHash.html" class="_attribution-link">https://www.gnu.org/software/emacs/manual/html_node/elisp/Checksum_002fHash.html</a>
</p>
</div>
|