annotate lisp/md5.el @ 33547:1a45cc116462

#
author André Spiegel <spiegel@gnu.org>
date Thu, 16 Nov 2000 13:55:30 +0000
parents 7203e43ca59f
children 158687c6a102
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
33370
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
1 ;;; md5.el --- MD5 message digest calculation (RFC 1321)
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
2
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
4
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
5 ;; Author: Dave Love <fx@gnu.org>
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
6 ;; Keywords: mail, processes, tools
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
7
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
8 ;; This file is free software; you can redistribute it and/or modify
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
9 ;; it under the terms of the GNU General Public License as published by
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
10 ;; the Free Software Foundation; either version 2, or (at your option)
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
11 ;; any later version.
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
12
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
13 ;; This file is distributed in the hope that it will be useful,
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
16 ;; GNU General Public License for more details.
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
17
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
18 ;; You should have received a copy of the GNU General Public License
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
20 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
21 ;; Boston, MA 02111-1307, USA.
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
22
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
23 ;;; Commentary:
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
24
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
25 ;; Provides the `md5' function for computing the MD5 `message
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
26 ;; digest'/`fingerprint'/`checksum' for a string or buffer. This is
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
27 ;; compatible with the XEmacs version. We expect to have primitive
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
28 ;; MD5 support in a future version of Emacs.
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
29
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
30 ;; MD5 is defined in RFC 1321.
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
31
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
32 ;;; Code:
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
33
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
34 ;; Not worth customizing? Will go away anyhow with primitive support.
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
35 (defvar md5-program "md5sum"
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
36 "Name of a program to calculate MD5 (message digest) checksums.
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
37 This should read standard input and output the MD5 checksum in the
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
38 first 32 bytes of standard output. `md5sum' is in GNU Textutils. An
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
39 alternative is `md5', present in the SSLeay distribution.")
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
40
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
41 ;;;###autoload
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
42 (defun md5 (object &optional start end encoding noerror)
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
43 "Return the MD5 message digest (checksum or fingerprint) of OBJECT.
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
44 OBJECT is a buffer or a atring. Optional arguments START and END
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
45 specify a region of the object to use, where the first character is 1
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
46 for both buffers and strings.
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
47
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
48 Optional argument ENCODING specifies a coding system with which to
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
49 encode the text for computing the digest. If omitted, the normal
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
50 rules will be used to find a coding system for output to
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
51 `md5-program'. It probably makes most sense to use unibyte data and
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
52 `binary' encoding. Optional argument NOERROR is for XEmacs
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
53 compatibility and is ignored.
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
54
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
55 In this implementation, the program named by `md5-program' is run to
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
56 do the calculation.
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
57
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
58 MD5 is defined in RFC 1321."
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
59 (with-temp-buffer
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
60 (let ((in-buffer (current-buffer))
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
61 (out-buffer (current-buffer)))
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
62 (if (stringp object)
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
63 (insert object)
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
64 (setq in-buffer object))
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
65 (goto-char (point-min))
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
66 (unless encoding
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
67 (setq encoding coding-system-for-write))
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
68 (with-current-buffer in-buffer
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
69 (let ((coding-system-for-write encoding))
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
70 (unless (eq 0 (call-process-region (or start (point-min))
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
71 (or end (point-max))
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
72 md5-program nil out-buffer))
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
73 (error "Running MD5 command %s failed"
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
74 (cons md5-program md5-program-args)))))
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
75 ;; The meaningful output is the first 32 characters.
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
76 ;; Don't return the newline that follows them!
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
77 (buffer-substring 1 33))))
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
78
7203e43ca59f *** empty log message ***
Dave Love <fx@gnu.org>
parents:
diff changeset
79 ;;; md5.el ends here