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