Debian Codegraph @

ext/digest/md5.scm

raw
1 ;;;
2 ;;; md5 - MD5 message-digest
3 ;;;
4 ;;; Copyright (c) 2002-2003 Kimura Fuyuki, All rights reserved.
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;;; RFC 1321 The MD5 Message-Digest Algorithm
35
36 (define-module rfc.md5
37 (extend util.digest)
38 (use gauche.uvector)
39 (export <md5> md5-digest md5-digest-string)
40 )
41 (select-module rfc.md5)
42
43 ;;;
44 ;;; High-level stuff
45 ;;;
46
47 (define-class <md5-meta> (<message-digest-algorithm-meta>)
48 ())
49
50 (define-class <md5> (<message-digest-algorithm>)
51 ((context :getter context-of))
52 :metaclass <md5-meta>)
53
54 (define-method initialize ((self <md5>) initargs)
55 (next-method)
56 (slot-set! self 'context (make <md5-context>)))
57
58 (define-constant *md5-unit-len* 4096)
59
60 (define (md5-digest)
61 (let ([md5 (make <md5-context>)]
62 [buf (make-u8vector *md5-unit-len*)])
63 (generator-for-each
64 (^x (%md5-update md5 x))
65 (^[] (let1 count (read-block! buf)
66 (cond [(eof-object? count) count]
67 [(< count *md5-unit-len*)
68 (uvector-alias <u8vector> buf 0 count)]
69 [else buf]))))
70 (%md5-final md5)))
71
72 (define (md5-digest-string string)
73 (with-input-from-string string md5-digest))
74
75 ;;;
76 ;;; Digest framework
77 ;;;
78 (define-method digest-update! ((self <md5>) data)
79 (%md5-update (context-of self) data))
80 (define-method digest-final! ((self <md5>))
81 (%md5-final (context-of self)))
82 (define-method digest ((class <md5-meta>))
83 (md5-digest))
84
85 ;;;
86 ;;; Low-level bindings
87 ;;;
88
89 (inline-stub
90 (declcode
91 (.include <gauche/priv/configP.h>
92 "md5.h")
93
94 (.define LIBGAUCHE_EXT_BODY)
95 (.include <gauche/extern.h>) ; fix SCM_EXTERN in SCM_CLASS_DECL
96 )
97
98 (define-ctype ScmMd5Context::(.struct
99 (SCM_HEADER :: ""
100 ctx::MD5_CTX)))
101
102 (define-cclass <md5-context> :private
103 ScmMd5Context* "Scm_Md5ContextClass" ()
104 ()
105 [allocator
106 (let* ([md5::ScmMd5Context* (SCM_NEW_INSTANCE ScmMd5Context klass)])
107 (cast void initargs) ; suppress unused var warning
108 (MD5_Init (& (-> md5 ctx)))
109 (return (SCM_OBJ md5)))])
110
111 (define-cproc %md5-update (md5::<md5-context> data) ::<void>
112 (cond
113 [(SCM_U8VECTORP data)
114 (MD5_Update (& (-> md5 ctx))
115 (SCM_UVECTOR_ELEMENTS (SCM_U8VECTOR data))
116 (SCM_U8VECTOR_SIZE (SCM_U8VECTOR data)))]
117 [(SCM_STRINGP data)
118 (let* ([b::(const ScmStringBody*) (SCM_STRING_BODY data)])
119 (MD5_Update (& (-> md5 ctx))
120 (cast (const unsigned char*) (SCM_STRING_BODY_START b))
121 (SCM_STRING_BODY_SIZE b)))]
122 [else (SCM_TYPE_ERROR data "u8vector or string")]))
123
124 (define-cproc %md5-final (md5::<md5-context>)
125 (let* ([digest::(.array (unsigned char) [16])])
126 (MD5_Final digest (& (-> md5 ctx)))
127 (return (Scm_MakeString (cast (char *) digest) 16 16
128 (logior SCM_STRING_INCOMPLETE
129 SCM_STRING_COPYING)))))
130 )