diff options
| author | Leo Liu | 2011-05-24 16:22:58 +0800 |
|---|---|---|
| committer | Leo Liu | 2011-05-24 16:22:58 +0800 |
| commit | e1b90ef6eca2e32b99fff7ecf14bd1f074046da8 (patch) | |
| tree | 8c55d1013121e1905517168deaa0e6dfd6153782 /src/fns.c | |
| parent | 4ba4c54add7f291e655fb0a5555f7049a9ed17e9 (diff) | |
| download | emacs-e1b90ef6eca2e32b99fff7ecf14bd1f074046da8.tar.gz emacs-e1b90ef6eca2e32b99fff7ecf14bd1f074046da8.zip | |
Implement primitive `sha1' and remove sha1.el
Diffstat (limited to 'src/fns.c')
| -rw-r--r-- | src/fns.c | 129 |
1 files changed, 92 insertions, 37 deletions
| @@ -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 | ||
| 4522 | DEFUN ("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 | |||
| 4525 | A message digest is a cryptographic checksum of a document, and the | ||
| 4526 | algorithm to calculate it is defined in RFC 1321. | ||
| 4527 | |||
| 4528 | The two optional arguments START and END are character positions | ||
| 4529 | specifying for which part of OBJECT the message digest should be | ||
| 4530 | computed. If nil or omitted, the digest is computed for the whole | ||
| 4531 | OBJECT. | ||
| 4532 | 4524 | ||
| 4533 | The MD5 message digest is computed from the result of encoding the | 4525 | Lisp_Object |
| 4534 | text in a coding system, not directly from the internal Emacs form of | 4526 | crypto_hash_function (int type, Lisp_Object object, Lisp_Object start, Lisp_Object end, Lisp_Object coding_system, Lisp_Object noerror, Lisp_Object binary) |
| 4535 | the text. The optional fourth argument CODING-SYSTEM specifies which | ||
| 4536 | coding system to encode the text with. It should be the same coding | ||
| 4537 | system that you used or will use when actually writing the text into a | ||
| 4538 | file. | ||
| 4539 | |||
| 4540 | If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If | ||
| 4541 | OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding | ||
| 4542 | system would be chosen by default for writing this text into a file. | ||
| 4543 | |||
| 4544 | If OBJECT is a string, the most preferred coding system (see the | ||
| 4545 | command `prefer-coding-system') is used. | ||
| 4546 | |||
| 4547 | If NOERROR is non-nil, silently assume the `raw-text' coding if the | ||
| 4548 | guesswork 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 | |||
| 4752 | DEFUN ("md5", Fmd5, Smd5, 1, 5, 0, | ||
| 4753 | doc: /* Return MD5 message digest of OBJECT, a buffer or string. | ||
| 4754 | |||
| 4755 | A message digest is a cryptographic checksum of a document, and the | ||
| 4756 | algorithm to calculate it is defined in RFC 1321. | ||
| 4757 | |||
| 4758 | The two optional arguments START and END are character positions | ||
| 4759 | specifying for which part of OBJECT the message digest should be | ||
| 4760 | computed. If nil or omitted, the digest is computed for the whole | ||
| 4761 | OBJECT. | ||
| 4762 | |||
| 4763 | The MD5 message digest is computed from the result of encoding the | ||
| 4764 | text in a coding system, not directly from the internal Emacs form of | ||
| 4765 | the text. The optional fourth argument CODING-SYSTEM specifies which | ||
| 4766 | coding system to encode the text with. It should be the same coding | ||
| 4767 | system that you used or will use when actually writing the text into a | ||
| 4768 | file. | ||
| 4769 | |||
| 4770 | If CODING-SYSTEM is nil or omitted, the default depends on OBJECT. If | ||
| 4771 | OBJECT is a buffer, the default for CODING-SYSTEM is whatever coding | ||
| 4772 | system would be chosen by default for writing this text into a file. | ||
| 4773 | |||
| 4774 | If OBJECT is a string, the most preferred coding system (see the | ||
| 4775 | command `prefer-coding-system') is used. | ||
| 4776 | |||
| 4777 | If NOERROR is non-nil, silently assume the `raw-text' coding if the | ||
| 4778 | guesswork 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 | |||
| 4784 | DEFUN ("sha1", Fsha1, Ssha1, 1, 4, 0, | ||
| 4785 | doc: /* Return the SHA-1 (Secure Hash Algorithm) of an OBJECT. | ||
| 4786 | |||
| 4787 | OBJECT is either a string or a buffer. Optional arguments START and | ||
| 4788 | END are character positions specifying which portion of OBJECT for | ||
| 4789 | computing the hash. If BINARY is non-nil, return a string in binary | ||
| 4790 | form. */) | ||
| 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 | ||