From: Leo <sdl.web@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: Eli Zaretskii <eliz@gnu.org>, Paul Eggert <eggert@cs.ucla.edu>,
sand@blarg.net, emacs-devel@gnu.org
Subject: Re: Adding sha256 and sha512 to C?
Date: Sat, 11 Jun 2011 13:43:00 +0800 [thread overview]
Message-ID: <m1hb7wapp7.fsf@th041090.ip.tsinghua.edu.cn> (raw)
In-Reply-To: <jwvboyk4yzz.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Mon, 30 May 2011 01:06:10 -0300")
[-- Attachment #1: Type: text/plain, Size: 87 bytes --]
Could people comment on the attached patch which implements sha-2?
Thanks in advance.
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: sha.diff --]
[-- Type: text/x-diff, Size: 6464 bytes --]
=== modified file 'src/deps.mk'
--- src/deps.mk 2011-05-24 08:22:58 +0000
+++ src/deps.mk 2011-06-11 05:29:51 +0000
@@ -284,8 +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 \
- ../lib/sha1.h blockinput.h atimer.h systime.h xterm.h ../lib/unistd.h \
- globals.h
+ ../lib/sha1.h ../lib/sha256.h ../lib/sha512.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-06-07 01:39:26 +0000
+++ src/fns.c 2011-06-11 05:41:20 +0000
@@ -4538,21 +4538,21 @@
\f
/************************************************************************
- MD5 and SHA1
+ MD5, SHA-1, and SHA-2
************************************************************************/
#include "md5.h"
#include "sha1.h"
+#include "sha256.h"
+#include "sha512.h"
/* Convert a possibly-signed character to an unsigned character. This is
a bit safer than casting to unsigned char, since it catches some type
errors that the cast doesn't. */
static inline unsigned char to_uchar (char ch) { return ch; }
-/* TYPE: 0 for md5, 1 for sha1. */
-
static Lisp_Object
-crypto_hash_function (int type, Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror, Lisp_Object binary)
+crypto_hash_function (void (*hash_func) (const char *buffer, size_t len, void *resblock), EMACS_UINT digest_size, Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror, Lisp_Object binary)
{
int i;
EMACS_INT size;
@@ -4562,6 +4562,7 @@
register EMACS_INT b, e;
register struct buffer *bp;
EMACS_INT temp;
+ char *digest;
Lisp_Object res=Qnil;
if (STRINGP (object))
@@ -4733,46 +4734,24 @@
object = code_convert_string (object, coding_system, Qnil, 1, 0, 0);
}
- switch (type)
+ digest = xmalloc(digest_size * sizeof(char));
+
+ hash_func(SSDATA (object) + start_byte,
+ SBYTES (object) - (size_byte - end_byte),
+ digest);
+
+ if (NILP(binary))
{
- case 0: /* MD5 */
- {
- char digest[16];
- md5_buffer (SSDATA (object) + start_byte,
- SBYTES (object) - (size_byte - end_byte),
- digest);
-
- if (NILP (binary))
- {
- char value[33];
- for (i = 0; i < 16; i++)
- sprintf (&value[2 * i], "%02x", to_uchar (digest[i]));
- res = make_string (value, 32);
- }
- else
- res = make_string (digest, 16);
- break;
- }
-
- case 1: /* SHA1 */
- {
- char digest[20];
- sha1_buffer (SSDATA (object) + start_byte,
- SBYTES (object) - (size_byte - end_byte),
- digest);
- if (NILP (binary))
- {
- char value[41];
- for (i = 0; i < 20; i++)
- sprintf (&value[2 * i], "%02x", to_uchar (digest[i]));
- res = make_string (value, 40);
- }
- else
- res = make_string (digest, 20);
- break;
- }
+ char *value = xmalloc((2 * digest_size + 1) * sizeof(char));
+ for (i = 0; i < digest_size; i++)
+ sprintf (&value[2 * i], "%02x", to_uchar (digest[i]));
+ res = make_string(value, digest_size * 2);
+ xfree(value);
}
+ else
+ res = make_string(digest, digest_size);
+ xfree(digest);
return res;
}
@@ -4805,7 +4784,7 @@
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 crypto_hash_function (0, object, start, end, coding_system, noerror, Qnil);
+ return crypto_hash_function ((void *) &md5_buffer, MD5_DIGEST_SIZE, object, start, end, coding_system, noerror, Qnil);
}
DEFUN ("sha1", Fsha1, Ssha1, 1, 4, 0,
@@ -4817,7 +4796,39 @@
form. */)
(Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary)
{
- return crypto_hash_function (1, object, start, end, Qnil, Qnil, binary);
+ return crypto_hash_function ((void *) &sha1_buffer, SHA1_DIGEST_SIZE, object, start, end, Qnil, Qnil, binary);
+}
+
+DEFUN ("sha224", Fsha224, Ssha224, 1, 4, 0,
+ doc: /* Return the SHA-224 (Secure Hash Algorithm) of an OBJECT.
+See `sha1' for explanation of the arguments. */)
+ (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary)
+{
+ return crypto_hash_function ((void *) &sha224_buffer, SHA224_DIGEST_SIZE, object, start, end, Qnil, Qnil, binary);
+}
+
+DEFUN ("sha256", Fsha256, Ssha256, 1, 4, 0,
+ doc: /* Return the SHA-256 (Secure Hash Algorithm) of an OBJECT.
+See `sha1' for explanation of the arguments. */)
+ (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary)
+{
+ return crypto_hash_function ((void *) &sha256_buffer, SHA256_DIGEST_SIZE, object, start, end, Qnil, Qnil, binary);
+}
+
+DEFUN ("sha384", Fsha384, Ssha384, 1, 4, 0,
+ doc: /* Return the SHA-384 (Secure Hash Algorithm) of an OBJECT.
+See `sha1' for explanation of the arguments. */)
+ (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary)
+{
+ return crypto_hash_function ((void *) &sha384_buffer, SHA384_DIGEST_SIZE, object, start, end, Qnil, Qnil, binary);
+}
+
+DEFUN ("sha512", Fsha512, Ssha512, 1, 4, 0,
+ doc: /* Return the SHA-512 (Secure Hash Algorithm) of an OBJECT.
+See `sha1' for explanation of the arguments. */)
+ (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary)
+{
+ return crypto_hash_function ((void *) &sha512_buffer, SHA512_DIGEST_SIZE, object, start, end, Qnil, Qnil, binary);
}
\f
@@ -4993,6 +5004,10 @@
defsubr (&Sbase64_decode_string);
defsubr (&Smd5);
defsubr (&Ssha1);
+ defsubr (&Ssha224);
+ defsubr (&Ssha256);
+ defsubr (&Ssha384);
+ defsubr (&Ssha512);
defsubr (&Slocale_info);
}
=== modified file 'src/makefile.w32-in'
--- src/makefile.w32-in 2011-05-31 17:03:24 +0000
+++ src/makefile.w32-in 2011-06-11 05:30:23 +0000
@@ -869,6 +869,8 @@
$(EMACS_ROOT)/nt/inc/sys/time.h \
$(EMACS_ROOT)/lib/md5.h \
$(EMACS_ROOT)/lib/sha1.h \
+ $(EMACS_ROOT)/lib/sha256.h \
+ $(EMACS_ROOT)/lib/sha512.h \
$(LISP_H) \
$(SRC)/atimer.h \
$(SRC)/blockinput.h \
next prev parent reply other threads:[~2011-06-11 5:43 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-05-28 3:18 Adding sha256 and sha512 to C? sand
2011-05-28 3:58 ` Paul Eggert
2011-05-28 7:25 ` Eli Zaretskii
2011-05-30 4:06 ` Stefan Monnier
2011-06-11 5:43 ` Leo [this message]
2011-06-11 8:00 ` Eli Zaretskii
2011-06-11 12:37 ` Leo
2011-06-11 15:24 ` Eli Zaretskii
2011-06-11 16:02 ` Paul Eggert
2011-06-11 20:36 ` Juanma Barranquero
2011-06-12 0:34 ` YAMAMOTO Mitsuharu
2011-06-12 13:03 ` Leo
2011-06-12 14:05 ` Thien-Thi Nguyen
2011-06-12 15:48 ` Deniz Dogan
2011-06-12 17:06 ` Richard Riley
2011-06-12 22:37 ` Paul Eggert
2011-06-19 16:08 ` Leo
2011-05-29 4:22 ` Leo
2011-05-29 5:18 ` Paul Eggert
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m1hb7wapp7.fsf@th041090.ip.tsinghua.edu.cn \
--to=sdl.web@gmail.com \
--cc=eggert@cs.ucla.edu \
--cc=eliz@gnu.org \
--cc=emacs-devel@gnu.org \
--cc=monnier@iro.umontreal.ca \
--cc=sand@blarg.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).