File: md5-old.el

package info (click to toggle)
xemacs21-packages 2009.02.17.dfsg.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 116,928 kB
  • ctags: 88,975
  • sloc: lisp: 1,232,060; ansic: 16,570; java: 13,514; xml: 6,477; sh: 4,611; makefile: 4,036; asm: 3,007; perl: 839; cpp: 500; ruby: 257; csh: 96; haskell: 93; awk: 49; python: 47
file content (298 lines) | stat: -rw-r--r-- 8,748 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
;;;  md5-old.el -- MD5 message digest algorithm

;; Copyright (C) 1998 Ray Jones

;; Author: Ray Jones, rjones@pobox.com
;; Keywords: MD5, message digest
;; Created: 1998-04-27

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, you can either send email to this
;; program's maintainer or write to: The Free Software Foundation,
;; Inc.; 675 Massachusetts Avenue; Cambridge, MA 02139, USA.

;;; Commentary:

;; this is a slower, more clear, version of md5.el.  it's based on md5-old.el

;;; Code:
(require 'cl)

(defun md5 (string)
  "return the md5 hash of a string, as a 128 bit string"
  (let* ((length (length string))
	 ;; md5 requires the message be padded to a length of 512*k +
	 ;; 64 (bits).  confusion source: we're working with bytes.
	 ;; padding is always done.
	 ;; 512 bits = 64 bytes, 64 bits = 8 bytes
	 (next-512 (+ 64 (logand (+ length 8) (lognot 63))))
	 (pad-bytes (- next-512 length 8))
	 (pad-string (make-string pad-bytes 0))
	 (len-string (make-string 8 0)))
    ;; message is constructed as:
    ;; original-message | pad | length-in-bits
    ;; pad is 10000... (bitwise)
    ;; length-in-bits is length before padding, and is 64 bits long

    ;; fill in the single bit of the pad
    (aset pad-string 0 (ash 1 7))

    ;; there's a slim chance of overflow when multiplying the length
    ;; by 8 to get the length in bits.  to avoid this, do some
    ;; slightly hairier math when writing the length into len-string.
    ;; also, it has to be LSB-first.  be still my aching brain.

    ;; LSB sucks.

    ;; only do the first 4 bytes, even though supposedly there are 8.
    ;; 32 bit emacsen think that (ash 40 -37) => 1
    ;; (supposed to be fixed in future releases)
    (dotimes (idx 4)
      (aset len-string idx (logand ?\xff
				   (ash length (- 3 (* idx 8))))))
    
    (md5-vector
     (md5-string-to-32bit-vec
      (concat string pad-string len-string)))))

(defun md5-string-to-32bit-vec (string)
  ;; emacs doesn't actually have 32 bits, in most implementations.
  ;; 32 bit numbers are represented as a pair of 16 bit numbers

  ;; 4 chars per 32 bit number, in LSB-first!
  (let* ((veclen (/ (length string) 4))
	 (vec (make-vector veclen nil))
	 (stridx 0))
    (dotimes (vecidx veclen)
      ;; MD5 integers are (hi . lo) 16 bit words
      (aset vec vecidx (cons (+ (ash (aref string (+ stridx 3)) 8)
				(aref string (+ stridx 2)))
			     (+ (ash (aref string (+ stridx 1)) 8)
				(aref string (+ stridx 0)))))
      (incf stridx 4))

    vec))

(defsubst md5-f2 (x y z)
  (logior (logand x y)
	  (logand (lognot x)
		  z)))

(defsubst md5-g2 (x y z)
  (logior (logand x z)
	  (logand y (lognot z))))

(defsubst md5-h2 (x y z)
  (logxor x y z))

(defsubst md5-i2 (x y z)
  (logxor y
	  (logior x
		  ;; this is normally a lognot, but that would set
		  ;; high bits, and there's no logand to clear them.
		  (logxor z #xffff))))

(defsubst md5-f (x y z)
  (cons (md5-f2 (car x) (car y) (car z))
	(md5-f2 (cdr x) (cdr y) (cdr z))))

(defsubst md5-g (x y z)
  (cons (md5-g2 (car x) (car y) (car z))
	(md5-g2 (cdr x) (cdr y) (cdr z))))

(defsubst md5-h (x y z)
  (cons (md5-h2 (car x) (car y) (car z))
	(md5-h2 (cdr x) (cdr y) (cdr z))))

(defsubst md5-i (x y z)
  (cons (md5-i2 (car x) (car y) (car z))
	(md5-i2 (cdr x) (cdr y) (cdr z))))

(defsubst md5<<< (val shift)
  "circular shift md5 32 bit int VAL by SHIFT bits"
  (let ((a (car val))
	(b (cdr val)))

    ;; shifts greater than 16 need to be handled by a swap, then a
    ;; smaller shift
    (when (> shift 16)
      (rotatef a b)
      (decf shift 16))

    (cons (logand #xffff (logior (ash a shift) (ash b (- shift 16))))
	  (logand #xffff (logior (ash b shift) (ash a (- shift 16)))))))

(defsubst md5+ (&rest args)
  ;; enough room to just add without carry checks
  (let* ((lo (apply #'+ (mapcar #'cdr args)))
	 (hi (+ (ash lo -16) (apply #'+ (mapcar #'car args)))))
    (cons (logand #xffff hi)
	  (logand #xffff lo))))

;; array of values for i=[1..64] => floor(2^32 * abs(sin(i)))
(defconst md5-t

  [(#xd76a . #xa478)
   (#xe8c7 . #xb756)
   (#x2420 . #x70db)
   (#xc1bd . #xceee)
   (#xf57c . #x0faf)
   (#x4787 . #xc62a)
   (#xa830 . #x4613)
   (#xfd46 . #x9501)
   (#x6980 . #x98d8)
   (#x8b44 . #xf7af)
   (#xffff . #x5bb1)
   (#x895c . #xd7be)
   (#x6b90 . #x1122)
   (#xfd98 . #x7193)
   (#xa679 . #x438e)
   (#x49b4 . #x0821)

   (#xf61e . #x2562)
   (#xc040 . #xb340)
   (#x265e . #x5a51)
   (#xe9b6 . #xc7aa)
   (#xd62f . #x105d)
   (#x0244 . #x1453)
   (#xd8a1 . #xe681)
   (#xe7d3 . #xfbc8)
   (#x21e1 . #xcde6)
   (#xc337 . #x07d6)
   (#xf4d5 . #x0d87)
   (#x455a . #x14ed)
   (#xa9e3 . #xe905)
   (#xfcef . #xa3f8)
   (#x676f . #x02d9)
   (#x8d2a . #x4c8a)

   (#xfffa . #x3942)
   (#x8771 . #xf681)
   (#x6d9d . #x6122)
   (#xfde5 . #x380c)
   (#xa4be . #xea44)
   (#x4bde . #xcfa9)
   (#xf6bb . #x4b60)
   (#xbebf . #xbc70)
   (#x289b . #x7ec6)
   (#xeaa1 . #x27fa)
   (#xd4ef . #x3085)
   (#x0488 . #x1d05)
   (#xd9d4 . #xd039)
   (#xe6db . #x99e5)
   (#x1fa2 . #x7cf8)
   (#xc4ac . #x5665)

   (#xf429 . #x2244)
   (#x432a . #xff97)
   (#xab94 . #x23a7)
   (#xfc93 . #xa039)
   (#x655b . #x59c3)
   (#x8f0c . #xcc92)
   (#xffef . #xf47d)
   (#x8584 . #x5dd1)
   (#x6fa8 . #x7e4f)
   (#xfe2c . #xe6e0)
   (#xa301 . #x4314)
   (#x4e08 . #x11a1)
   (#xf753 . #x7e82)
   (#xbd3a . #xf235)
   (#x2ad7 . #xd2bb)
   (#xeb86 . #xd391)])

(eval-and-compile
  (defun md5-rewrite (fun w x y z vec-idx shift)
    "helper function for md5-vector, below.  ugly coding practice,
having a macro-rewriter elsewhere, but the indentation was getting a
bit out of control.
NB: vec, v-offset, and t-idx below must be defined where the macro is
called!" 
    `(setq ,w (md5+ ,x
		    (md5<<< (md5+ ,w
				  ,(list fun x y z)
				  (aref vec (+ v-offset ,vec-idx))
				  (aref md5-t t-idx))
			    ,shift)))))


(defun md5-vector (vec)
  ;; initialize the chaining variables
  (let ((a (cons #x6745 #x2301))
	(b (cons #xefcd #xab89))
	(c (cons #x98ba #xdcfe))
	(d (cons #x1032 #x5476))
	(v-offset 0))

    (dotimes (count (/ (length vec) 16))
      (let ((AA a) (BB b) (CC c) (DD d)
	    (t-idx 0))
	(macrolet
	    ((f (v1 v2 v3 v4 v-idx shift)
		`(progn
		   ,(md5-rewrite 'md5-f v1 v2 v3 v4 v-idx shift)
		   (incf t-idx))))

	  (f a b c d  0  7) (f d a b c  1 12) (f c d a b  2 17) (f b c d a  3 22)
	  (f a b c d  4  7) (f d a b c  5 12) (f c d a b  6 17) (f b c d a  7 22)
	  (f a b c d  8  7) (f d a b c  9 12) (f c d a b 10 17) (f b c d a 11 22)
	  (f a b c d 12  7) (f d a b c 13 12) (f c d a b 14 17) (f b c d a 15 22))

	(macrolet
	    ((g (v1 v2 v3 v4 v-idx shift)
		`(progn
		   ,(md5-rewrite 'md5-g v1 v2 v3 v4 v-idx shift)
		   (incf t-idx))))

	  (g a b c d  1  5) (g d a b c  6  9) (g c d a b 11 14) (g b c d a  0 20)
	  (g a b c d  5  5) (g d a b c 10  9) (g c d a b 15 14) (g b c d a  4 20)
	  (g a b c d  9  5) (g d a b c 14  9) (g c d a b  3 14) (g b c d a  8 20)
	  (g a b c d 13  5) (g d a b c  2  9) (g c d a b  7 14) (g b c d a 12 20))

	(macrolet
	    ((h (v1 v2 v3 v4 v-idx shift)
		`(progn
		   ,(md5-rewrite 'md5-h v1 v2 v3 v4 v-idx shift)
		   (incf t-idx))))

	  (h a b c d  5  4) (h d a b c  8 11) (h c d a b 11 16) (h b c d a 14 23)
	  (h a b c d  1  4) (h d a b c  4 11) (h c d a b  7 16) (h b c d a 10 23)
	  (h a b c d 13  4) (h d a b c  0 11) (h c d a b  3 16) (h b c d a  6 23)
	  (h a b c d  9  4) (h d a b c 12 11) (h c d a b 15 16) (h b c d a  2 23))

	(macrolet
	    ((i (v1 v2 v3 v4 v-idx shift)
		`(progn
		   ,(md5-rewrite `md5-i v1 v2 v3 v4 v-idx shift)
		   (incf t-idx))))

	  (i a b c d  0  6) (i d a b c  7 10) (i c d a b 14 15) (i b c d a  5 21)
	  (i a b c d 12  6) (i d a b c  3 10) (i c d a b 10 15) (i b c d a  1 21)
	  (i a b c d  8  6) (i d a b c 15 10) (i c d a b  6 15) (i b c d a 13 21)
	  (i a b c d  4  6) (i d a b c 11 10) (i c d a b  2 15) (i b c d a  9 21))

	(setq a (md5+ AA a)
	      b (md5+ BB b)
	      c (md5+ CC c)
	      d (md5+ DD d)))

      (incf v-offset 16))

    ;; swap back from LSB-first.  i feel ill.
    (mapconcat #'(lambda (x) (format "%02x%02x" (logand #xff x) (ash x -8)))
	       (list (cdr a) (car a)
		     (cdr b) (car b)
		     (cdr c) (car c)
		     (cdr d) (car d))
	       "")))

(provide 'md5)