emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Adding sha256 and sha512 to C?


From: Leo
Subject: Re: Adding sha256 and sha512 to C?
Date: Sat, 11 Jun 2011 13:43:00 +0800
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3.50 (Mac OS X 10.6.7)

Could people comment on the attached patch which implements sha-2?
Thanks in advance.

=== 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 @@
 
 
 /************************************************************************
-                            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);
 }
 
 
@@ -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 \


reply via email to

[Prev in Thread] Current Thread [Next in Thread]