* FLIM-ELS (flim-modules): Add `digest-md5'.
[elisp/flim.git] / digest-md5.el
1 ;;; digest-md5.el --- Compute DIGEST-MD5.
2
3 ;; Copyright (C) 1999 Kenichi OKADA
4
5 ;; Author: Kenichi OKADA <okada@opaopa.org>
6 ;; Keywords: DIGEST-MD5, HMAC-MD5, SASL, IMAP, POP, ACAP
7
8 ;; This file is part of FLIM (Faithful Library about Internet Message).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or
13 ;; (at your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; This program is implemented from draft-leach-digest-sasl-05.txt.
28 ;;
29 ;; It is caller's responsibility to base64-decode challenges and
30 ;; base64-encode responses in IMAP4 AUTHENTICATE command.
31 ;;
32 ;; Passphrase should be longer than 16 bytes. (See RFC 2195)
33
34 ;; Examples.
35 ;;
36 ;; (digest-md5-make-response "chris" "elwood.innosoft.com"
37 ;;                        "OA6MG9tEQGm2hh" "OA6MHXh6VqTrRk" 1
38 ;;                        "auth" "imap/elwood.innosoft.com"
39 ;;                        "d388dad90d4bbd760a152321f2143af7" nil "utf-8")
40 ;; => "charset=utf-8,username=\"chris\",realm=\"elwood.innosoft.com\",nonce=\"OA6MG9tEQGm2hh\",nc=00000001,cnonce=\"OA6MHXh6VqTrRk\",digest-uri=\"imap/elwood.innosoft.com\",response=d388dad90d4bbd760a152321f2143af7,qop=auth"
41 ;;
42
43 ;;; Code:
44
45 (require 'hmac-md5)
46
47 (defun digest-md5-digest-uri (serv-type host &optional serv-name)
48   (concat serv-type "/" host
49           (if (and serv-name
50                    (null (string= host serv-name)))
51               (concat "/" serv-name))))
52
53 (defun digest-md5-digest-response (username 
54                                    realm nonce cnonce
55                                    nonce-count qop digest-uri response 
56                                    &optional maxbuf charset cipher authzid)
57   (concat
58    (if charset
59        (concat "charset=" charset ","))
60    "username=\"" username "\""
61    ",realm=\"" realm "\""
62    ",nonce=\"" nonce "\""
63    (format ",nc=%08x" nonce-count)
64    ",cnonce=\"" cnonce "\""
65    ",digest-uri=\"" digest-uri "\""
66    ",response=" response
67    ",qop=" qop
68    (if maxbuf
69        (concat ",maxbuf=" maxbuf))
70    (if cipher
71        (concat ",cipher=" cipher))
72    (if authzid
73        (concat ",authzid=\"" authzid "\""))))
74
75   
76 (provide 'digest-md5)
77
78 ;;; digest-md5.el ends here