Importing XEmacs to Java bridge.
[elisp/xemacs-java.git] / sha1-java.el
1 ;;; sha1-java.el --- SHA1 Secure Hash Algorithm using Java.
2
3 ;; Copyright (C) 2000 Daiki Ueno
4
5 ;; Author: Daiki Ueno <ueno@unixuser.org>
6 ;; Keywords: SHA1, FIPS 180-1
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 (defvar sha1-java-message-digest
30   (let ((class (java-find-class "java.security.MessageDigest")))
31     (java-call-static-method class "getInstance" "SHA")))
32
33 (defun sha1-java-update (bytes)
34   "Update the current SHA1 state with BYTES (an string of uni-bytes)."
35   (let ((bytes (mapvector #'char-to-int bytes)))
36     (java-call-virtual-method sha1-java-message-digest "update" bytes)))
37
38 (defun sha1-java-encode (message &optional binary)
39   "Encodes MESSAGE using the SHA1 message digest algorithm.
40 MESSAGE must be a unibyte-string.
41 By default, return a string which formed hex-decimal charcters
42 from message digest.
43 If optional argument BINARY is non-nil, then return binary formed
44 string of message digest."
45   (sha1-java-update message)
46   (mapconcat
47    (lambda (byte)
48      (if binary
49          (char-to-string (logand byte #xff))
50        (format "%02x" (logand byte #xff))))
51    (java-call-virtual-method sha1-java-message-digest "digest") ""))
52
53 (provide 'sha1-java)
54
55 ;;; sha1-java.el ends here