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: Paul Eggert
Subject: Re: Adding sha256 and sha512 to C?
Date: Sun, 12 Jun 2011 15:37:39 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110516 Thunderbird/3.1.10

On 06/12/11 06:03, Leo wrote:

> (sha OBJECT &optional START END BINARY ALGORITHM)
> 
> where ALGORITHM can be 1 (default), 224, 256, 384, 512, and make sha1
> obsolete? In a sense we unify all SHA functions and leave MD5 as is.

That's better, thanks, but I still have two qualms.  First, the name
"sha" is confusing at the Emacs Lisp level: it feels too much like
"ash".  It's not like programmers will be using crypto functions in
every expression; their names need not be *that* short.  How about the
name "secure-hash" instead?  That's pretty short.

Second, naming algorithms via bit counts doesn't sound
forward-looking.  SHA-3 is likely to have a 512-bit variant, for
example.  How about using atoms to name the algorithms, e.g., SHA-1,
SHA-224, SHA-256, etc.?  This is more likely to be robust after SHA-3
comes out, not to mention SHA-4 etc.

+      hash_func          = &md5_buffer;

There's no need for the "&" here, or in similar assignments to
hash_func.  (And there's no need for multiple spaces before the "=".)

+  digest = make_uninit_string (digest_size);
...
+      Lisp_Object value = make_uninit_string (2 * digest_size);

There's no need to call make_uninit_string twice, as only one
string is being returned.  Any temporary buffer for the digest can
be put into the C stack.  Or, perhaps better, use the same
uninitialized string for both the binary digest and the text
digest, and run the binary-to-text loop backwards (and without
using sprintf) so that the loop doesn't stomp on its own work.
Something like this:

      unsigned char *p = SDATA (digest);
      for (i = digest_size - 1; i >= 0; i--)
        {
          static char const hexdigit[16] = "0123456789abcdef";
          int p_i = p[i];
          p[2 * i] = hexdigit[p_i >> 4];
          p[2 * i + 1] = hexdigit[p_i & 0xf];
        }

The text-vs-binary checksum thing seems to be enough of a hassle that
perhaps it should be pulled out into a separate function, rather than
as a flag to the sha/secure-hash function.  That is, secure-hash could
always return the text form, and if someone wants a binary form they
could call the text-to-binary converter.

Won't there need to be changes to the Emacs Lisp reference manual, and
to NEWS?




reply via email to

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