summaryrefslogtreecommitdiffstats
path: root/Modules/_blake2/blake2module.c
diff options
context:
space:
mode:
authorChristian Heimes <christian@python.org>2016-09-06 20:03:25 (GMT)
committerChristian Heimes <christian@python.org>2016-09-06 20:03:25 (GMT)
commit121b9487d13b7dcd5a864a6067c4a5c88ba15b5d (patch)
treeb2ac9866afda319cb6582de045c47f1a023d1973 /Modules/_blake2/blake2module.c
parent5d75f441ef0a3cfe7af0d865db2530528e424818 (diff)
downloadcpython-121b9487d13b7dcd5a864a6067c4a5c88ba15b5d.zip
cpython-121b9487d13b7dcd5a864a6067c4a5c88ba15b5d.tar.gz
cpython-121b9487d13b7dcd5a864a6067c4a5c88ba15b5d.tar.bz2
Issue #26798: Add BLAKE2 (blake2b and blake2s) to hashlib.
Diffstat (limited to 'Modules/_blake2/blake2module.c')
-rw-r--r--Modules/_blake2/blake2module.c105
1 files changed, 105 insertions, 0 deletions
diff --git a/Modules/_blake2/blake2module.c b/Modules/_blake2/blake2module.c
new file mode 100644
index 0000000..e2a3d42
--- /dev/null
+++ b/Modules/_blake2/blake2module.c
@@ -0,0 +1,105 @@
+/*
+ * Written in 2013 by Dmitry Chestnykh <dmitry@codingrobots.com>
+ * Modified for CPython by Christian Heimes <christian@python.org>
+ *
+ * To the extent possible under law, the author have dedicated all
+ * copyright and related and neighboring rights to this software to
+ * the public domain worldwide. This software is distributed without
+ * any warranty. http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+#include "Python.h"
+
+#include "impl/blake2.h"
+
+extern PyTypeObject PyBlake2_BLAKE2bType;
+extern PyTypeObject PyBlake2_BLAKE2sType;
+
+
+PyDoc_STRVAR(blake2mod__doc__,
+"_blake2b provides BLAKE2b for hashlib\n"
+);
+
+
+static struct PyMethodDef blake2mod_functions[] = {
+ {NULL, NULL}
+};
+
+static struct PyModuleDef blake2_module = {
+ PyModuleDef_HEAD_INIT,
+ "_blake2",
+ blake2mod__doc__,
+ -1,
+ blake2mod_functions,
+ NULL,
+ NULL,
+ NULL,
+ NULL
+};
+
+#define ADD_INT(d, name, value) do { \
+ PyObject *x = PyLong_FromLong(value); \
+ if (!x) { \
+ Py_DECREF(m); \
+ return NULL; \
+ } \
+ if (PyDict_SetItemString(d, name, x) < 0) { \
+ Py_DECREF(m); \
+ return NULL; \
+ } \
+ Py_DECREF(x); \
+} while(0)
+
+
+PyMODINIT_FUNC
+PyInit__blake2(void)
+{
+ PyObject *m;
+ PyObject *d;
+
+ m = PyModule_Create(&blake2_module);
+ if (m == NULL)
+ return NULL;
+
+ /* BLAKE2b */
+ Py_TYPE(&PyBlake2_BLAKE2bType) = &PyType_Type;
+ if (PyType_Ready(&PyBlake2_BLAKE2bType) < 0) {
+ return NULL;
+ }
+
+ Py_INCREF(&PyBlake2_BLAKE2bType);
+ PyModule_AddObject(m, "blake2b", (PyObject *)&PyBlake2_BLAKE2bType);
+
+ d = PyBlake2_BLAKE2bType.tp_dict;
+ ADD_INT(d, "SALT_SIZE", BLAKE2B_SALTBYTES);
+ ADD_INT(d, "PERSON_SIZE", BLAKE2B_PERSONALBYTES);
+ ADD_INT(d, "MAX_KEY_SIZE", BLAKE2B_KEYBYTES);
+ ADD_INT(d, "MAX_DIGEST_SIZE", BLAKE2B_OUTBYTES);
+
+ PyModule_AddIntConstant(m, "BLAKE2B_SALT_SIZE", BLAKE2B_SALTBYTES);
+ PyModule_AddIntConstant(m, "BLAKE2B_PERSON_SIZE", BLAKE2B_PERSONALBYTES);
+ PyModule_AddIntConstant(m, "BLAKE2B_MAX_KEY_SIZE", BLAKE2B_KEYBYTES);
+ PyModule_AddIntConstant(m, "BLAKE2B_MAX_DIGEST_SIZE", BLAKE2B_OUTBYTES);
+
+ /* BLAKE2s */
+ Py_TYPE(&PyBlake2_BLAKE2sType) = &PyType_Type;
+ if (PyType_Ready(&PyBlake2_BLAKE2sType) < 0) {
+ return NULL;
+ }
+
+ Py_INCREF(&PyBlake2_BLAKE2sType);
+ PyModule_AddObject(m, "blake2s", (PyObject *)&PyBlake2_BLAKE2sType);
+
+ d = PyBlake2_BLAKE2sType.tp_dict;
+ ADD_INT(d, "SALT_SIZE", BLAKE2S_SALTBYTES);
+ ADD_INT(d, "PERSON_SIZE", BLAKE2S_PERSONALBYTES);
+ ADD_INT(d, "MAX_KEY_SIZE", BLAKE2S_KEYBYTES);
+ ADD_INT(d, "MAX_DIGEST_SIZE", BLAKE2S_OUTBYTES);
+
+ PyModule_AddIntConstant(m, "BLAKE2S_SALT_SIZE", BLAKE2S_SALTBYTES);
+ PyModule_AddIntConstant(m, "BLAKE2S_PERSON_SIZE", BLAKE2S_PERSONALBYTES);
+ PyModule_AddIntConstant(m, "BLAKE2S_MAX_KEY_SIZE", BLAKE2S_KEYBYTES);
+ PyModule_AddIntConstant(m, "BLAKE2S_MAX_DIGEST_SIZE", BLAKE2S_OUTBYTES);
+
+ return m;
+}