aboutsummaryrefslogtreecommitdiffstats
path: root/src/fns.c
diff options
context:
space:
mode:
authorLeo Liu2011-05-24 16:22:58 +0800
committerLeo Liu2011-05-24 16:22:58 +0800
commite1b90ef6eca2e32b99fff7ecf14bd1f074046da8 (patch)
tree8c55d1013121e1905517168deaa0e6dfd6153782 /src/fns.c
parent4ba4c54add7f291e655fb0a5555f7049a9ed17e9 (diff)
downloademacs-e1b90ef6eca2e32b99fff7ecf14bd1f074046da8.tar.gz
emacs-e1b90ef6eca2e32b99fff7ecf14bd1f074046da8.zip
Implement primitive `sha1' and remove sha1.el
Diffstat (limited to 'src/fns.c')
-rw-r--r--src/fns.c129
1 files changed, 92 insertions, 37 deletions
diff --git a/src/fns.c b/src/fns.c
index 16dc0fe0de2..9d73e48b928 100644
--- a/src/fns.c
+++ b/src/fns.c
@@ -4514,42 +4514,17 @@ including negative integers. */)
4514 4514
4515 4515
4516/************************************************************************ 4516/************************************************************************
4517 MD5 4517 MD5 and SHA1
4518 ************************************************************************/ 4518 ************************************************************************/
4519 4519
4520#include "md5.h" 4520#include "md5.h"
4521#include "sha1.h"
4521 4522
4522DEFUN ("md5", Fmd5, Smd5, 1, 5, 0, 4523/* TYPE: 0 for md5, 1 for sha1. */
4523 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
4524
4525A message digest is a cryptographic checksum of a document, and the
4526algorithm to calculate it is defined in RFC 1321.
4527
4528The two optional arguments START and END are character positions
4529specifying for which part of OBJECT the message digest should be
4530computed. If nil or omitted, the digest is computed for the whole
4531OBJECT.
4532 4524
4533The MD5 message digest is computed from the result of encoding the 4525Lisp_Object
4534text in a coding system, not directly from the internal Emacs form of 4526crypto_hash_function (int type, Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror, Lisp_Object binary)
4535the text. The optional fourth argument CODING-SYSTEM specifies which
4536coding system to encode the text with. It should be the same coding
4537system that you used or will use when actually writing the text into a
4538file.
4539
4540If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
4541OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
4542system would be chosen by default for writing this text into a file.
4543
4544If OBJECT is a string, the most preferred coding system (see the
4545command `prefer-coding-system') is used.
4546
4547If NOERROR is non-nil, silently assume the `raw-text' coding if the
4548guesswork fails. Normally, an error is signaled in such case. */)
4549 (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror)
4550{ 4527{
4551 unsigned char digest[16];
4552 char value[33];
4553 int i; 4528 int i;
4554 EMACS_INT size; 4529 EMACS_INT size;
4555 EMACS_INT size_byte = 0; 4530 EMACS_INT size_byte = 0;
@@ -4558,6 +4533,7 @@ guesswork fails. Normally, an error is signaled in such case. */)
4558 register EMACS_INT b, e; 4533 register EMACS_INT b, e;
4559 register struct buffer *bp; 4534 register struct buffer *bp;
4560 EMACS_INT temp; 4535 EMACS_INT temp;
4536 Lisp_Object res=Qnil;
4561 4537
4562 if (STRINGP (object)) 4538 if (STRINGP (object))
4563 { 4539 {
@@ -4728,15 +4704,93 @@ guesswork fails. Normally, an error is signaled in such case. */)
4728 object = code_convert_string (object, coding_system, Qnil, 1, 0, 0); 4704 object = code_convert_string (object, coding_system, Qnil, 1, 0, 0);
4729 } 4705 }
4730 4706
4731 md5_buffer (SSDATA (object) + start_byte, 4707 switch (type)
4732 SBYTES (object) - (size_byte - end_byte), 4708 {
4733 digest); 4709 case 0: /* MD5 */
4710 {
4711 unsigned char digest[16];
4712 md5_buffer (SSDATA (object) + start_byte,
4713 SBYTES (object) - (size_byte - end_byte),
4714 digest);
4734 4715
4735 for (i = 0; i < 16; i++) 4716 if (NILP(binary))
4736 sprintf (&value[2 * i], "%02x", digest[i]); 4717 {
4737 value[32] = '\0'; 4718 unsigned char value[33];
4719 for (i = 0; i < 16; i++)
4720 sprintf (&value[2 * i], "%02x", digest[i]);
4721 value[32] = '\0';
4722 res = make_string (value, 32);
4723 }
4724 else
4725 res = make_string (digest, 16);
4726 break;
4727 }
4738 4728
4739 return make_string (value, 32); 4729 case 1: /* SHA1 */
4730 {
4731 unsigned char digest[20];
4732 sha1_buffer (SDATA (object) + start_byte,
4733 SBYTES (object) - (size_byte - end_byte),
4734 digest);
4735 if (NILP(binary))
4736 {
4737 unsigned char value[41];
4738 for (i = 0; i < 20; i++)
4739 sprintf (&value[2 * i], "%02x", digest[i]);
4740 value[40] = '\0';
4741 res = make_string (value, 40);
4742 }
4743 else
4744 res = make_string (digest, 20);
4745 break;
4746 }
4747 }
4748
4749 return res;
4750}
4751
4752DEFUN ("md5", Fmd5, Smd5, 1, 5, 0,
4753 doc: /* Return MD5 message digest of OBJECT, a buffer or string.
4754
4755A message digest is a cryptographic checksum of a document, and the
4756algorithm to calculate it is defined in RFC 1321.
4757
4758The two optional arguments START and END are character positions
4759specifying for which part of OBJECT the message digest should be
4760computed. If nil or omitted, the digest is computed for the whole
4761OBJECT.
4762
4763The MD5 message digest is computed from the result of encoding the
4764text in a coding system, not directly from the internal Emacs form of
4765the text. The optional fourth argument CODING-SYSTEM specifies which
4766coding system to encode the text with. It should be the same coding
4767system that you used or will use when actually writing the text into a
4768file.
4769
4770If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If
4771OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding
4772system would be chosen by default for writing this text into a file.
4773
4774If OBJECT is a string, the most preferred coding system (see the
4775command `prefer-coding-system') is used.
4776
4777If NOERROR is non-nil, silently assume the `raw-text' coding if the
4778guesswork fails. Normally, an error is signaled in such case. */)
4779 (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror)
4780{
4781 return crypto_hash_function (0, object, start, end, coding_system, noerror, Qnil);
4782}
4783
4784DEFUN ("sha1", Fsha1, Ssha1, 1, 4, 0,
4785 doc: /* Return the SHA-1 (Secure Hash Algorithm) of an OBJECT.
4786
4787OBJECT is either a string or a buffer. Optional arguments START and
4788END are character positions specifying which portion of OBJECT for
4789computing the hash. If BINARY is non-nil, return a string in binary
4790form. */)
4791 (Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object binary)
4792{
4793 return crypto_hash_function (1, object, start, end, Qnil, Qnil, binary);
4740} 4794}
4741 4795
4742 4796
@@ -4911,6 +4965,7 @@ this variable. */);
4911 defsubr (&Sbase64_encode_string); 4965 defsubr (&Sbase64_encode_string);
4912 defsubr (&Sbase64_decode_string); 4966 defsubr (&Sbase64_decode_string);
4913 defsubr (&Smd5); 4967 defsubr (&Smd5);
4968 defsubr (&Ssha1);
4914 defsubr (&Slocale_info); 4969 defsubr (&Slocale_info);
4915} 4970}
4916 4971