1 ;;;; This file implements The MD5 Message-Digest Algorithm, as defined in
2 ;;;; RFC 1321 by R. Rivest, published April 1992.
4 ;;;; It was written by Pierre R. Mai, with copious input from the
5 ;;;; cmucl-help mailing-list hosted at cons.org, in November 2001 and
6 ;;;; has been placed into the public domain.
8 ;;;; While the implementation should work on all conforming Common
9 ;;;; Lisp implementations, it has only been optimized for CMU CL,
10 ;;;; where it achieved comparable performance to the standard md5sum
11 ;;;; utility (within a factor of 1.5 or less on iA32 and UltraSparc
14 ;;;; Since the implementation makes heavy use of arithmetic on
15 ;;;; (unsigned-byte 32) numbers, acceptable performance is likely only
16 ;;;; on CL implementations that support unboxed arithmetic on such
17 ;;;; numbers in some form. For other CL implementations a 16bit
18 ;;;; implementation of MD5 is probably more suitable.
20 ;;;; The code implements correct operation for files of unbounded size
21 ;;;; as is, at the cost of having to do a single generic integer
22 ;;;; addition for each call to update-md5-state. If you call
23 ;;;; update-md5-state frequently with little data, this can pose a
24 ;;;; performance problem. If you can live with a size restriction of
25 ;;;; 512 MB, then you can enable fast fixnum arithmetic by putting
26 ;;;; :md5-small-length onto *features* prior to compiling this file.
28 ;;;; Testing code can be compiled by including :md5-testing on
29 ;;;; *features* prior to compilation. In that case evaluating
30 ;;;; (md5::test-rfc1321) will run all the test-cases present in
31 ;;;; Appendix A.5 of RFC 1321 and report on the results.
32 ;;;; Evaluating (md5::test-other) will run further test-cases
33 ;;;; gathered by the author to cover regressions, etc.
35 ;;;; This software is "as is", and has no warranty of any kind. The
36 ;;;; authors assume no responsibility for the consequences of any use
37 ;;;; of this software.
39 (defpackage #:md5
(:use
#:cl
)
41 ;; Low-Level types and functions
42 #:md5-regs
#:initial-md5-regs
#:md5regs-digest
43 #:update-md5-block
#:fill-block
#:fill-block-ub8
#:fill-block-char
44 ;; Mid-Level types and functions
45 #:md5-state
#:md5-state-p
#:make-md5-state
46 #:update-md5-state
#:finalize-md5-state
47 ;; High-Level functions on sequences, streams and files
48 #:md5sum-sequence
#:md5sum-stream
#:md5sum-file
49 ;; Very High level functions
55 (eval-when (:compile-toplevel
)
56 (defparameter *old-expansion-limit
* ext
:*inline-expansion-limit
*)
57 (setq ext
:*inline-expansion-limit
* (max ext
:*inline-expansion-limit
* 1000)))
60 (eval-when (:compile-toplevel
:execute
)
61 (defparameter *old-features
* *features
*)
62 (pushnew (c:backend-byte-order c
:*target-backend
*) *features
*))
64 ;;; Section 2: Basic Datatypes
67 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
69 "Corresponds to the 32bit quantity word of the MD5 Spec"
74 "Corresponds to the 32bit quantity word of the MD5 Spec"
77 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
78 (defmacro assemble-ub32
(a b c d
)
79 "Assemble an ub32 value from the given (unsigned-byte 8) values,
80 where a is the intended low-order byte and d the high-order byte."
81 `(the ub32
(logior (ash ,d
24) (ash ,c
16) (ash ,b
8) ,a
))))
83 ;;; Section 3.4: Auxilliary functions
85 (declaim (inline f g h i
)
86 (ftype (function (ub32 ub32 ub32
) ub32
) f g h i
))
89 (declare (type ub32 x y z
)
90 (optimize (speed 3) (safety 0) (space 0) (debug 0)))
92 (kernel:32bit-logical-or
(kernel:32bit-logical-and x y
)
93 (kernel:32bit-logical-andc1 x z
))
95 (logior (logand x y
) (logandc1 x z
)))
98 (declare (type ub32 x y z
)
99 (optimize (speed 3) (safety 0) (space 0) (debug 0)))
101 (kernel:32bit-logical-or
(kernel:32bit-logical-and x z
)
102 (kernel:32bit-logical-andc2 y z
))
104 (logior (logand x z
) (logandc2 y z
)))
107 (declare (type ub32 x y z
)
108 (optimize (speed 3) (safety 0) (space 0) (debug 0)))
110 (kernel:32bit-logical-xor x
(kernel:32bit-logical-xor y z
))
115 (declare (type ub32 x y z
)
116 (optimize (speed 3) (safety 0) (space 0) (debug 0)))
118 (kernel:32bit-logical-xor y
(kernel:32bit-logical-orc2 x z
))
120 (ldb (byte 32 0) (logxor y
(logorc2 x z
))))
122 (declaim (inline mod32
+)
123 (ftype (function (ub32 ub32
) ub32
) mod32
+))
125 (declare (type ub32 a b
) (optimize (speed 3) (safety 0) (space 0) (debug 0)))
126 (ldb (byte 32 0) (+ a b
)))
129 (define-compiler-macro mod32
+ (a b
)
130 `(ext:truly-the ub32
(+ ,a
,b
)))
132 (declaim (inline rol32
)
133 (ftype (function (ub32 (unsigned-byte 5)) ub32
) rol32
))
135 (declare (type ub32 a
) (type (unsigned-byte 5) s
)
136 (optimize (speed 3) (safety 0) (space 0) (debug 0)))
138 (kernel:32bit-logical-or
#+little-endian
(kernel:shift-towards-end a s
)
139 #+big-endian
(kernel:shift-towards-start a s
)
142 (logior (ldb (byte 32 0) (ash a s
)) (ash a
(- s
32))))
144 ;;; Section 3.4: Table T
146 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
147 (defparameter *t
* (make-array 64 :element-type
'ub32
149 (loop for i from
1 to
64
153 (abs (sin (float i
0.0d0
)))))))))
155 ;;; Section 3.4: Helper Macro for single round definitions
157 (defmacro with-md5-round
((op block
) &rest clauses
)
158 (loop for
(a b c d k s i
) in clauses
160 `(setq ,a
(mod32+ ,b
(rol32 (mod32+ (mod32+ ,a
(,op
,b
,c
,d
))
161 (mod32+ (aref ,block
,k
)
166 (return `(progn ,@result
))))
168 ;;; Section 3.3: (Initial) MD5 Working Set
171 "The working state of the MD5 algorithm, which contains the 4 32-bit
172 registers A, B, C and D."
173 `(simple-array (unsigned-byte 32) (4)))
175 (defmacro md5-regs-a
(regs)
178 (defmacro md5-regs-b
(regs)
181 (defmacro md5-regs-c
(regs)
184 (defmacro md5-regs-d
(regs)
187 (defconstant +md5-magic-a
+ (assemble-ub32 #x01
#x23
#x45
#x67
)
188 "Initial value of Register A of the MD5 working state.")
189 (defconstant +md5-magic-b
+ (assemble-ub32 #x89
#xab
#xcd
#xef
)
190 "Initial value of Register B of the MD5 working state.")
191 (defconstant +md5-magic-c
+ (assemble-ub32 #xfe
#xdc
#xba
#x98
)
192 "Initial value of Register C of the MD5 working state.")
193 (defconstant +md5-magic-d
+ (assemble-ub32 #x76
#x54
#x32
#x10
)
194 "Initial value of Register D of the MD5 working state.")
196 (declaim (inline initial-md5-regs
))
197 (defun initial-md5-regs ()
198 "Create the initial working state of an MD5 run."
199 (declare (optimize (speed 3) (safety 0) (space 0) (debug 0)))
200 (let ((regs (make-array 4 :element-type
'(unsigned-byte 32))))
201 (declare (type md5-regs regs
))
202 (setf (md5-regs-a regs
) +md5-magic-a
+
203 (md5-regs-b regs
) +md5-magic-b
+
204 (md5-regs-c regs
) +md5-magic-c
+
205 (md5-regs-d regs
) +md5-magic-d
+)
208 ;;; Section 3.4: Operation on 16-Word Blocks
210 (defun update-md5-block (regs block
)
211 "This is the core part of the MD5 algorithm. It takes a complete 16
212 word block of input, and updates the working state in A, B, C, and D
214 (declare (type md5-regs regs
)
215 (type (simple-array ub32
(16)) block
)
216 (optimize (speed 3) (safety 0) (space 0) (debug 0)))
217 (let ((A (md5-regs-a regs
)) (B (md5-regs-b regs
))
218 (C (md5-regs-c regs
)) (D (md5-regs-d regs
)))
219 (declare (type ub32 A B C D
))
221 (with-md5-round (f block
)
222 (A B C D
0 7 1)(D A B C
1 12 2)(C D A B
2 17 3)(B C D A
3 22 4)
223 (A B C D
4 7 5)(D A B C
5 12 6)(C D A B
6 17 7)(B C D A
7 22 8)
224 (A B C D
8 7 9)(D A B C
9 12 10)(C D A B
10 17 11)(B C D A
11 22 12)
225 (A B C D
12 7 13)(D A B C
13 12 14)(C D A B
14 17 15)(B C D A
15 22 16))
227 (with-md5-round (g block
)
228 (A B C D
1 5 17)(D A B C
6 9 18)(C D A B
11 14 19)(B C D A
0 20 20)
229 (A B C D
5 5 21)(D A B C
10 9 22)(C D A B
15 14 23)(B C D A
4 20 24)
230 (A B C D
9 5 25)(D A B C
14 9 26)(C D A B
3 14 27)(B C D A
8 20 28)
231 (A B C D
13 5 29)(D A B C
2 9 30)(C D A B
7 14 31)(B C D A
12 20 32))
233 (with-md5-round (h block
)
234 (A B C D
5 4 33)(D A B C
8 11 34)(C D A B
11 16 35)(B C D A
14 23 36)
235 (A B C D
1 4 37)(D A B C
4 11 38)(C D A B
7 16 39)(B C D A
10 23 40)
236 (A B C D
13 4 41)(D A B C
0 11 42)(C D A B
3 16 43)(B C D A
6 23 44)
237 (A B C D
9 4 45)(D A B C
12 11 46)(C D A B
15 16 47)(B C D A
2 23 48))
239 (with-md5-round (i block
)
240 (A B C D
0 6 49)(D A B C
7 10 50)(C D A B
14 15 51)(B C D A
5 21 52)
241 (A B C D
12 6 53)(D A B C
3 10 54)(C D A B
10 15 55)(B C D A
1 21 56)
242 (A B C D
8 6 57)(D A B C
15 10 58)(C D A B
6 15 59)(B C D A
13 21 60)
243 (A B C D
4 6 61)(D A B C
11 10 62)(C D A B
2 15 63)(B C D A
9 21 64))
245 (setf (md5-regs-a regs
) (mod32+ (md5-regs-a regs
) A
)
246 (md5-regs-b regs
) (mod32+ (md5-regs-b regs
) B
)
247 (md5-regs-c regs
) (mod32+ (md5-regs-c regs
) C
)
248 (md5-regs-d regs
) (mod32+ (md5-regs-d regs
) D
))
251 ;;; Section 3.4: Converting 8bit-vectors into 16-Word Blocks
253 (declaim (inline fill-block fill-block-ub8 fill-block-char
))
255 (defun fill-block-ub8 (block buffer offset
)
256 "Convert a complete 64 (unsigned-byte 8) input vector segment
257 starting from offset into the given 16 word MD5 block."
258 (declare (type (integer 0 #.
(- most-positive-fixnum
64)) offset
)
259 (type (simple-array ub32
(16)) block
)
260 (type (simple-array (unsigned-byte 8) (*)) buffer
)
261 (optimize (speed 3) (safety 0) (space 0) (debug 0)))
262 ;; #+(and :cmu :little-endian)
263 ;; (kernel:bit-bash-copy ;; There is a problem with this specific code (PBrochard)
264 ;; buffer (+ (* vm:vector-data-offset vm:word-bits) (* offset vm:byte-bits))
265 ;; block (* vm:vector-data-offset vm:word-bits)
266 ;; (* 64 vm:byte-bits))
267 ;; #-(and :cmu :little-endian)
268 (loop for i of-type
(integer 0 16) from
0
269 for j of-type
(integer 0 #.most-positive-fixnum
)
270 from offset to
(+ offset
63) by
4
273 (assemble-ub32 (aref buffer j
)
274 (aref buffer
(+ j
1))
275 (aref buffer
(+ j
2))
276 (aref buffer
(+ j
3))))))
278 (defun fill-block-char (block buffer offset
)
279 "Convert a complete 64 character input string segment starting from
280 offset into the given 16 word MD5 block."
281 (declare (type (integer 0 #.
(- most-positive-fixnum
64)) offset
)
282 (type (simple-array ub32
(16)) block
)
283 (type simple-string buffer
)
284 (optimize (speed 3) (safety 0) (space 0) (debug 0)))
285 ;; #+(and :cmu :little-endian)
286 ;; (kernel:bit-bash-copy ;; There is a problem with this specific code (PBrochard)
287 ;; buffer (+ (* vm:vector-data-offset vm:word-bits) (* offset vm:byte-bits))
288 ;; block (* vm:vector-data-offset vm:word-bits)
289 ;; (* 64 vm:byte-bits))
290 ;; #-(and :cmu :little-endian)
291 (loop for i of-type
(integer 0 16) from
0
292 for j of-type
(integer 0 #.most-positive-fixnum
)
293 from offset to
(+ offset
63) by
4
296 (assemble-ub32 (char-code (schar buffer j
))
297 (char-code (schar buffer
(+ j
1)))
298 (char-code (schar buffer
(+ j
2)))
299 (char-code (schar buffer
(+ j
3)))))))
301 (defun fill-block (block buffer offset
)
302 "Convert a complete 64 byte input vector segment into the given 16
303 word MD5 block. This currently works on (unsigned-byte 8) and
304 character simple-arrays, via the functions `fill-block-ub8' and
305 `fill-block-char' respectively."
306 (declare (type (integer 0 #.
(- most-positive-fixnum
64)) offset
)
307 (type (simple-array ub32
(16)) block
)
308 (type (simple-array * (*)) buffer
)
309 (optimize (speed 3) (safety 0) (space 0) (debug 0)))
311 ((simple-array (unsigned-byte 8) (*))
312 (fill-block-ub8 block buffer offset
))
314 (fill-block-char block buffer offset
))))
316 ;;; Section 3.5: Message Digest Output
318 (declaim (inline md5regs-digest
))
319 (defun md5regs-digest (regs)
320 "Create the final 16 byte message-digest from the MD5 working state
321 in regs. Returns a (simple-array (unsigned-byte 8) (16))."
322 (declare (optimize (speed 3) (safety 0) (space 0) (debug 0))
323 (type md5-regs regs
))
324 (let ((result (make-array 16 :element-type
'(unsigned-byte 8))))
325 (declare (type (simple-array (unsigned-byte 8) (16)) result
))
326 (macrolet ((frob (reg offset
)
327 (let ((var (gensym)))
329 (declare (type ub32
,var
))
331 (aref result
,offset
) (ldb (byte 8 0) ,var
)
332 (aref result
,(+ offset
1)) (ldb (byte 8 8) ,var
)
333 (aref result
,(+ offset
2)) (ldb (byte 8 16) ,var
)
334 (aref result
,(+ offset
3)) (ldb (byte 8 24) ,var
))))))
335 (frob (md5-regs-a regs
) 0)
336 (frob (md5-regs-b regs
) 4)
337 (frob (md5-regs-c regs
) 8)
338 (frob (md5-regs-d regs
) 12))
341 ;;; Mid-Level Drivers
343 (defstruct (md5-state
344 (:constructor make-md5-state
())
346 (regs (initial-md5-regs) :type md5-regs
:read-only t
)
348 #-md5-small-length
(integer 0 *)
349 #+md5-small-length
(unsigned-byte 29))
350 (block (make-array 16 :element-type
'(unsigned-byte 32)) :read-only t
351 :type
(simple-array (unsigned-byte 32) (16)))
352 (buffer (make-array 64 :element-type
'(unsigned-byte 8)) :read-only t
353 :type
(simple-array (unsigned-byte 8) (64)))
354 (buffer-index 0 :type
(integer 0 63))
357 (declaim (inline copy-to-buffer
))
358 (defun copy-to-buffer (from from-offset count buffer buffer-offset
)
359 "Copy a partial segment from input vector from starting at
360 from-offset and copying count elements into the 64 byte buffer
361 starting at buffer-offset."
362 (declare (optimize (speed 3) (safety 0) (space 0) (debug 0))
363 (type (unsigned-byte 29) from-offset
)
364 (type (integer 0 63) count buffer-offset
)
365 (type (simple-array * (*)) from
)
366 (type (simple-array (unsigned-byte 8) (64)) buffer
))
368 ;; (kernel:bit-bash-copy ;; There is a problem with this specific code (PBrochard)
369 ;; from (+ (* vm:vector-data-offset vm:word-bits) (* from-offset vm:byte-bits))
370 ;; buffer (+ (* vm:vector-data-offset vm:word-bits)
371 ;; (* buffer-offset vm:byte-bits))
372 ;; (* count vm:byte-bits))
376 (loop for buffer-index of-type
(integer 0 64) from buffer-offset
377 for from-index of-type fixnum from from-offset
378 below
(+ from-offset count
)
380 (setf (aref buffer buffer-index
)
381 (char-code (schar (the simple-string from
) from-index
)))))
382 ((simple-array (unsigned-byte 8) (*))
383 (loop for buffer-index of-type
(integer 0 64) from buffer-offset
384 for from-index of-type fixnum from from-offset
385 below
(+ from-offset count
)
387 (setf (aref buffer buffer-index
)
388 (aref (the (simple-array (unsigned-byte 8) (*)) from
)
391 (defun update-md5-state (state sequence
&key
(start 0) (end (length sequence
)))
392 "Update the given md5-state from sequence, which is either a
393 simple-string or a simple-array with element-type (unsigned-byte 8),
394 bounded by start and end, which must be numeric bounding-indices."
395 (declare (type md5-state state
)
396 (type (simple-array * (*)) sequence
)
397 (type fixnum start end
)
398 (optimize (speed 3) #+cmu
(safety 0) (space 0) (debug 0))
400 (ext:optimize-interface
(safety 1) (debug 1)))
401 (let ((regs (md5-state-regs state
))
402 (block (md5-state-block state
))
403 (buffer (md5-state-buffer state
)))
404 (declare (type md5-regs regs
)
405 (type (simple-array (unsigned-byte 32) (16)) block
)
406 (type (simple-array (unsigned-byte 8) (64)) buffer
))
408 (unless (zerop (md5-state-buffer-index state
))
409 (let* ((buffer-index (md5-state-buffer-index state
))
410 (remainder (- 64 buffer-index
))
411 (length (- end start
))
412 (amount (min remainder length
)))
413 (declare (type (integer 0 63) buffer-index remainder amount
)
414 (type fixnum length
))
415 (copy-to-buffer sequence start amount buffer buffer-index
)
416 (setf (md5-state-amount state
)
417 #-md5-small-length
(+ (md5-state-amount state
) amount
)
418 #+md5-small-length
(the (unsigned-byte 29)
419 (+ (md5-state-amount state
) amount
)))
420 (setq start
(the fixnum
(+ start amount
)))
421 (if (< length remainder
)
422 (setf (md5-state-buffer-index state
)
423 (the (integer 0 63) (+ buffer-index amount
)))
425 (fill-block-ub8 block buffer
0)
426 (update-md5-block regs block
)
427 (setf (md5-state-buffer-index state
) 0)))))
428 ;; Leave when nothing to do
430 (return-from update-md5-state state
))
431 ;; Handle main-part and new-rest
433 ((simple-array (unsigned-byte 8) (*))
435 (declare (type (simple-array (unsigned-byte 8) (*)) sequence
))
436 (loop for offset of-type
(unsigned-byte 29) from start below end by
64
437 until
(< (- end offset
) 64)
439 (fill-block-ub8 block sequence offset
)
440 (update-md5-block regs block
)
442 (let ((amount (- end offset
)))
443 (unless (zerop amount
)
444 (copy-to-buffer sequence offset amount buffer
0))
445 (setf (md5-state-buffer-index state
) amount
)))))
448 (declare (type simple-string sequence
))
449 (loop for offset of-type
(unsigned-byte 29) from start below end by
64
450 until
(< (- end offset
) 64)
452 (fill-block-char block sequence offset
)
453 (update-md5-block regs block
)
455 (let ((amount (- end offset
)))
456 (unless (zerop amount
)
457 (copy-to-buffer sequence offset amount buffer
0))
458 (setf (md5-state-buffer-index state
) amount
))))))
459 (setf (md5-state-amount state
)
460 #-md5-small-length
(+ (md5-state-amount state
)
461 (the fixnum
(- end start
)))
462 #+md5-small-length
(the (unsigned-byte 29)
463 (+ (md5-state-amount state
)
464 (the fixnum
(- end start
)))))
467 (defun finalize-md5-state (state)
468 "If the given md5-state has not already been finalized, finalize it,
469 by processing any remaining input in its buffer, with suitable padding
470 and appended bit-length, as specified by the MD5 standard.
472 The resulting MD5 message-digest is returned as an array of sixteen
473 (unsigned-byte 8) values. Calling `update-md5-state' after a call to
474 `finalize-md5-state' results in unspecified behaviour."
475 (declare (type md5-state state
)
476 (optimize (speed 3) #+cmu
(safety 0) (space 0) (debug 0))
478 (ext:optimize-interface
(safety 1) (debug 1)))
479 (or (md5-state-finalized-p state
)
480 (let ((regs (md5-state-regs state
))
481 (block (md5-state-block state
))
482 (buffer (md5-state-buffer state
))
483 (buffer-index (md5-state-buffer-index state
))
484 (total-length (* 8 (md5-state-amount state
))))
485 (declare (type md5-regs regs
)
486 (type (integer 0 63) buffer-index
)
487 (type (simple-array ub32
(16)) block
)
488 (type (simple-array (unsigned-byte 8) (*)) buffer
))
489 ;; Add mandatory bit 1 padding
490 (setf (aref buffer buffer-index
) #x80
)
491 ;; Fill with 0 bit padding
492 (loop for index of-type
(integer 0 64)
493 from
(1+ buffer-index
) below
64
494 do
(setf (aref buffer index
) #x00
))
495 (fill-block-ub8 block buffer
0)
496 ;; Flush block first if length wouldn't fit
497 (when (>= buffer-index
56)
498 (update-md5-block regs block
)
499 ;; Create new fully 0 padded block
500 (loop for index of-type
(integer 0 16) from
0 below
16
501 do
(setf (aref block index
) #x00000000
)))
502 ;; Add 64bit message bit length
503 (setf (aref block
14) (ldb (byte 32 0) total-length
))
505 (setf (aref block
15) (ldb (byte 32 32) total-length
))
507 (update-md5-block regs block
)
508 ;; Done, remember digest for later calls
509 (setf (md5-state-finalized-p state
)
510 (md5regs-digest regs
)))))
512 ;;; High-Level Drivers
514 (defun md5sum-sequence (sequence &key
(start 0) end
)
515 "Calculate the MD5 message-digest of data in sequence. On CMU CL
516 this works for all sequences whose element-type is supported by the
517 underlying MD5 routines, on other implementations it only works for 1d
518 simple-arrays with such element types."
519 (declare (optimize (speed 3) (space 0) (debug 0))
520 (type vector sequence
) (type fixnum start
))
521 (let ((state (make-md5-state)))
522 (declare (type md5-state state
))
524 (lisp::with-array-data
((data sequence
) (real-start start
) (real-end end
))
525 (update-md5-state state data
:start real-start
:end real-end
))
527 (let ((real-end (or end
(length sequence
))))
528 (declare (type fixnum real-end
))
529 (update-md5-state state sequence
:start start
:end real-end
))
530 (finalize-md5-state state
)))
532 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
533 (defconstant +buffer-size
+ (* 128 1024)
534 "Size of internal buffer to use for md5sum-stream and md5sum-file
535 operations. This should be a multiple of 64, the MD5 block size."))
537 (deftype buffer-index
() `(integer 0 ,+buffer-size
+))
539 (defun md5sum-stream (stream)
540 "Calculate an MD5 message-digest of the contents of stream. Its
541 element-type has to be either (unsigned-byte 8) or character."
542 (declare (optimize (speed 3) (space 0) (debug 0)))
543 (let ((state (make-md5-state)))
544 (declare (type md5-state state
))
546 ((equal (stream-element-type stream
) '(unsigned-byte 8))
547 (let ((buffer (make-array +buffer-size
+
548 :element-type
'(unsigned-byte 8))))
549 (declare (type (simple-array (unsigned-byte 8) (#.
+buffer-size
+))
551 (loop for bytes of-type buffer-index
= (read-sequence buffer stream
)
552 do
(update-md5-state state buffer
:end bytes
)
553 until
(< bytes
+buffer-size
+)
555 (return (finalize-md5-state state
)))))
556 ((equal (stream-element-type stream
) 'character
)
557 (let ((buffer (make-string +buffer-size
+)))
558 (declare (type (simple-string #.
+buffer-size
+) buffer
))
559 (loop for bytes of-type buffer-index
= (read-sequence buffer stream
)
560 do
(update-md5-state state buffer
:end bytes
)
561 until
(< bytes
+buffer-size
+)
563 (return (finalize-md5-state state
)))))
565 (error "Unsupported stream element-type ~S for stream ~S."
566 (stream-element-type stream
) stream
)))))
568 (defun md5sum-file (pathname)
569 "Calculate the MD5 message-digest of the file specified by pathname."
570 (declare (optimize (speed 3) (space 0) (debug 0)))
571 (with-open-file (stream pathname
:element-type
'(unsigned-byte 8))
572 (md5sum-stream stream
)))
576 (defun md5-string (md5-digest)
577 (format nil
"~(~{~2,'0X~}~)"
578 (map 'list
#'identity md5-digest
)))
581 (defun md5 (sequence)
582 (md5-string (md5sum-sequence sequence
)))
587 (defconstant +rfc1321-testsuite
+
588 '(("" .
"d41d8cd98f00b204e9800998ecf8427e")
589 ("a" .
"0cc175b9c0f1b6a831c399e269772661")
590 ("abc" .
"900150983cd24fb0d6963f7d28e17f72")
591 ("message digest" .
"f96b697d7cb7938d525a2f31aaf161d0")
592 ("abcdefghijklmnopqrstuvwxyz" .
"c3fcd3d76192e4007dfb496cca67e13b")
593 ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" .
594 "d174ab98d277d9f5a5611c2c9f419d9f")
595 ("12345678901234567890123456789012345678901234567890123456789012345678901234567890" .
596 "57edf4a22be3c955ac49da2e2107b67a"))
597 "AList of test input strings and stringified message-digests
598 according to the test suite in Appendix A.5 of RFC 1321")
601 (defconstant +other-testsuite
+
602 '(;; From padding bug report by Edi Weitz
603 ("1631901HERR BUCHHEISTERCITROEN NORD1043360796beckenbauer" .
604 "d734945e5930bb28859ccd13c830358b")
605 ;; Test padding for strings from 0 to 69*8 bits in size.
606 ("" .
"d41d8cd98f00b204e9800998ecf8427e")
607 ("a" .
"0cc175b9c0f1b6a831c399e269772661")
608 ("aa" .
"4124bc0a9335c27f086f24ba207a4912")
609 ("aaa" .
"47bce5c74f589f4867dbd57e9ca9f808")
610 ("aaaa" .
"74b87337454200d4d33f80c4663dc5e5")
611 ("aaaaa" .
"594f803b380a41396ed63dca39503542")
612 ("aaaaaa" .
"0b4e7a0e5fe84ad35fb5f95b9ceeac79")
613 ("aaaaaaa" .
"5d793fc5b00a2348c3fb9ab59e5ca98a")
614 ("aaaaaaaa" .
"3dbe00a167653a1aaee01d93e77e730e")
615 ("aaaaaaaaa" .
"552e6a97297c53e592208cf97fbb3b60")
616 ("aaaaaaaaaa" .
"e09c80c42fda55f9d992e59ca6b3307d")
617 ("aaaaaaaaaaa" .
"d57f21e6a273781dbf8b7657940f3b03")
618 ("aaaaaaaaaaaa" .
"45e4812014d83dde5666ebdf5a8ed1ed")
619 ("aaaaaaaaaaaaa" .
"c162de19c4c3731ca3428769d0cd593d")
620 ("aaaaaaaaaaaaaa" .
"451599a5f9afa91a0f2097040a796f3d")
621 ("aaaaaaaaaaaaaaa" .
"12f9cf6998d52dbe773b06f848bb3608")
622 ("aaaaaaaaaaaaaaaa" .
"23ca472302f49b3ea5592b146a312da0")
623 ("aaaaaaaaaaaaaaaaa" .
"88e42e96cc71151b6e1938a1699b0a27")
624 ("aaaaaaaaaaaaaaaaaa" .
"2c60c24e7087e18e45055a33f9a5be91")
625 ("aaaaaaaaaaaaaaaaaaa" .
"639d76897485360b3147e66e0a8a3d6c")
626 ("aaaaaaaaaaaaaaaaaaaa" .
"22d42eb002cefa81e9ad604ea57bc01d")
627 ("aaaaaaaaaaaaaaaaaaaaa" .
"bd049f221af82804c5a2826809337c9b")
628 ("aaaaaaaaaaaaaaaaaaaaaa" .
"ff49cfac3968dbce26ebe7d4823e58bd")
629 ("aaaaaaaaaaaaaaaaaaaaaaa" .
"d95dbfee231e34cccb8c04444412ed7d")
630 ("aaaaaaaaaaaaaaaaaaaaaaaa" .
"40edae4bad0e5bf6d6c2dc5615a86afb")
631 ("aaaaaaaaaaaaaaaaaaaaaaaaa" .
"a5a8bfa3962f49330227955e24a2e67c")
632 ("aaaaaaaaaaaaaaaaaaaaaaaaaa" .
"ae791f19bdf77357ff10bb6b0e97e121")
633 ("aaaaaaaaaaaaaaaaaaaaaaaaaaa" .
"aaab9c59a88bf0bdfcb170546c5459d6")
634 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
"b0f0545856af1a340acdedce23c54b97")
635 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
"f7ce3d7d44f3342107d884bfa90c966a")
636 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
"59e794d45697b360e18ba972bada0123")
637 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
"3b0845db57c200be6052466f87b2198a")
638 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
"5eca9bd3eb07c006cd43ae48dfde7fd3")
639 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
"b4f13cb081e412f44e99742cb128a1a5")
640 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
"4c660346451b8cf91ef50f4634458d41")
641 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
642 "11db24dc3f6c2145701db08625dd6d76")
643 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
644 "80dad3aad8584778352c68ab06250327")
645 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
646 "1227fe415e79db47285cb2689c93963f")
647 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
648 "8e084f489f1bdf08c39f98ff6447ce6d")
649 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
650 "08b2f2b0864bac1ba1585043362cbec9")
651 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
652 "4697843037d962f62a5a429e611e0f5f")
653 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
654 "10c4da18575c092b486f8ab96c01c02f")
655 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
656 "af205d729450b663f48b11d839a1c8df")
657 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
658 "0d3f91798fac6ee279ec2485b25f1124")
659 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
660 "4c3c7c067634daec9716a80ea886d123")
661 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
662 "d1e358e6e3b707282cdd06e919f7e08c")
663 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
664 "8c6ded4f0af86e0a7e301f8a716c4363")
665 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
666 "4c2d8bcb02d982d7cb77f649c0a2dea8")
667 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
668 "bdb662f765cd310f2a547cab1cfecef6")
669 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
670 "08ff5f7301d30200ab89169f6afdb7af")
671 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
672 "6eb6a030bcce166534b95bc2ab45d9cf")
673 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
674 "1bb77918e5695c944be02c16ae29b25e")
675 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
676 "b6fe77c19f0f0f4946c761d62585bfea")
677 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
678 "e9e7e260dce84ffa6e0e7eb5fd9d37fc")
679 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
680 "eced9e0b81ef2bba605cbc5e2e76a1d0")
681 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
682 "ef1772b6dff9a122358552954ad0df65")
683 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
684 "3b0c8ac703f828b04c6c197006d17218")
685 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
686 "652b906d60af96844ebd21b674f35e93")
687 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
688 "dc2f2f2462a0d72358b2f99389458606")
689 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
690 "762fc2665994b217c52c3c2eb7d9f406")
691 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
692 "cc7ed669cf88f201c3297c6a91e1d18d")
693 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
694 "cced11f7bbbffea2f718903216643648")
695 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
696 "24612f0ce2c9d2cf2b022ef1e027a54f")
697 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
698 "b06521f39153d618550606be297466d5")
699 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
700 "014842d480b571495a4a0363793f7367")
701 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
702 "c743a45e0d2e6a95cb859adae0248435")
703 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
704 "def5d97e01e1219fb2fc8da6c4d6ba2f")
705 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
706 "92cb737f8687ccb93022fdb411a77cca")
707 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
708 "a0d1395c7fb36247bfe2d49376d9d133")
709 ("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" .
710 "ab75504250558b788f99d1ebd219abf2"))
711 "AList of test input strings and stringified message-digests
712 according to my additional test suite")
715 (defun test-with-testsuite (testsuite)
716 (loop for count from
1
717 for
(source . md5-string
) in testsuite
718 for md5-digest
= (md5sum-sequence source
)
719 for md5-result-string
= (md5-string md5-digest
)
723 "~2&Test-Case ~D:~% Input: ~S~% Required: ~A~% Returned: ~A~%"
724 count source md5-string md5-result-string
)
725 when
(string= md5-string md5-result-string
)
726 do
(format *trace-output
* " OK~%")
729 and do
(format *trace-output
* " FAILED~%")
731 (format *trace-output
*
732 "~2&~[All ~D test cases succeeded~:;~:*~D of ~D test cases failed~].~%"
734 (return (zerop failed
))))
737 (defun test-rfc1321 ()
738 (test-with-testsuite +rfc1321-testsuite
+))
742 (test-with-testsuite +other-testsuite
+))
745 (eval-when (:compile-toplevel
:execute
)
746 (setq *features
* *old-features
*))
749 (eval-when (:compile-toplevel
)
750 (setq ext
:*inline-expansion-limit
* *old-expansion-limit
*))