From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Leo Newsgroups: gmane.emacs.devel Subject: Re: Move sha1 to C? Date: Mon, 23 May 2011 16:30:50 +0800 Message-ID: References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: dough.gmane.org 1306139493 30342 80.91.229.12 (23 May 2011 08:31:33 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 23 May 2011 08:31:33 +0000 (UTC) Cc: Stefan Monnier , emacs-devel@gnu.org To: Eli Zaretskii Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon May 23 10:31:28 2011 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1QOQXv-0001ha-J9 for ged-emacs-devel@m.gmane.org; Mon, 23 May 2011 10:31:27 +0200 Original-Received: from localhost ([::1]:54353 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QOQXv-0004uR-4l for ged-emacs-devel@m.gmane.org; Mon, 23 May 2011 04:31:27 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:52656) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QOQXq-0004uC-T3 for emacs-devel@gnu.org; Mon, 23 May 2011 04:31:24 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QOQXp-0003k0-A7 for emacs-devel@gnu.org; Mon, 23 May 2011 04:31:22 -0400 Original-Received: from mail-pv0-f169.google.com ([74.125.83.169]:33862) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QOQXo-0003jb-K8; Mon, 23 May 2011 04:31:21 -0400 Original-Received: by pvc12 with SMTP id 12so3405751pvc.0 for ; Mon, 23 May 2011 01:31:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-type; bh=95pkeWvSADkSV+Qp6DVtAx49blkOUfZfcNF3KHACFAs=; b=azgA2f+uZxLJB8fqtTx/rtXwXP2gwH/dkoiroTZ5rLXIL76WIND+l0/9ZApmx7iblx yMjOdz1nAJplXwGLKoGmvVu+VnCYuZXPRQ5AZbHNV238c8lwxvkb70QPdUbEtDw7hFZ6 TVqKXT5IsBWbfWPnDfSaZe1ysaP4IKYUMrSOs= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; b=ojB/R0q4/hAr89wRyhYzbhJGwVNMyON0y21KzJEClnbNB1GU7D+KZmYB3MuhobA3Et VhJa1Qgiyt2lXlaAOdfh4lBOohYw7So3SJJxW80PeEe176Nz3AJkE4HpFA/W9q+KTmJc 5InW9hxr1V50svza4rafRWTidrHJ4NH1FvYRg= Original-Received: by 10.68.28.132 with SMTP id b4mr2004808pbh.433.1306139478705; Mon, 23 May 2011 01:31:18 -0700 (PDT) Original-Received: from th041094.ip.tsinghua.edu.cn ([123.114.36.240]) by mx.google.com with ESMTPS id s5sm4205670pba.64.2011.05.23.01.31.03 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 23 May 2011 01:31:17 -0700 (PDT) In-Reply-To: (Eli Zaretskii's message of "Mon, 23 May 2011 03:21:19 -0400") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3.50 (Mac OS X 10.6.7) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 74.125.83.169 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:139637 Archived-At: --=-=-= Content-Type: text/plain On 2011-05-23 15:21 +0800, Eli Zaretskii wrote: >> From: Leo >> Date: Mon, 23 May 2011 04:02:04 +0800 >> >> 1. add sha1.[ch] from gnulib > > Note that gnulib's sha1.c uses malloc, which is not safe in Emacs. > So either make sure sha1_stream is never ever called by Emacs, or use > xmalloc instead. Thanks for this note. sha1_stream is not used. >> Should the signature of sha1 stay as is or change to be similar to md5? >> >> (md5 OBJECT &optional START END CODING-SYSTEM NOERROR) > > Are there any real reasons not to keep the API? If there are good > reasons, let's hear them. If there are no good reasons, my vote is to > keep the API backward-compatible. I think it is better to keep the API. The BINARY arg can be useful. Please review the attached patch. ChangeLog entry omitted for now. Changes due to 'make sync-from-gnulib' aren't included. --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=sha1.diff Content-Description: sha1.diff === modified file 'Makefile.in' --- Makefile.in 2011-05-21 09:53:32 +0000 +++ Makefile.in 2011-05-23 08:00:38 +0000 @@ -331,8 +331,8 @@ # $(gnulib_srcdir) (relative to $(srcdir) and should have build tools # as per $(gnulib_srcdir)/DEPENDENCIES. GNULIB_MODULES = \ - careadlinkat crypto/md5 dtoastr filemode getloadavg getopt-gnu \ - ignore-value intprops lstat mktime readlink \ + careadlinkat crypto/md5 crypto/sha1 dtoastr filemode getloadavg \ + getopt-gnu ignore-value intprops lstat mktime readlink \ socklen stdarg stdio strftime strtoumax symlink sys_stat GNULIB_TOOL_FLAGS = \ --conditional-dependencies --import --no-changelog --no-vc-files \ === modified file 'lisp/bindings.el' --- lisp/bindings.el 2011-04-19 13:44:55 +0000 +++ lisp/bindings.el 2011-05-23 08:02:18 +0000 @@ -646,9 +646,10 @@ (make-variable-buffer-local 'indent-tabs-mode) -;; We have base64 and md5 functions built in now. +;; We have base64, md5 and sha1 functions built in now. (provide 'base64) (provide 'md5) +(provide 'sha1) (provide 'overlay '(display syntax-table field)) (provide 'text-properties '(display syntax-table field point-entered)) === renamed file 'lisp/sha1.el' => 'lisp/obsolete/sha1.el' --- lisp/sha1.el 2011-01-25 04:08:28 +0000 +++ lisp/obsolete/sha1.el 2011-05-23 08:07:18 +0000 @@ -4,6 +4,7 @@ ;; Author: Shuhei KOBAYASHI ;; Keywords: SHA1, FIPS 180-1 +;; Obsolete-since: 24.1 ;; This file is part of GNU Emacs. === modified file 'src/deps.mk' --- src/deps.mk 2011-04-23 10:33:28 +0000 +++ src/deps.mk 2011-05-23 06:31:38 +0000 @@ -284,7 +284,8 @@ floatfns.o: floatfns.c syssignal.h lisp.h globals.h $(config_h) fns.o: fns.c commands.h lisp.h $(config_h) frame.h buffer.h character.h \ keyboard.h keymap.h window.h $(INTERVALS_H) coding.h ../lib/md5.h \ - blockinput.h atimer.h systime.h xterm.h ../lib/unistd.h globals.h + ../lib/sha1.h blockinput.h atimer.h systime.h xterm.h ../lib/unistd.h \ + globals.h print.o: print.c process.h frame.h window.h buffer.h keyboard.h character.h \ lisp.h globals.h $(config_h) termchar.h $(INTERVALS_H) msdos.h termhooks.h \ blockinput.h atimer.h systime.h font.h charset.h coding.h ccl.h \ === modified file 'src/fns.c' --- src/fns.c 2011-05-15 17:17:44 +0000 +++ src/fns.c 2011-05-23 06:20:26 +0000 @@ -4514,42 +4514,17 @@ /************************************************************************ - MD5 + MD5 and SHA1 ************************************************************************/ #include "md5.h" +#include "sha1.h" -DEFUN ("md5", Fmd5, Smd5, 1, 5, 0, - doc: /* Return MD5 message digest of OBJECT, a buffer or string. - -A message digest is a cryptographic checksum of a document, and the -algorithm to calculate it is defined in RFC 1321. - -The two optional arguments START and END are character positions -specifying for which part of OBJECT the message digest should be -computed. If nil or omitted, the digest is computed for the whole -OBJECT. - -The MD5 message digest is computed from the result of encoding the -text in a coding system, not directly from the internal Emacs form of -the text. The optional fourth argument CODING-SYSTEM specifies which -coding system to encode the text with. It should be the same coding -system that you used or will use when actually writing the text into a -file. - -If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If -OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding -system would be chosen by default for writing this text into a file. - -If OBJECT is a string, the most preferred coding system (see the -command `prefer-coding-system') is used. +/* TYPE: 0 for md5, 1 for sha1. */ -If NOERROR is non-nil, silently assume the `raw-text' coding if the -guesswork fails. Normally, an error is signaled in such case. */) - (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror) +Lisp_Object +hash_function (int type, Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror, Lisp_Object binary) { - unsigned char digest[16]; - char value[33]; int i; EMACS_INT size; EMACS_INT size_byte = 0; @@ -4558,6 +4533,7 @@ register EMACS_INT b, e; register struct buffer *bp; EMACS_INT temp; + Lisp_Object res=Qnil; if (STRINGP (object)) { @@ -4728,15 +4704,92 @@ object = code_convert_string (object, coding_system, Qnil, 1, 0, 0); } - md5_buffer (SSDATA (object) + start_byte, - SBYTES (object) - (size_byte - end_byte), - digest); - - for (i = 0; i < 16; i++) - sprintf (&value[2 * i], "%02x", digest[i]); - value[32] = '\0'; + switch (type) + { + case 0: /* MD5 */ + { + unsigned char digest[16]; + md5_buffer (SSDATA (object) + start_byte, + SBYTES (object) - (size_byte - end_byte), + digest); + + if (NILP(binary)) + { + unsigned char value[33]; + for (i = 0; i < 16; i++) + sprintf (&value[2 * i], "%02x", digest[i]); + value[32] = '\0'; + res = make_string (value, 32); + } + else + res = make_string (digest, 16); + break; + } + + case 1: /* SHA1 */ + { + unsigned char digest[20]; + sha1_buffer (SDATA (object) + start_byte, + SBYTES (object) - (size_byte - end_byte), + digest); + if (NILP(binary)) + { + unsigned char value[41]; + for (i = 0; i < 20; i++) + sprintf (&value[2 * i], "%02x", digest[i]); + value[40] = '\0'; + res = make_string (value, 40); + } + else + res = make_string (digest, 20); + break; + } + } + + return res; +} - return make_string (value, 32); +DEFUN ("md5", Fmd5, Smd5, 1, 5, 0, + doc: /* Return MD5 message digest of OBJECT, a buffer or string. + +A message digest is a cryptographic checksum of a document, and the +algorithm to calculate it is defined in RFC 1321. + +The two optional arguments START and END are character positions +specifying for which part of OBJECT the message digest should be +computed. If nil or omitted, the digest is computed for the whole +OBJECT. + +The MD5 message digest is computed from the result of encoding the +text in a coding system, not directly from the internal Emacs form of +the text. The optional fourth argument CODING-SYSTEM specifies which +coding system to encode the text with. It should be the same coding +system that you used or will use when actually writing the text into a +file. + +If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If +OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding +system would be chosen by default for writing this text into a file. + +If OBJECT is a string, the most preferred coding system (see the +command `prefer-coding-system') is used. + +If NOERROR is non-nil, silently assume the `raw-text' coding if the +guesswork fails. Normally, an error is signaled in such case. */) + (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror) +{ + return hash_function (0, object, start, end, coding_system, noerror, Qnil); +} + +DEFUN ("sha1", Fsha1, Ssha1, 1, 4, 0, + doc: /* Return the SHA1 (Secure Hash Algorithm) of an OBJECT. +OBJECT is either a string or a buffer. +Optional arguments BEG and END denote buffer positions for computing +the hash of a portion of OBJECT. +If BINARY is non-nil, return a string in binary form. */) + (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary) +{ + return hash_function (1, object, start, end, Qnil, Qnil, binary); } @@ -4911,6 +4964,7 @@ defsubr (&Sbase64_encode_string); defsubr (&Sbase64_decode_string); defsubr (&Smd5); + defsubr (&Ssha1); defsubr (&Slocale_info); } --=-=-= Content-Type: text/plain Leo --=-=-=--