1 ;;; md5.el --- MD5 message digest calculation (RFC 1321)
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
5 ;; Author: Dave Love <fx@gnu.org>
6 ;; Keywords: mail, processes, tools
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)
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.
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.
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.
30 ;; MD5 is defined in RFC 1321.
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.")
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.
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.
55 In this implementation, the program named by `md5-program' is run to
58 MD5 is defined in RFC 1321."
60 (let ((in-buffer (current-buffer))
61 (out-buffer (current-buffer)))
64 (setq in-buffer object
))
65 (goto-char (point-min))
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))
72 md5-program nil out-buffer
))
73 (error "Running MD5 command %s failed"
74 (cons md5-program md5-program-args
)))))
75 ;; The meaningful output is the first 32 characters.
76 ;; Don't return the newline that follows them!
77 (buffer-substring 1 33))))