Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(24)

Side by Side Diff: Modules/sha256module.c

Issue 20173: Derby #4: Convert 53 sites to Argument Clinic across 5 files
Patch Set: Created 6 years, 1 month ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* SHA256 module */ 1 /* SHA256 module */
2 2
3 /* This module provides an interface to NIST's SHA-256 and SHA-224 Algorithms */ 3 /* This module provides an interface to NIST's SHA-256 and SHA-224 Algorithms */
4 4
5 /* See below for information about the original code this module was 5 /* See below for information about the original code this module was
6 based upon. Additional work performed by: 6 based upon. Additional work performed by:
7 7
8 Andrew Kuchling (amk@amk.ca) 8 Andrew Kuchling (amk@amk.ca)
9 Greg Stein (gstein@lyra.org) 9 Greg Stein (gstein@lyra.org)
10 Trevor Perrin (trevp@trevp.net) 10 Trevor Perrin (trevp@trevp.net)
(...skipping 17 matching lines...) Expand all
28 #if SIZEOF_INT == 4 28 #if SIZEOF_INT == 4
29 typedef unsigned int SHA_INT32; /* 32-bit integer */ 29 typedef unsigned int SHA_INT32; /* 32-bit integer */
30 #else 30 #else
31 /* not defined. compilation will die. */ 31 /* not defined. compilation will die. */
32 #endif 32 #endif
33 33
34 /* The SHA block size and message digest sizes, in bytes */ 34 /* The SHA block size and message digest sizes, in bytes */
35 35
36 #define SHA_BLOCKSIZE 64 36 #define SHA_BLOCKSIZE 64
37 #define SHA_DIGESTSIZE 32 37 #define SHA_DIGESTSIZE 32
38
39 /*[clinic input]
40 module _sha256
41 class _sha256.SHA256
larry 2014/01/10 12:58:57 The name of the class is SHA256Type.
42 [clinic start generated code]*/
43 /*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709] */
44 /*[python input]
45 class SHAobject_converter(self_converter):
46 type = "SHAobject *"
47 [python start generated code]*/
48 /*[python end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709] */
38 49
39 /* The structure for storing SHA info */ 50 /* The structure for storing SHA info */
40 51
41 typedef struct { 52 typedef struct {
42 PyObject_HEAD 53 PyObject_HEAD
43 SHA_INT32 digest[8]; /* Message digest */ 54 SHA_INT32 digest[8]; /* Message digest */
44 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */ 55 SHA_INT32 count_lo, count_hi; /* 64-bit bit count */
45 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */ 56 SHA_BYTE data[SHA_BLOCKSIZE]; /* SHA data buffer */
46 int local; /* unprocessed amount in data */ 57 int local; /* unprocessed amount in data */
47 int digestsize; 58 int digestsize;
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after
455 hex_digest[j++] = Py_hexdigits[c]; 466 hex_digest[j++] = Py_hexdigits[c];
456 c = (digest[i] & 0xf); 467 c = (digest[i] & 0xf);
457 hex_digest[j++] = Py_hexdigits[c]; 468 hex_digest[j++] = Py_hexdigits[c];
458 } 469 }
459 #ifdef Py_DEBUG 470 #ifdef Py_DEBUG
460 assert(_PyUnicode_CheckConsistency(retval, 1)); 471 assert(_PyUnicode_CheckConsistency(retval, 1));
461 #endif 472 #endif
462 return retval; 473 return retval;
463 } 474 }
464 475
465 PyDoc_STRVAR(SHA256_update__doc__, 476 /*[clinic input]
466 "Update this hash object's state with the provided string."); 477 _sha256.SHA256.update
478 self: SHAobject
479 obj: object
480 /
481
482 Update this hash object's state with the provided string.
483 [clinic start generated code]*/
484
485 PyDoc_STRVAR(_sha256_SHA256_update__doc__,
486 "update(obj)\n"
487 "Update this hash object\'s state with the provided string.");
488
489 #define _SHA256_SHA256_UPDATE_METHODDEF \
490 {"update", (PyCFunction)_sha256_SHA256_update, METH_O, _sha256_SHA256_update __doc__},
467 491
468 static PyObject * 492 static PyObject *
469 SHA256_update(SHAobject *self, PyObject *args) 493 _sha256_SHA256_update(SHAobject *self, PyObject *obj)
494 /*[clinic end generated code: checksum=52c21735e19c8d9455303732eda5e5c1c7adb60e] */
470 { 495 {
471 PyObject *obj;
472 Py_buffer buf; 496 Py_buffer buf;
473
474 if (!PyArg_ParseTuple(args, "O:update", &obj))
475 return NULL;
476 497
477 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf); 498 GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
478 499
479 sha_update(self, buf.buf, buf.len); 500 sha_update(self, buf.buf, buf.len);
480 501
481 PyBuffer_Release(&buf); 502 PyBuffer_Release(&buf);
482 Py_INCREF(Py_None); 503 Py_INCREF(Py_None);
483 return Py_None; 504 return Py_None;
484 } 505 }
485 506
486 static PyMethodDef SHA_methods[] = { 507 static PyMethodDef SHA_methods[] = {
487 {"copy", (PyCFunction)SHA256_copy, METH_NOARGS, SHA256_copy__doc_ _}, 508 {"copy", (PyCFunction)SHA256_copy, METH_NOARGS, SHA256_copy__doc_ _},
488 {"digest", (PyCFunction)SHA256_digest, METH_NOARGS, SHA256_digest__do c__}, 509 {"digest", (PyCFunction)SHA256_digest, METH_NOARGS, SHA256_digest__do c__},
489 {"hexdigest", (PyCFunction)SHA256_hexdigest, METH_NOARGS, SHA256_hexdigest_ _doc__}, 510 {"hexdigest", (PyCFunction)SHA256_hexdigest, METH_NOARGS, SHA256_hexdigest_ _doc__},
490 {"update", (PyCFunction)SHA256_update, METH_VARARGS, SHA256_update__do c__}, 511 _SHA256_SHA256_UPDATE_METHODDEF
491 {NULL, NULL} /* sentinel */ 512 {NULL, NULL} /* sentinel */
492 }; 513 };
493 514
494 static PyObject * 515 static PyObject *
495 SHA256_get_block_size(PyObject *self, void *closure) 516 SHA256_get_block_size(PyObject *self, void *closure)
496 { 517 {
497 return PyLong_FromLong(SHA_BLOCKSIZE); 518 return PyLong_FromLong(SHA_BLOCKSIZE);
498 } 519 }
499 520
500 static PyObject * 521 static PyObject *
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
587 0, /*tp_iter*/ 608 0, /*tp_iter*/
588 0, /*tp_iternext*/ 609 0, /*tp_iternext*/
589 SHA_methods, /* tp_methods */ 610 SHA_methods, /* tp_methods */
590 SHA_members, /* tp_members */ 611 SHA_members, /* tp_members */
591 SHA_getseters, /* tp_getset */ 612 SHA_getseters, /* tp_getset */
592 }; 613 };
593 614
594 615
595 /* The single module-level function: new() */ 616 /* The single module-level function: new() */
596 617
597 PyDoc_STRVAR(SHA256_new__doc__, 618 /*[clinic input]
619 _sha256.SHA256.sha256
larry 2014/01/10 12:58:57 This should just be _sha256.sha256. It's a functi
620
621 string: object = None
larry 2014/01/10 12:58:57 Use a default of NULL here.
622
623 Return a new SHA-256 hash object; optionally initialized with a string.
624 [clinic start generated code]*/
625
626 PyDoc_STRVAR(_sha256_SHA256_sha256__doc__,
627 "sha256(string=None)\n"
598 "Return a new SHA-256 hash object; optionally initialized with a string."); 628 "Return a new SHA-256 hash object; optionally initialized with a string.");
599 629
630 #define _SHA256_SHA256_SHA256_METHODDEF \
631 {"sha256", (PyCFunction)_sha256_SHA256_sha256, METH_VARARGS|METH_KEYWORDS, _ sha256_SHA256_sha256__doc__},
632
600 static PyObject * 633 static PyObject *
601 SHA256_new(PyObject *self, PyObject *args, PyObject *kwdict) 634 _sha256_SHA256_sha256_impl(PyObject *self, PyObject *string);
635
636 static PyObject *
637 _sha256_SHA256_sha256(PyObject *self, PyObject *args, PyObject *kwargs)
602 { 638 {
603 static char *kwlist[] = {"string", NULL}; 639 PyObject *return_value = NULL;
640 static char *_keywords[] = {"string", NULL};
641 PyObject *string = Py_None;
642
643 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
644 "|O:sha256", _keywords,
645 &string))
646 goto exit;
647 return_value = _sha256_SHA256_sha256_impl(self, string);
648
649 exit:
650 return return_value;
651 }
652
653 static PyObject *
654 _sha256_SHA256_sha256_impl(PyObject *self, PyObject *string)
655 /*[clinic end generated code: checksum=c3c1d266f9e3196c16cad4bf2711fb78f5637f8a] */
656 {
604 SHAobject *new; 657 SHAobject *new;
605 PyObject *data_obj = NULL;
606 Py_buffer buf; 658 Py_buffer buf;
607 659
608 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist, 660 if (string != Py_None)
609 &data_obj)) { 661 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
610 return NULL;
611 }
612
613 if (data_obj)
614 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
615 662
616 if ((new = newSHA256object()) == NULL) { 663 if ((new = newSHA256object()) == NULL) {
617 if (data_obj) 664 if (string != Py_None)
larry 2014/01/10 12:58:57 Once you use a default of NULL for string, you can
618 PyBuffer_Release(&buf); 665 PyBuffer_Release(&buf);
619 return NULL; 666 return NULL;
620 } 667 }
621 668
622 sha_init(new); 669 sha_init(new);
623 670
624 if (PyErr_Occurred()) { 671 if (PyErr_Occurred()) {
625 Py_DECREF(new); 672 Py_DECREF(new);
626 if (data_obj) 673 if (string != Py_None)
627 PyBuffer_Release(&buf); 674 PyBuffer_Release(&buf);
628 return NULL; 675 return NULL;
629 } 676 }
630 if (data_obj) { 677 if (string != Py_None) {
631 sha_update(new, buf.buf, buf.len); 678 sha_update(new, buf.buf, buf.len);
632 PyBuffer_Release(&buf); 679 PyBuffer_Release(&buf);
633 } 680 }
634 681
635 return (PyObject *)new; 682 return (PyObject *)new;
636 } 683 }
637 684
638 PyDoc_STRVAR(SHA224_new__doc__, 685 /*[clinic input]
686 _sha256.SHA256.sha224
larry 2014/01/10 12:58:57 This should just be _sha256.sha224. It's a functi
687
688 string: object = None
larry 2014/01/10 12:58:57 Use a default of NULL here.
689
690 Return a new SHA-224 hash object; optionally initialized with a string.
691 [clinic start generated code]*/
692
693 PyDoc_STRVAR(_sha256_SHA256_sha224__doc__,
694 "sha224(string=None)\n"
639 "Return a new SHA-224 hash object; optionally initialized with a string."); 695 "Return a new SHA-224 hash object; optionally initialized with a string.");
640 696
697 #define _SHA256_SHA256_SHA224_METHODDEF \
698 {"sha224", (PyCFunction)_sha256_SHA256_sha224, METH_VARARGS|METH_KEYWORDS, _ sha256_SHA256_sha224__doc__},
699
641 static PyObject * 700 static PyObject *
642 SHA224_new(PyObject *self, PyObject *args, PyObject *kwdict) 701 _sha256_SHA256_sha224_impl(PyObject *self, PyObject *string);
702
703 static PyObject *
704 _sha256_SHA256_sha224(PyObject *self, PyObject *args, PyObject *kwargs)
643 { 705 {
644 static char *kwlist[] = {"string", NULL}; 706 PyObject *return_value = NULL;
707 static char *_keywords[] = {"string", NULL};
708 PyObject *string = Py_None;
709
710 if (!PyArg_ParseTupleAndKeywords(args, kwargs,
711 "|O:sha224", _keywords,
712 &string))
713 goto exit;
714 return_value = _sha256_SHA256_sha224_impl(self, string);
715
716 exit:
717 return return_value;
718 }
719
720 static PyObject *
721 _sha256_SHA256_sha224_impl(PyObject *self, PyObject *string)
722 /*[clinic end generated code: checksum=5954d185599991b04b593b6e0cec82cae05c2bad] */
723 {
645 SHAobject *new; 724 SHAobject *new;
646 PyObject *data_obj = NULL;
647 Py_buffer buf; 725 Py_buffer buf;
648 726
649 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist, 727 if (string != Py_None)
larry 2014/01/10 12:58:57 Once you use a default of NULL for "string", you c
650 &data_obj)) { 728 GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
651 return NULL;
652 }
653
654 if (data_obj)
655 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);
656 729
657 if ((new = newSHA224object()) == NULL) { 730 if ((new = newSHA224object()) == NULL) {
658 if (data_obj) 731 if (string != Py_None)
659 PyBuffer_Release(&buf); 732 PyBuffer_Release(&buf);
660 return NULL; 733 return NULL;
661 } 734 }
662 735
663 sha224_init(new); 736 sha224_init(new);
664 737
665 if (PyErr_Occurred()) { 738 if (PyErr_Occurred()) {
666 Py_DECREF(new); 739 Py_DECREF(new);
667 if (data_obj) 740 if (string != Py_None)
668 PyBuffer_Release(&buf); 741 PyBuffer_Release(&buf);
669 return NULL; 742 return NULL;
670 } 743 }
671 if (data_obj) { 744 if (string != Py_None) {
672 sha_update(new, buf.buf, buf.len); 745 sha_update(new, buf.buf, buf.len);
673 PyBuffer_Release(&buf); 746 PyBuffer_Release(&buf);
674 } 747 }
675 748
676 return (PyObject *)new; 749 return (PyObject *)new;
677 } 750 }
678 751
679 752
680 /* List of functions exported by this module */ 753 /* List of functions exported by this module */
681 754
682 static struct PyMethodDef SHA_functions[] = { 755 static struct PyMethodDef SHA_functions[] = {
683 {"sha256", (PyCFunction)SHA256_new, METH_VARARGS|METH_KEYWORDS, SHA256_new__ doc__}, 756 _SHA256_SHA256_SHA256_METHODDEF
684 {"sha224", (PyCFunction)SHA224_new, METH_VARARGS|METH_KEYWORDS, SHA224_new__ doc__}, 757 _SHA256_SHA256_SHA224_METHODDEF
685 {NULL, NULL} /* Sentinel */ 758 {NULL, NULL} /* Sentinel */
686 }; 759 };
687 760
688 761
689 /* Initialize this module. */ 762 /* Initialize this module. */
690 763
691 #define insint(n,v) { PyModule_AddIntConstant(m,n,v); } 764 #define insint(n,v) { PyModule_AddIntConstant(m,n,v); }
692 765
693 766
694 static struct PyModuleDef _sha256module = { 767 static struct PyModuleDef _sha256module = {
(...skipping 24 matching lines...) Expand all
719 if (m == NULL) 792 if (m == NULL)
720 return NULL; 793 return NULL;
721 794
722 Py_INCREF((PyObject *)&SHA224type); 795 Py_INCREF((PyObject *)&SHA224type);
723 PyModule_AddObject(m, "SHA224Type", (PyObject *)&SHA224type); 796 PyModule_AddObject(m, "SHA224Type", (PyObject *)&SHA224type);
724 Py_INCREF((PyObject *)&SHA256type); 797 Py_INCREF((PyObject *)&SHA256type);
725 PyModule_AddObject(m, "SHA256Type", (PyObject *)&SHA256type); 798 PyModule_AddObject(m, "SHA256Type", (PyObject *)&SHA256type);
726 return m; 799 return m;
727 800
728 } 801 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+