annotate lisp/w3/md5.el @ 93:486ff617c2a1

Added tag r20-0test1 for changeset 6a43545367ac
author cvs
date Mon, 13 Aug 2007 09:11:41 +0200
parents 0293115a14e9
children e04119814345
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
1 ;;; md5.el -- MD5 Message Digest Algorithm
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
2 ;;; Gareth Rees <gdr11@cl.cam.ac.uk>
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
3
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
4 ;; LCD Archive Entry:
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
5 ;; md5|Gareth Rees|gdr11@cl.cam.ac.uk|
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
6 ;; MD5 cryptographic message digest algorithm|
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
7 ;; 13-Nov-95|1.0|~/misc/md5.el.Z|
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
8
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
9 ;;; Details: ------------------------------------------------------------------
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
10
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
11 ;; This is a direct translation into Emacs LISP of the reference C
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
12 ;; implementation of the MD5 Message-Digest Algorithm written by RSA
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
13 ;; Data Security, Inc.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
14 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
15 ;; The algorithm takes a message (that is, a string of bytes) and
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
16 ;; computes a 16-byte checksum or "digest" for the message. This digest
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
17 ;; is supposed to be cryptographically strong in the sense that if you
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
18 ;; are given a 16-byte digest D, then there is no easier way to
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
19 ;; construct a message whose digest is D than to exhaustively search the
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
20 ;; space of messages. However, the robustness of the algorithm has not
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
21 ;; been proven, and a similar algorithm (MD4) was shown to be unsound,
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
22 ;; so treat with caution!
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
23 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
24 ;; The C algorithm uses 32-bit integers; because GNU Emacs
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
25 ;; implementations provide 28-bit integers (with 24-bit integers on
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
26 ;; versions prior to 19.29), the code represents a 32-bit integer as the
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
27 ;; cons of two 16-bit integers. The most significant word is stored in
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
28 ;; the car and the least significant in the cdr. The algorithm requires
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
29 ;; at least 17 bits of integer representation in order to represent the
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
30 ;; carry from a 16-bit addition.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
31
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
32 ;;; Usage: --------------------------------------------------------------------
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
33
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
34 ;; To compute the MD5 Message Digest for a message M (represented as a
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
35 ;; string or as a vector of bytes), call
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
36 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
37 ;; (md5-encode M)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
38 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
39 ;; which returns the message digest as a vector of 16 bytes. If you
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
40 ;; need to supply the message in pieces M1, M2, ... Mn, then call
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
41 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
42 ;; (md5-init)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
43 ;; (md5-update M1)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
44 ;; (md5-update M2)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
45 ;; ...
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
46 ;; (md5-update Mn)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
47 ;; (md5-final)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
48
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
49 ;;; Copyright and licence: ----------------------------------------------------
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
50
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 14
diff changeset
51 ;; Copyright (C) 1995, 1996, 1997 by Gareth Rees
14
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
52 ;; Derived from the RSA Data Security, Inc. MD5 Message-Digest Algorithm
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
53 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
54 ;; md5.el is free software; you can redistribute it and/or modify it
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
55 ;; under the terms of the GNU General Public License as published by the
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
56 ;; Free Software Foundation; either version 2, or (at your option) any
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
57 ;; later version.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
58 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
59 ;; md5.el is distributed in the hope that it will be useful, but WITHOUT
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
60 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
61 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
62 ;; for more details.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
63 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
64 ;; The original copyright notice is given below, as required by the
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
65 ;; licence for the original code. This code is distributed under *both*
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
66 ;; RSA's original licence and the GNU General Public Licence. (There
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
67 ;; should be no problems, as the former is more liberal than the
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
68 ;; latter).
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
69
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
70 ;;; Original copyright notice: ------------------------------------------------
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
71
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
72 ;; Copyright (C) 1990, RSA Data Security, Inc. All rights reserved.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
73 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
74 ;; License to copy and use this software is granted provided that it is
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
75 ;; identified as the "RSA Data Security, Inc. MD5 Message- Digest
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
76 ;; Algorithm" in all material mentioning or referencing this software or
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
77 ;; this function.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
78 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
79 ;; License is also granted to make and use derivative works provided
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
80 ;; that such works are identified as "derived from the RSA Data
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
81 ;; Security, Inc. MD5 Message-Digest Algorithm" in all material
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
82 ;; mentioning or referencing the derived work.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
83 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
84 ;; RSA Data Security, Inc. makes no representations concerning either
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
85 ;; the merchantability of this software or the suitability of this
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
86 ;; software for any particular purpose. It is provided "as is" without
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
87 ;; express or implied warranty of any kind.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
88 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
89 ;; These notices must be retained in any copies of any part of this
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
90 ;; documentation and/or software.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
91
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
92 ;;; Code: ---------------------------------------------------------------------
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
93
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
94 (defvar md5-program "md5"
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
95 "*Program that reads a message on its standard input and writes an
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
96 MD5 digest on its output.")
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
97
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
98 (defvar md5-maximum-internal-length 4096
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
99 "*The maximum size of a piece of data that should use the MD5 routines
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
100 written in lisp. If a message exceeds this, it will be run through an
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
101 external filter for processing. Also see the `md5-program' variable.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
102 This variable has no effect if you call the md5-init|update|final
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
103 functions - only used by the `md5' function's simpler interface.")
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
104
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
105 (defvar md5-bits (make-vector 4 0)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
106 "Number of bits handled, modulo 2^64.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
107 Represented as four 16-bit numbers, least significant first.")
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
108 (defvar md5-buffer (make-vector 4 '(0 . 0))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
109 "Scratch buffer (four 32-bit integers).")
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
110 (defvar md5-input (make-vector 64 0)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
111 "Input buffer (64 bytes).")
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
112
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
113 (defun md5-unhex (x)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
114 (if (> x ?9)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
115 (if (>= x ?a)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
116 (+ 10 (- x ?a))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
117 (+ 10 (- x ?A)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
118 (- x ?0)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
119
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
120 (defun md5-encode (message)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
121 "Encodes MESSAGE using the MD5 message digest algorithm.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
122 MESSAGE must be a string or an array of bytes.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
123 Returns a vector of 16 bytes containing the message digest."
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
124 (if (<= (length message) md5-maximum-internal-length)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
125 (progn
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
126 (md5-init)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
127 (md5-update message)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
128 (md5-final))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
129 (save-excursion
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
130 (set-buffer (get-buffer-create " *md5-work*"))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
131 (erase-buffer)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
132 (insert message)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
133 (call-process-region (point-min) (point-max)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
134 md5-program
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
135 t (current-buffer))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
136 ;; MD5 digest is 32 chars long
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
137 ;; mddriver adds a newline to make neaten output for tty
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
138 ;; viewing, make sure we leave it behind.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
139 (let ((data (buffer-substring (point-min) (+ (point-min) 32)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
140 (vec (make-vector 16 0))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
141 (ctr 0))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
142 (while (< ctr 16)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
143 (aset vec ctr (+ (* 16 (md5-unhex (aref data (* ctr 2))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
144 (md5-unhex (aref data (1+ (* ctr 2))))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
145 (setq ctr (1+ ctr)))))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
146
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
147 (defsubst md5-add (x y)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
148 "Return 32-bit sum of 32-bit integers X and Y."
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
149 (let ((m (+ (car x) (car y)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
150 (l (+ (cdr x) (cdr y))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
151 (cons (logand 65535 (+ m (lsh l -16))) (logand l 65535))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
152
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
153 ;; FF, GG, HH and II are basic MD5 functions, providing transformations
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
154 ;; for rounds 1, 2, 3 and 4 respectively. Each function follows this
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
155 ;; pattern of computation (where ROTATE(x,y) means rotate 32-bit value x
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
156 ;; by y bits to the left):
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
157 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
158 ;; FF(a,b,c,d,x,s,ac) = ROTATE(a + F(b,c,d) + x + ac,s) + b
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
159 ;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
160 ;; so we use the macro `md5-make-step' to construct each one. The
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
161 ;; helper functions F, G, H and I operate on 16-bit numbers; the full
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
162 ;; operation splits its inputs, operates on the halves separately and
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
163 ;; then puts the results together.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
164
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
165 (defsubst md5-F (x y z) (logior (logand x y) (logand (lognot x) z)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
166 (defsubst md5-G (x y z) (logior (logand x z) (logand y (lognot z))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
167 (defsubst md5-H (x y z) (logxor x y z))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
168 (defsubst md5-I (x y z) (logxor y (logior x (logand 65535 (lognot z)))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
169
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
170 (defmacro md5-make-step (name func)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
171 (`
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
172 (defun (, name) (a b c d x s ac)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
173 (let*
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
174 ((m1 (+ (car a) ((, func) (car b) (car c) (car d)) (car x) (car ac)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
175 (l1 (+ (cdr a) ((, func) (cdr b) (cdr c) (cdr d)) (cdr x) (cdr ac)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
176 (m2 (logand 65535 (+ m1 (lsh l1 -16))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
177 (l2 (logand 65535 l1))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
178 (m3 (logand 65535 (if (> s 15)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
179 (+ (lsh m2 (- s 32)) (lsh l2 (- s 16)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
180 (+ (lsh m2 s) (lsh l2 (- s 16))))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
181 (l3 (logand 65535 (if (> s 15)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
182 (+ (lsh l2 (- s 32)) (lsh m2 (- s 16)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
183 (+ (lsh l2 s) (lsh m2 (- s 16)))))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
184 (md5-add (cons m3 l3) b)))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
185
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
186 (md5-make-step md5-FF md5-F)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
187 (md5-make-step md5-GG md5-G)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
188 (md5-make-step md5-HH md5-H)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
189 (md5-make-step md5-II md5-I)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
190
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
191 (defun md5-init ()
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
192 "Initialise the state of the message-digest routines."
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
193 (aset md5-bits 0 0)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
194 (aset md5-bits 1 0)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
195 (aset md5-bits 2 0)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
196 (aset md5-bits 3 0)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
197 (aset md5-buffer 0 '(26437 . 8961))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
198 (aset md5-buffer 1 '(61389 . 43913))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
199 (aset md5-buffer 2 '(39098 . 56574))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
200 (aset md5-buffer 3 '( 4146 . 21622)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
201
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
202 (defun md5-update (string)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
203 "Update the current MD5 state with STRING (an array of bytes)."
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
204 (let ((len (length string))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
205 (i 0)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
206 (j 0))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
207 (while (< i len)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
208 ;; Compute number of bytes modulo 64
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
209 (setq j (% (/ (aref md5-bits 0) 8) 64))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
210
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
211 ;; Store this byte (truncating to 8 bits to be sure)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
212 (aset md5-input j (logand 255 (aref string i)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
213
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
214 ;; Update number of bits by 8 (modulo 2^64)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
215 (let ((c 8) (k 0))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
216 (while (and (> c 0) (< k 4))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
217 (let ((b (aref md5-bits k)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
218 (aset md5-bits k (logand 65535 (+ b c)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
219 (setq c (if (> b (- 65535 c)) 1 0)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
220 k (1+ k)))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
221
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
222 ;; Increment number of bytes processed
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
223 (setq i (1+ i))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
224
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
225 ;; When 64 bytes accumulated, pack them into sixteen 32-bit
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
226 ;; integers in the array `in' and then tranform them.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
227 (if (= j 63)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
228 (let ((in (make-vector 16 (cons 0 0)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
229 (k 0)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
230 (kk 0))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
231 (while (< k 16)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
232 (aset in k (md5-pack md5-input kk))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
233 (setq k (+ k 1) kk (+ kk 4)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
234 (md5-transform in))))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
235
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
236 (defun md5-pack (array i)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
237 "Pack the four bytes at ARRAY reference I to I+3 into a 32-bit integer."
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
238 (cons (+ (lsh (aref array (+ i 3)) 8) (aref array (+ i 2)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
239 (+ (lsh (aref array (+ i 1)) 8) (aref array (+ i 0)))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
240
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
241 (defun md5-byte (array n b)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
242 "Unpack byte B (0 to 3) from Nth member of ARRAY of 32-bit integers."
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
243 (let ((e (aref array n)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
244 (cond ((eq b 0) (logand 255 (cdr e)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
245 ((eq b 1) (lsh (cdr e) -8))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
246 ((eq b 2) (logand 255 (car e)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
247 ((eq b 3) (lsh (car e) -8)))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
248
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
249 (defun md5-final ()
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
250 (let ((in (make-vector 16 (cons 0 0)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
251 (j 0)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
252 (digest (make-vector 16 0))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
253 (padding))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
254
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
255 ;; Save the number of bits in the message
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
256 (aset in 14 (cons (aref md5-bits 1) (aref md5-bits 0)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
257 (aset in 15 (cons (aref md5-bits 3) (aref md5-bits 2)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
258
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
259 ;; Compute number of bytes modulo 64
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
260 (setq j (% (/ (aref md5-bits 0) 8) 64))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
261
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
262 ;; Pad out computation to 56 bytes modulo 64
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
263 (setq padding (make-vector (if (< j 56) (- 56 j) (- 120 j)) 0))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
264 (aset padding 0 128)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
265 (md5-update padding)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
266
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
267 ;; Append length in bits and transform
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
268 (let ((k 0) (kk 0))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
269 (while (< k 14)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
270 (aset in k (md5-pack md5-input kk))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
271 (setq k (+ k 1) kk (+ kk 4))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
272 (md5-transform in)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
273
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
274 ;; Store the results in the digest
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
275 (let ((k 0) (kk 0))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
276 (while (< k 4)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
277 (aset digest (+ kk 0) (md5-byte md5-buffer k 0))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
278 (aset digest (+ kk 1) (md5-byte md5-buffer k 1))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
279 (aset digest (+ kk 2) (md5-byte md5-buffer k 2))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
280 (aset digest (+ kk 3) (md5-byte md5-buffer k 3))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
281 (setq k (+ k 1) kk (+ kk 4))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
282
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
283 ;; Return digest
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
284 digest))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
285
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
286 ;; It says in the RSA source, "Note that if the Mysterious Constants are
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
287 ;; arranged backwards in little-endian order and decrypted with the DES
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
288 ;; they produce OCCULT MESSAGES!" Security through obscurity?
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
289
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
290 (defun md5-transform (in)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
291 "Basic MD5 step. Transform md5-buffer based on array IN."
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
292 (let ((a (aref md5-buffer 0))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
293 (b (aref md5-buffer 1))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
294 (c (aref md5-buffer 2))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
295 (d (aref md5-buffer 3)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
296 (setq
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
297 a (md5-FF a b c d (aref in 0) 7 '(55146 . 42104))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
298 d (md5-FF d a b c (aref in 1) 12 '(59591 . 46934))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
299 c (md5-FF c d a b (aref in 2) 17 '( 9248 . 28891))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
300 b (md5-FF b c d a (aref in 3) 22 '(49597 . 52974))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
301 a (md5-FF a b c d (aref in 4) 7 '(62844 . 4015))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
302 d (md5-FF d a b c (aref in 5) 12 '(18311 . 50730))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
303 c (md5-FF c d a b (aref in 6) 17 '(43056 . 17939))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
304 b (md5-FF b c d a (aref in 7) 22 '(64838 . 38145))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
305 a (md5-FF a b c d (aref in 8) 7 '(27008 . 39128))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
306 d (md5-FF d a b c (aref in 9) 12 '(35652 . 63407))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
307 c (md5-FF c d a b (aref in 10) 17 '(65535 . 23473))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
308 b (md5-FF b c d a (aref in 11) 22 '(35164 . 55230))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
309 a (md5-FF a b c d (aref in 12) 7 '(27536 . 4386))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
310 d (md5-FF d a b c (aref in 13) 12 '(64920 . 29075))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
311 c (md5-FF c d a b (aref in 14) 17 '(42617 . 17294))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
312 b (md5-FF b c d a (aref in 15) 22 '(18868 . 2081))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
313 a (md5-GG a b c d (aref in 1) 5 '(63006 . 9570))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
314 d (md5-GG d a b c (aref in 6) 9 '(49216 . 45888))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
315 c (md5-GG c d a b (aref in 11) 14 '( 9822 . 23121))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
316 b (md5-GG b c d a (aref in 0) 20 '(59830 . 51114))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
317 a (md5-GG a b c d (aref in 5) 5 '(54831 . 4189))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
318 d (md5-GG d a b c (aref in 10) 9 '( 580 . 5203))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
319 c (md5-GG c d a b (aref in 15) 14 '(55457 . 59009))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
320 b (md5-GG b c d a (aref in 4) 20 '(59347 . 64456))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
321 a (md5-GG a b c d (aref in 9) 5 '( 8673 . 52710))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
322 d (md5-GG d a b c (aref in 14) 9 '(49975 . 2006))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
323 c (md5-GG c d a b (aref in 3) 14 '(62677 . 3463))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
324 b (md5-GG b c d a (aref in 8) 20 '(17754 . 5357))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
325 a (md5-GG a b c d (aref in 13) 5 '(43491 . 59653))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
326 d (md5-GG d a b c (aref in 2) 9 '(64751 . 41976))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
327 c (md5-GG c d a b (aref in 7) 14 '(26479 . 729))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
328 b (md5-GG b c d a (aref in 12) 20 '(36138 . 19594))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
329 a (md5-HH a b c d (aref in 5) 4 '(65530 . 14658))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
330 d (md5-HH d a b c (aref in 8) 11 '(34673 . 63105))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
331 c (md5-HH c d a b (aref in 11) 16 '(28061 . 24866))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
332 b (md5-HH b c d a (aref in 14) 23 '(64997 . 14348))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
333 a (md5-HH a b c d (aref in 1) 4 '(42174 . 59972))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
334 d (md5-HH d a b c (aref in 4) 11 '(19422 . 53161))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
335 c (md5-HH c d a b (aref in 7) 16 '(63163 . 19296))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
336 b (md5-HH b c d a (aref in 10) 23 '(48831 . 48240))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
337 a (md5-HH a b c d (aref in 13) 4 '(10395 . 32454))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
338 d (md5-HH d a b c (aref in 0) 11 '(60065 . 10234))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
339 c (md5-HH c d a b (aref in 3) 16 '(54511 . 12421))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
340 b (md5-HH b c d a (aref in 6) 23 '( 1160 . 7429))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
341 a (md5-HH a b c d (aref in 9) 4 '(55764 . 53305))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
342 d (md5-HH d a b c (aref in 12) 11 '(59099 . 39397))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
343 c (md5-HH c d a b (aref in 15) 16 '( 8098 . 31992))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
344 b (md5-HH b c d a (aref in 2) 23 '(50348 . 22117))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
345 a (md5-II a b c d (aref in 0) 6 '(62505 . 8772))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
346 d (md5-II d a b c (aref in 7) 10 '(17194 . 65431))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
347 c (md5-II c d a b (aref in 14) 15 '(43924 . 9127))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
348 b (md5-II b c d a (aref in 5) 21 '(64659 . 41017))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
349 a (md5-II a b c d (aref in 12) 6 '(25947 . 22979))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
350 d (md5-II d a b c (aref in 3) 10 '(36620 . 52370))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
351 c (md5-II c d a b (aref in 10) 15 '(65519 . 62589))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
352 b (md5-II b c d a (aref in 1) 21 '(34180 . 24017))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
353 a (md5-II a b c d (aref in 8) 6 '(28584 . 32335))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
354 d (md5-II d a b c (aref in 15) 10 '(65068 . 59104))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
355 c (md5-II c d a b (aref in 6) 15 '(41729 . 17172))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
356 b (md5-II b c d a (aref in 13) 21 '(19976 . 4513))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
357 a (md5-II a b c d (aref in 4) 6 '(63315 . 32386))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
358 d (md5-II d a b c (aref in 11) 10 '(48442 . 62005))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
359 c (md5-II c d a b (aref in 2) 15 '(10967 . 53947))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
360 b (md5-II b c d a (aref in 9) 21 '(60294 . 54161)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
361
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
362 (aset md5-buffer 0 (md5-add (aref md5-buffer 0) a))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
363 (aset md5-buffer 1 (md5-add (aref md5-buffer 1) b))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
364 (aset md5-buffer 2 (md5-add (aref md5-buffer 2) c))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
365 (aset md5-buffer 3 (md5-add (aref md5-buffer 3) d))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
366
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
367 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
368 ;;; Here begins the merger with the XEmacs API and the md5.el from the URL
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
369 ;;; package. Courtesy wmperry@cs.indiana.edu
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
370 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
371 (defun md5 (object &optional start end)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
372 "Return the MD5 (a secure message digest algorithm) of an object.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
373 OBJECT is either a string or a buffer.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
374 Optional arguments START and END denote buffer positions for computing the
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
375 hash of a portion of OBJECT."
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
376 (let ((buffer nil))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
377 (unwind-protect
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
378 (save-excursion
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
379 (setq buffer (generate-new-buffer " *md5-work*"))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
380 (set-buffer buffer)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
381 (cond
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
382 ((bufferp object)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
383 (insert-buffer-substring object start end))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
384 ((stringp object)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
385 (insert (if (or start end)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
386 (substring object start end)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
387 object)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
388 (t nil))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
389 (prog1
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
390 (if (<= (point-max) md5-maximum-internal-length)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
391 (mapconcat
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
392 (function (lambda (node) (format "%02x" node)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
393 (md5-encode (buffer-string))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
394 "")
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
395 (call-process-region (point-min) (point-max)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
396 (or shell-file-name "/bin/sh")
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
397 t buffer nil
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
398 "-c" md5-program)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
399 ;; MD5 digest is 32 chars long
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
400 ;; mddriver adds a newline to make neaten output for tty
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
401 ;; viewing, make sure we leave it behind.
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
402 (buffer-substring (point-min) (+ (point-min) 32)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
403 (kill-buffer buffer)))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
404 (and buffer (kill-buffer buffer) nil))))
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
405
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
406 (provide 'md5)
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
407
9ee227acff29 Import from CVS: tag r19-15b90
cvs
parents:
diff changeset
408 ;;; md5.el ends here ----------------------------------------------------------