summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDwayne Litzenberger <dlitz@dlitz.net>2013-02-17 11:21:38 -0800
committerDwayne Litzenberger <dlitz@dlitz.net>2013-02-17 20:07:02 -0800
commitfd398a28e3a227a539b264a9f1e11287b904c7da (patch)
tree9f1628ef88c17604f55ec0ad652b0e1fb3959f38 /src
parent0d8ea5ff1607a3d7ae544667bff99229954484ff (diff)
downloadpycrypto-fd398a28e3a227a539b264a9f1e11287b904c7da.tar.gz
Hash: Speed up initialization by removing pure-Python wrappershash-speedup-wip
The pure Python wrappers around Crypto.Hash.* were convenient, but they slowed down hash initialization by 4-7x. There is a speed trade-off here: The MD5 and SHA1 objects are just wrapped hashlib objects (or old-style md5/sha objects). To maintain API compatibility with the rest of PyCrypto, we still have to wrap them, so they're slower to initialize than the rest of the hash functions. If hashlib ever adds a .new() method, we will automatically use hashlib directly and gain the initialization speed-up.
Diffstat (limited to 'src')
-rw-r--r--src/MD2.c29
-rw-r--r--src/MD4.c20
-rw-r--r--src/RIPEMD160.c23
-rw-r--r--src/SHA224.c19
-rw-r--r--src/SHA256.c21
-rw-r--r--src/SHA384.c19
-rw-r--r--src/SHA512.c19
-rw-r--r--src/hash_template.c22
8 files changed, 136 insertions, 36 deletions
diff --git a/src/MD2.c b/src/MD2.c
index dadb999..043bcc8 100644
--- a/src/MD2.c
+++ b/src/MD2.c
@@ -31,21 +31,26 @@
#include <string.h>
#include "pycrypto_compat.h"
-#define MODULE_NAME _MD2
-#define ALGORITHM_NAME "MD2"
+#define MODULE_NAME MD2
#define DIGEST_SIZE 16
#define BLOCK_SIZE 64
-/**
- * id-md2 OBJECT IDENTIFIER ::= {
- * iso(1) member-body(2) us(840) rsadsi(113549)
- * digestAlgorithm(2) 2
- * }
- */
-static const char md2_oid[] = { 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x02 };
-
-#define DER_OID ((void*)&md2_oid)
-#define DER_OID_SIZE (sizeof md2_oid)
+static char MODULE__doc__[] =
+ "MD2 cryptographic hash algorithm.\n"
+ "\n"
+ "MD2 is specified in RFC1319_ and it produces the 128 bit digest of a message.\n"
+ "\n"
+ " >>> from Crypto.Hash import MD2\n"
+ " >>>\n"
+ " >>> h = MD2.new()\n"
+ " >>> h.update(b'Hello')\n"
+ " >>> print h.hexdigest()\n"
+ "\n"
+ "MD2 stand for Message Digest version 2, and it was invented by Rivest in 1989.\n"
+ "\n"
+ "This algorithm is both slow and insecure. Do not use it for new designs.\n"
+ "\n"
+ ".. _RFC1319: http://tools.ietf.org/html/rfc1319\n";
typedef unsigned char U8;
typedef unsigned int U32;
diff --git a/src/MD4.c b/src/MD4.c
index d5c20f4..7e453a8 100644
--- a/src/MD4.c
+++ b/src/MD4.c
@@ -31,11 +31,27 @@
#include <string.h>
#include "pycrypto_compat.h"
-#define MODULE_NAME _MD4
-#define ALGORITHM_NAME "MD4"
+#define MODULE_NAME MD4
#define DIGEST_SIZE 16
#define BLOCK_SIZE 64
+static char MODULE__doc__[] =
+ "MD4 cryptographic hash algorithm.\n"
+ "\n"
+ "MD4 is specified in RFC1320_ and produces the 128 bit digest of a message.\n"
+ "\n"
+ " >>> from Crypto.Hash import MD4\n"
+ " >>>\n"
+ " >>> h = MD4.new()\n"
+ " >>> h.update(b'Hello')\n"
+ " >>> print h.hexdigest()\n"
+ "\n"
+ "MD4 stand for Message Digest version 4, and it was invented by Rivest in 1990.\n"
+ "\n"
+ "This algorithm is insecure. Do not use it for new designs.\n"
+ "\n"
+ ".. _RFC1320: http://tools.ietf.org/html/rfc1320\n";
+
typedef unsigned int U32;
typedef unsigned char U8;
#define U32_MAX (U32)4294967295
diff --git a/src/RIPEMD160.c b/src/RIPEMD160.c
index 37d4c73..9593fc8 100644
--- a/src/RIPEMD160.c
+++ b/src/RIPEMD160.c
@@ -61,6 +61,26 @@
#define RIPEMD160_DIGEST_SIZE 20
#define BLOCK_SIZE 64
+static char MODULE__doc__[] =
+ "RIPEMD-160 cryptographic hash algorithm.\n"
+ "\n"
+ "RIPEMD-160_ produces the 160 bit digest of a message.\n"
+ "\n"
+ " >>> from Crypto.Hash import RIPEMD160\n"
+ " >>>\n"
+ " >>> h = RIPEMD160.new()\n"
+ " >>> h.update(b'Hello')\n"
+ " >>> print h.hexdigest()\n"
+ "\n"
+ "RIPEMD-160 stands for RACE Integrity Primitives Evaluation Message Digest\n"
+ "with a 160 bit digest. It was invented by Dobbertin, Bosselaers, and Preneel.\n"
+ "\n"
+ "This algorithm is considered secure, although it has not been scrutinized as\n"
+ "extensively as SHA-1. Moreover, it provides an informal security level of just\n"
+ "80bits.\n"
+ "\n"
+ ".. _RIPEMD-160: http://homes.esat.kuleuven.be/~bosselae/ripemd160.html\n";
+
#define RIPEMD160_MAGIC 0x9f19dd68u
typedef struct {
uint32_t magic;
@@ -401,8 +421,7 @@ static int ripemd160_digest(const ripemd160_state *self, unsigned char *out)
}
/* Template definitions */
-#define MODULE_NAME _RIPEMD160
-#define ALGORITHM_NAME "RIPEMD160"
+#define MODULE_NAME RIPEMD160
#define DIGEST_SIZE RIPEMD160_DIGEST_SIZE
#define hash_state ripemd160_state
#define hash_init ripemd160_init
diff --git a/src/SHA224.c b/src/SHA224.c
index 99e2f7c..86591cf 100644
--- a/src/SHA224.c
+++ b/src/SHA224.c
@@ -27,13 +27,28 @@
*
*/
-#define MODULE_NAME _SHA224
-#define ALGORITHM_NAME "SHA224"
+#define MODULE_NAME SHA224
#define DIGEST_SIZE (224/8)
#define BLOCK_SIZE (512/8)
#define WORD_SIZE 4
#define SCHEDULE_SIZE 64
+static char MODULE__doc__[] =
+ "SHA-224 cryptographic hash algorithm.\n"
+ "\n"
+ "SHA-224 belongs to the SHA-2_ family of cryptographic hashes.\n"
+ "It produces the 224 bit digest of a message.\n"
+ "\n"
+ " >>> from Crypto.Hash import SHA224\n"
+ " >>>\n"
+ " >>> h = SHA224.new()\n"
+ " >>> h.update(b'Hello')\n"
+ " >>> print h.hexdigest()\n"
+ "\n"
+ "*SHA* stands for Secure Hash Algorithm.\n"
+ "\n"
+ ".. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf\n";
+
#include "hash_SHA2.h"
/* Initial Values H */
diff --git a/src/SHA256.c b/src/SHA256.c
index 8cc3265..9473abc 100644
--- a/src/SHA256.c
+++ b/src/SHA256.c
@@ -26,13 +26,28 @@
* ===================================================================
*
*/
-#define MODULE_NAME _SHA256
-#define ALGORITHM_NAME "SHA256"
+#define MODULE_NAME SHA256
#define DIGEST_SIZE (256/8)
#define BLOCK_SIZE (512/8)
#define WORD_SIZE 4
#define SCHEDULE_SIZE 64
-
+
+static char MODULE__doc__[] =
+ "SHA-256 cryptographic hash algorithm.\n"
+ "\n"
+ "SHA-256 belongs to the SHA-2_ family of cryptographic hashes.\n"
+ "It produces the 256 bit digest of a message.\n"
+ "\n"
+ " >>> from Crypto.Hash import SHA256\n"
+ " >>>\n"
+ " >>> h = SHA256.new()\n"
+ " >>> h.update(b'Hello')\n"
+ " >>> print h.hexdigest()\n"
+ "\n"
+ "*SHA* stands for Secure Hash Algorithm.\n"
+ "\n"
+ ".. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf\n";
+
#include "hash_SHA2.h"
/* Initial Values H */
diff --git a/src/SHA384.c b/src/SHA384.c
index 24b200c..eb7051e 100644
--- a/src/SHA384.c
+++ b/src/SHA384.c
@@ -27,13 +27,28 @@
*
*/
-#define MODULE_NAME _SHA384
-#define ALGORITHM_NAME "SHA384"
+#define MODULE_NAME SHA384
#define DIGEST_SIZE (384/8)
#define BLOCK_SIZE (1024/8)
#define WORD_SIZE 8
#define SCHEDULE_SIZE 80
+static char MODULE__doc__[] =
+ "SHA-384 cryptographic hash algorithm.\n"
+ "\n"
+ "SHA-384 belongs to the SHA-2_ family of cryptographic hashes.\n"
+ "It produces the 384 bit digest of a message.\n"
+ "\n"
+ " >>> from Crypto.Hash import SHA384\n"
+ " >>>\n"
+ " >>> h = SHA384.new()\n"
+ " >>> h.update(b'Hello')\n"
+ " >>> print h.hexdigest()\n"
+ "\n"
+ "*SHA* stands for Secure Hash Algorithm.\n"
+ "\n"
+ ".. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf\n";
+
#include "hash_SHA2.h"
/* Initial Values H */
diff --git a/src/SHA512.c b/src/SHA512.c
index 3b227ab..f12755c 100644
--- a/src/SHA512.c
+++ b/src/SHA512.c
@@ -27,13 +27,28 @@
*
*/
-#define MODULE_NAME _SHA512
-#define ALGORITHM_NAME "SHA512"
+#define MODULE_NAME SHA512
#define DIGEST_SIZE (512/8)
#define BLOCK_SIZE (1024/8)
#define WORD_SIZE 8
#define SCHEDULE_SIZE 80
+static char MODULE__doc__[] =
+ "SHA-512 cryptographic hash algorithm.\n"
+ "\n"
+ "SHA-512 belongs to the SHA-2_ family of cryptographic hashes.\n"
+ "It produces the 512 bit digest of a message.\n"
+ "\n"
+ " >>> from Crypto.Hash import SHA512\n"
+ " >>>\n"
+ " >>> h = SHA512.new()\n"
+ " >>> h.update(b'Hello')\n"
+ " >>> print h.hexdigest()\n"
+ "\n"
+ "*SHA* stands for Secure Hash Algorithm.\n"
+ "\n"
+ ".. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf\n";
+
#include "hash_SHA2.h"
/* Initial Values H */
diff --git a/src/hash_template.c b/src/hash_template.c
index afffbfb..d085bb8 100644
--- a/src/hash_template.c
+++ b/src/hash_template.c
@@ -210,12 +210,12 @@ ALG_getattr(PyObject *self, char *name)
if (PyUnicode_CompareWithASCIIString(attr, "digest_size")==0)
return PyLong_FromLong(DIGEST_SIZE);
if (PyUnicode_CompareWithASCIIString(attr, "name")==0)
- return PyUnicode_FromString(ALGORITHM_NAME);
+ return PyUnicode_FromString(_MODULE_STRING); /* we should try to be compatible with hashlib here */
#else
if (strcmp(name, "digest_size")==0)
return PyInt_FromLong(DIGEST_SIZE);
if (strcmp(name, "name")==0)
- return PyString_FromString(ALGORITHM_NAME);
+ return PyString_FromString(_MODULE_STRING); /* we should try to be compatible with hashlib here */
#endif
#ifdef IS_PY3K
@@ -313,14 +313,14 @@ static struct PyMethodDef ALG_functions[] = {
#ifdef IS_PY3K
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
- "Crypto.Hash." _MODULE_STRING,
- NULL,
- -1,
- ALG_functions,
- NULL,
- NULL,
- NULL,
- NULL
+ "Crypto.Hash." _MODULE_STRING, /* m_name */
+ MODULE__doc__, /* m_doc */
+ -1, /* m_size */
+ ALG_functions, /* m_methods */
+ NULL, /* m_reload */
+ NULL, /* m_traverse */
+ NULL, /* m_clear */
+ NULL /* m_free */
};
#endif
@@ -353,7 +353,7 @@ _MODULE_NAME (void)
return NULL;
#else
ALGtype.ob_type = &PyType_Type;
- m = Py_InitModule("Crypto.Hash." _MODULE_STRING, ALG_functions);
+ m = Py_InitModule3("Crypto.Hash." _MODULE_STRING, ALG_functions, MODULE__doc__);
#endif
/* Add some symbolic constants to the module */