annotate lisp/gnus/md5.el @ 114:8619ce7e4c50 r20-1b9

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