hmac-md5.el (hmac-md5): Specify the 4th arg to `md5' as `binary'
[elisp/flim.git] / hmac-util.el
1 ;;; hmac-util.el --- Utilities for HMAC functions.
2
3 ;; Copyright (C) 1999 Shuhei KOBAYASHI
4
5 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
6 ;; Keywords: HMAC, RFC 2104
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 ;;; Code:
28
29 (defsubst hmac-hex-to-int (chr)
30   (cond ((<= ?a chr) (+ (- chr ?a) 10))
31         ((<= ?A chr) (+ (- chr ?A) 10))
32         ((<= ?0 chr) (- chr ?0))))
33
34 (defsubst hmac-int-to-hex (num)
35   (aref "0123456789abcdef" num))
36
37 (defun hmac-unhex-string (str)
38   (let* ((len (length str))
39          (dst (make-string (/ len 2) 0))
40          (idx 0)(pos 0))
41     (while (< pos len)
42       (aset dst idx (logior (lsh (hmac-hex-to-int (aref str pos)) 4)
43                             (hmac-hex-to-int (aref str (1+ pos)))))
44       (setq idx (1+ idx)
45             pos (+ 2 pos)))
46     dst))
47
48 (defun hmac-hex-string (str)
49   (let* ((len (length str))
50          (dst (make-string (* len 2) 0))
51          (idx 0)(pos 0))
52     (while (< pos len)
53       (aset dst idx (hmac-int-to-hex (logand (lsh (aref str pos) -4) 15)))
54       (setq idx (1+ idx))
55       (aset dst idx (hmac-int-to-hex (logand (aref str pos) 15)))
56       (setq idx (1+ idx)
57             pos (1+ pos)))
58     dst))
59
60 (provide 'hmac-util)
61
62 ;;; hmac-util.el ends here.