| 1 |
;;; |
| 2 |
;;; digest - abstract base class for message digest algorithms |
| 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 |
;; An abstract base- and meta-class of message digest algorithms. |
| 35 |
;; The concrete implementation is given in modules such as |
| 36 |
;; rfc.md5 and rfc.sha. |
| 37 |
|
| 38 |
(define-module util.digest |
| 39 |
(use gauche.uvector) |
| 40 |
(use rfc.base64) |
| 41 |
(export <message-digest-algorithm> <message-digest-algorithm-meta> |
| 42 |
digest-update! digest-final! digest |
| 43 |
digest-to digest-message-to |
| 44 |
|
| 45 |
;; Obsoleted API |
| 46 |
digest-string digest-hexify) |
| 47 |
) |
| 48 |
(select-module util.digest) |
| 49 |
|
| 50 |
(autoload gauche.vport open-input-uvector) |
| 51 |
|
| 52 |
;; API for digest algorithms |
| 53 |
;; Actual digest algorithms such as SHA should subclass |
| 54 |
;; <message-digest-algorithm>, and implement digest-update! |
| 55 |
;; and digest-final!, or digest. |
| 56 |
(define-class <message-digest-algorithm-meta> (<class>) |
| 57 |
;; Block size (in bytes) used in HMAC, determined by each algorithm. |
| 58 |
;; Older algorithms uses 64, while SHA-384/512 uses 128. |
| 59 |
((hmac-block-size :init-keyword :hmac-block-size :init-value 64))) |
| 60 |
|
| 61 |
(define-class <message-digest-algorithm> () |
| 62 |
() |
| 63 |
:metaclass <message-digest-algorithm-meta>) |
| 64 |
|
| 65 |
(define-method digest-update! ((self <message-digest-algorithm>) data) |
| 66 |
#f) |
| 67 |
(define-method digest-final! ((self <message-digest-algorithm>)) |
| 68 |
#f) |
| 69 |
(define-method digest ((digester <message-digest-algorithm-meta>)) |
| 70 |
#f) |
| 71 |
|
| 72 |
;; User API |
| 73 |
(define-method digest-to ((target <string-meta>) |
| 74 |
(digester <message-digest-algorithm-meta>)) |
| 75 |
(digest digester)) |
| 76 |
(define-method digest-to ((target <u8vector-meta>) |
| 77 |
(digester <message-digest-algorithm-meta>)) |
| 78 |
(string->u8vector (digest digester))) |
| 79 |
;; Special targets: |
| 80 |
;; base64 |
| 81 |
;; base64url |
| 82 |
;; base64url-nopad |
| 83 |
;; base32 |
| 84 |
;; base32hex |
| 85 |
;; base16 |
| 86 |
;; hex |
| 87 |
(define-method digest-to ((target <symbol>) |
| 88 |
(digester <message-digest-algorithm-meta>)) |
| 89 |
(define encoder |
| 90 |
(ecase target |
| 91 |
[(base64) base64-encode-message] |
| 92 |
[(base64url) (cut base64-encode-message <> :url-safe #t)] |
| 93 |
[(base64url-nopad) (cut base64-encode-message <> |
| 94 |
:url-safe #t :omit-padding #t)] |
| 95 |
[(base32) base32-encode-message] |
| 96 |
[(base32hex) base32hex-encode-message] |
| 97 |
[(base16) base16-encode-message] |
| 98 |
[(hex) (cut base16-encode-message <> :lowercase #t)])) |
| 99 |
(encoder (digest digester))) |
| 100 |
|
| 101 |
;; User API |
| 102 |
(define-method digest-message-to (target |
| 103 |
(digester <message-digest-algorithm-meta>) |
| 104 |
message) |
| 105 |
(etypecase message |
| 106 |
[<string> (with-input-from-string message (cut digest-to target digester))] |
| 107 |
[<u8vector> (with-input-from-port (open-input-uvector message) |
| 108 |
(cut digest-to target digester))])) |
| 109 |
|
| 110 |
|
| 111 |
;; OBSOLETED |
| 112 |
;; Use digest-message-to. |
| 113 |
(define-method digest-string ((digester <message-digest-algorithm-meta>) |
| 114 |
string) |
| 115 |
(digest-message-to <string> digester string)) |
| 116 |
|
| 117 |
;; OBSOLETED |
| 118 |
;; Use base16-encode-message, or digest-message-to with |
| 119 |
;; predefined encoder targets. |
| 120 |
(define (digest-hexify data) (base16-encode-message data :lowercase #t)) |