Back to index

python3.2  3.2.2
Classes | Defines | Functions | Variables
sha1module.c File Reference
#include "Python.h"
#include "hashlib.h"

Go to the source code of this file.

Classes

struct  sha1_state
struct  SHA1object

Defines

#define SHA1_BLOCKSIZE   64
#define SHA1_DIGESTSIZE   20
#define ROL(x, y)   ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
#define ROLc(x, y)   ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
#define STORE32H(x, y)
#define LOAD32H(x, y)
#define STORE64H(x, y)
#define MIN(x, y)   ( ((x)<(y))?(x):(y) )
#define F0(x, y, z)   (z ^ (x & (y ^ z)))
#define F1(x, y, z)   (x ^ y ^ z)
#define F2(x, y, z)   ((x & y) | (z & (x | y)))
#define F3(x, y, z)   (x ^ y ^ z)
#define FF_0(a, b, c, d, e, i)   e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30);
#define FF_1(a, b, c, d, e, i)   e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30);
#define FF_2(a, b, c, d, e, i)   e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30);
#define FF_3(a, b, c, d, e, i)   e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30);
#define insint(n, v)   { PyModule_AddIntConstant(m,n,v); }

Functions

static void sha1_compress (struct sha1_state *sha1, unsigned char *buf)
void sha1_init (struct sha1_state *sha1)
 Initialize the hash state.
void sha1_process (struct sha1_state *sha1, const unsigned char *in, Py_ssize_t inlen)
 Process a block of memory though the hash.
void sha1_done (struct sha1_state *sha1, unsigned char *out)
 Terminate the hash to get the digest.
static SHA1objectnewSHA1object (void)
static void SHA1_dealloc (PyObject *ptr)
 PyDoc_STRVAR (SHA1_copy__doc__,"Return a copy of the hash object.")
static PyObjectSHA1_copy (SHA1object *self, PyObject *unused)
 PyDoc_STRVAR (SHA1_digest__doc__,"Return the digest value as a string of binary data.")
static PyObjectSHA1_digest (SHA1object *self, PyObject *unused)
 PyDoc_STRVAR (SHA1_hexdigest__doc__,"Return the digest value as a string of hexadecimal digits.")
static PyObjectSHA1_hexdigest (SHA1object *self, PyObject *unused)
 PyDoc_STRVAR (SHA1_update__doc__,"Update this hash object's state with the provided string.")
static PyObjectSHA1_update (SHA1object *self, PyObject *args)
static PyObjectSHA1_get_block_size (PyObject *self, void *closure)
static PyObjectSHA1_get_name (PyObject *self, void *closure)
static PyObjectsha1_get_digest_size (PyObject *self, void *closure)
 PyDoc_STRVAR (SHA1_new__doc__,"Return a new SHA1 hash object; optionally initialized with a string.")
static PyObjectSHA1_new (PyObject *self, PyObject *args, PyObject *kwdict)
PyMODINIT_FUNC PyInit__sha1 (void)

Variables

static PyTypeObject SHA1type
static PyMethodDef SHA1_methods []
static PyGetSetDef SHA1_getseters []
static struct PyMethodDef []
static struct PyModuleDef

Class Documentation

struct sha1_state

Definition at line 39 of file sha1module.c.

Class Members
unsigned char buf
SHA1_INT32 curlen
SHA1_INT64 length
SHA1_INT32 state
struct SHA1object

Definition at line 45 of file sha1module.c.

Collaboration diagram for SHA1object:
Class Members
PyObject_HEAD struct sha1_state

Define Documentation

#define F0 (   x,
  y,
 
)    (z ^ (x & (y ^ z)))

Definition at line 102 of file sha1module.c.

#define F1 (   x,
  y,
 
)    (x ^ y ^ z)

Definition at line 103 of file sha1module.c.

#define F2 (   x,
  y,
 
)    ((x & y) | (z & (x | y)))

Definition at line 104 of file sha1module.c.

#define F3 (   x,
  y,
 
)    (x ^ y ^ z)

Definition at line 105 of file sha1module.c.

#define FF_0 (   a,
  b,
  c,
  d,
  e,
  i 
)    e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30);
#define FF_1 (   a,
  b,
  c,
  d,
  e,
  i 
)    e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30);
#define FF_2 (   a,
  b,
  c,
  d,
  e,
  i 
)    e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30);
#define FF_3 (   a,
  b,
  c,
  d,
  e,
  i 
)    e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30);
#define insint (   n,
  v 
)    { PyModule_AddIntConstant(m,n,v); }

Definition at line 538 of file sha1module.c.

#define LOAD32H (   x,
 
)
Value:
{ x = ((unsigned long)((y)[0] & 255)<<24) | \
           ((unsigned long)((y)[1] & 255)<<16) | \
           ((unsigned long)((y)[2] & 255)<<8)  | \
           ((unsigned long)((y)[3] & 255)); }

Definition at line 83 of file sha1module.c.

#define MIN (   x,
 
)    ( ((x)<(y))?(x):(y) )

Definition at line 96 of file sha1module.c.

#define ROL (   x,
 
)    ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)

Definition at line 74 of file sha1module.c.

#define ROLc (   x,
 
)    ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)

Definition at line 75 of file sha1module.c.

#define SHA1_BLOCKSIZE   64

Definition at line 34 of file sha1module.c.

#define SHA1_DIGESTSIZE   20

Definition at line 35 of file sha1module.c.

#define STORE32H (   x,
 
)
Value:
{ (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255);   \
       (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }

Definition at line 79 of file sha1module.c.

#define STORE64H (   x,
 
)
Value:
{ (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255);     \
     (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255);     \
     (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255);     \
     (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }

Definition at line 89 of file sha1module.c.


Function Documentation

static SHA1object* newSHA1object ( void  ) [static]

Definition at line 296 of file sha1module.c.

Here is the caller graph for this function:

PyDoc_STRVAR ( SHA1_copy__doc__  ,
"Return a copy of the hash object."   
)
PyDoc_STRVAR ( SHA1_digest__doc__  ,
"Return the digest value as a string of binary data."   
)
PyDoc_STRVAR ( SHA1_hexdigest__doc__  ,
"Return the digest value as a string of hexadecimal digits."   
)
PyDoc_STRVAR ( SHA1_update__doc__  ,
"Update this hash object's state with the provided string."   
)
PyDoc_STRVAR ( SHA1_new__doc__  ,
"Return a new SHA1 hash object; optionally initialized with a string."   
)

Definition at line 554 of file sha1module.c.

{
    Py_TYPE(&SHA1type) = &PyType_Type;
    if (PyType_Ready(&SHA1type) < 0)
        return NULL;
    return PyModule_Create(&_sha1module);
}

Here is the call graph for this function:

static void sha1_compress ( struct sha1_state sha1,
unsigned char *  buf 
) [static]

Definition at line 107 of file sha1module.c.

{
    SHA1_INT32 a,b,c,d,e,W[80],i;

    /* copy the state into 512-bits into W[0..15] */
    for (i = 0; i < 16; i++) {
        LOAD32H(W[i], buf + (4*i));
    }

    /* copy state */
    a = sha1->state[0];
    b = sha1->state[1];
    c = sha1->state[2];
    d = sha1->state[3];
    e = sha1->state[4];

    /* expand it */
    for (i = 16; i < 80; i++) {
        W[i] = ROL(W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16], 1);
    }

    /* compress */
    /* round one */
    #define FF_0(a,b,c,d,e,i) e = (ROLc(a, 5) + F0(b,c,d) + e + W[i] + 0x5a827999UL); b = ROLc(b, 30);
    #define FF_1(a,b,c,d,e,i) e = (ROLc(a, 5) + F1(b,c,d) + e + W[i] + 0x6ed9eba1UL); b = ROLc(b, 30);
    #define FF_2(a,b,c,d,e,i) e = (ROLc(a, 5) + F2(b,c,d) + e + W[i] + 0x8f1bbcdcUL); b = ROLc(b, 30);
    #define FF_3(a,b,c,d,e,i) e = (ROLc(a, 5) + F3(b,c,d) + e + W[i] + 0xca62c1d6UL); b = ROLc(b, 30);

    for (i = 0; i < 20; ) {
       FF_0(a,b,c,d,e,i++);
       FF_0(e,a,b,c,d,i++);
       FF_0(d,e,a,b,c,i++);
       FF_0(c,d,e,a,b,i++);
       FF_0(b,c,d,e,a,i++);
    }

    /* round two */
    for (; i < 40; )  {
       FF_1(a,b,c,d,e,i++);
       FF_1(e,a,b,c,d,i++);
       FF_1(d,e,a,b,c,i++);
       FF_1(c,d,e,a,b,i++);
       FF_1(b,c,d,e,a,i++);
    }

    /* round three */
    for (; i < 60; )  {
       FF_2(a,b,c,d,e,i++);
       FF_2(e,a,b,c,d,i++);
       FF_2(d,e,a,b,c,i++);
       FF_2(c,d,e,a,b,i++);
       FF_2(b,c,d,e,a,i++);
    }

    /* round four */
    for (; i < 80; )  {
       FF_3(a,b,c,d,e,i++);
       FF_3(e,a,b,c,d,i++);
       FF_3(d,e,a,b,c,i++);
       FF_3(c,d,e,a,b,i++);
       FF_3(b,c,d,e,a,i++);
    }

    #undef FF_0
    #undef FF_1
    #undef FF_2
    #undef FF_3

    /* store */
    sha1->state[0] = sha1->state[0] + a;
    sha1->state[1] = sha1->state[1] + b;
    sha1->state[2] = sha1->state[2] + c;
    sha1->state[3] = sha1->state[3] + d;
    sha1->state[4] = sha1->state[4] + e;
}

Here is the caller graph for this function:

static PyObject* SHA1_copy ( SHA1object self,
PyObject unused 
) [static]

Definition at line 316 of file sha1module.c.

{
    SHA1object *newobj;

    if (Py_TYPE(self) == &SHA1type) {
        if ( (newobj = newSHA1object())==NULL)
            return NULL;
    } else {
        if ( (newobj = newSHA1object())==NULL)
            return NULL;
    }

    newobj->hash_state = self->hash_state;
    return (PyObject *)newobj;
}

Here is the call graph for this function:

static void SHA1_dealloc ( PyObject ptr) [static]

Definition at line 305 of file sha1module.c.

{
    PyObject_Del(ptr);
}
static PyObject* SHA1_digest ( SHA1object self,
PyObject unused 
) [static]

Definition at line 336 of file sha1module.c.

{
    unsigned char digest[SHA1_DIGESTSIZE];
    struct sha1_state temp;

    temp = self->hash_state;
    sha1_done(&temp, digest);
    return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE);
}

Here is the call graph for this function:

void sha1_done ( struct sha1_state sha1,
unsigned char *  out 
)

Terminate the hash to get the digest.

Parameters:
sha1The hash state
out[out] The destination of the hash (20 bytes)

Definition at line 240 of file sha1module.c.

{
    int i;

    assert(sha1 != NULL);
    assert(out != NULL);
    assert(sha1->curlen < sizeof(sha1->buf));

    /* increase the length of the message */
    sha1->length += sha1->curlen * 8;

    /* append the '1' bit */
    sha1->buf[sha1->curlen++] = (unsigned char)0x80;

    /* if the length is currently above 56 bytes we append zeros
     * then compress.  Then we can fall back to padding zeros and length
     * encoding like normal.
     */
    if (sha1->curlen > 56) {
        while (sha1->curlen < 64) {
            sha1->buf[sha1->curlen++] = (unsigned char)0;
        }
        sha1_compress(sha1, sha1->buf);
        sha1->curlen = 0;
    }

    /* pad upto 56 bytes of zeroes */
    while (sha1->curlen < 56) {
        sha1->buf[sha1->curlen++] = (unsigned char)0;
    }

    /* store length */
    STORE64H(sha1->length, sha1->buf+56);
    sha1_compress(sha1, sha1->buf);

    /* copy output */
    for (i = 0; i < 5; i++) {
        STORE32H(sha1->state[i], out+(4*i));
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* SHA1_get_block_size ( PyObject self,
void closure 
) [static]

Definition at line 415 of file sha1module.c.

Here is the call graph for this function:

static PyObject* sha1_get_digest_size ( PyObject self,
void closure 
) [static]

Definition at line 427 of file sha1module.c.

Here is the call graph for this function:

static PyObject* SHA1_get_name ( PyObject self,
void closure 
) [static]

Definition at line 421 of file sha1module.c.

{
    return PyUnicode_FromStringAndSize("SHA1", 3);
}

Here is the call graph for this function:

static PyObject* SHA1_hexdigest ( SHA1object self,
PyObject unused 
) [static]

Definition at line 350 of file sha1module.c.

{
    unsigned char digest[SHA1_DIGESTSIZE];
    struct sha1_state temp;
    PyObject *retval;
    Py_UNICODE *hex_digest;
    int i, j;

    /* Get the raw (binary) digest value */
    temp = self->hash_state;
    sha1_done(&temp, digest);

    /* Create a new string */
    retval = PyUnicode_FromStringAndSize(NULL, SHA1_DIGESTSIZE * 2);
    if (!retval)
            return NULL;
    hex_digest = PyUnicode_AS_UNICODE(retval);
    if (!hex_digest) {
            Py_DECREF(retval);
            return NULL;
    }

    /* Make hex version of the digest */
    for(i=j=0; i<SHA1_DIGESTSIZE; i++) {
        char c;
        c = (digest[i] >> 4) & 0xf;
        c = (c>9) ? c+'a'-10 : c + '0';
        hex_digest[j++] = c;
        c = (digest[i] & 0xf);
        c = (c>9) ? c+'a'-10 : c + '0';
        hex_digest[j++] = c;
    }
    return retval;
}

Here is the call graph for this function:

void sha1_init ( struct sha1_state sha1)

Initialize the hash state.

Parameters:
sha1The hash state you wish to initialize

Definition at line 187 of file sha1module.c.

{
   assert(sha1 != NULL);
   sha1->state[0] = 0x67452301UL;
   sha1->state[1] = 0xefcdab89UL;
   sha1->state[2] = 0x98badcfeUL;
   sha1->state[3] = 0x10325476UL;
   sha1->state[4] = 0xc3d2e1f0UL;
   sha1->curlen = 0;
   sha1->length = 0;
}

Here is the caller graph for this function:

static PyObject* SHA1_new ( PyObject self,
PyObject args,
PyObject kwdict 
) [static]

Definition at line 490 of file sha1module.c.

{
    static char *kwlist[] = {"string", NULL};
    SHA1object *new;
    PyObject *data_obj = NULL;
    Py_buffer buf;

    if (!PyArg_ParseTupleAndKeywords(args, kwdict, "|O:new", kwlist,
                                     &data_obj)) {
        return NULL;
    }

    if (data_obj)
        GET_BUFFER_VIEW_OR_ERROUT(data_obj, &buf);

    if ((new = newSHA1object()) == NULL) {
        if (data_obj)
            PyBuffer_Release(&buf);
        return NULL;
    }

    sha1_init(&new->hash_state);

    if (PyErr_Occurred()) {
        Py_DECREF(new);
        if (data_obj)
            PyBuffer_Release(&buf);
        return NULL;
    }
    if (data_obj) {
        sha1_process(&new->hash_state, buf.buf, buf.len);
        PyBuffer_Release(&buf);
    }

    return (PyObject *)new;
}

Here is the call graph for this function:

void sha1_process ( struct sha1_state sha1,
const unsigned char *  in,
Py_ssize_t  inlen 
)

Process a block of memory though the hash.

Parameters:
sha1The hash state
inThe data to hash
inlenThe length of the data (octets)

Definition at line 205 of file sha1module.c.

{
    Py_ssize_t n;

    assert(sha1 != NULL);
    assert(in != NULL);
    assert(sha1->curlen <= sizeof(sha1->buf));

    while (inlen > 0) {
        if (sha1->curlen == 0 && inlen >= SHA1_BLOCKSIZE) {
           sha1_compress(sha1, (unsigned char *)in);
           sha1->length   += SHA1_BLOCKSIZE * 8;
           in             += SHA1_BLOCKSIZE;
           inlen          -= SHA1_BLOCKSIZE;
        } else {
           n = MIN(inlen, (SHA1_BLOCKSIZE - sha1->curlen));
           memcpy(sha1->buf + sha1->curlen, in, (size_t)n);
           sha1->curlen   += n;
           in             += n;
           inlen          -= n;
           if (sha1->curlen == SHA1_BLOCKSIZE) {
              sha1_compress(sha1, sha1->buf);
              sha1->length += 8*SHA1_BLOCKSIZE;
              sha1->curlen = 0;
           }
       }
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static PyObject* SHA1_update ( SHA1object self,
PyObject args 
) [static]

Definition at line 389 of file sha1module.c.

{
    PyObject *obj;
    Py_buffer buf;

    if (!PyArg_ParseTuple(args, "O:update", &obj))
        return NULL;

    GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);

    sha1_process(&self->hash_state, buf.buf, buf.len);

    PyBuffer_Release(&buf);
    Py_INCREF(Py_None);
    return Py_None;
}

Here is the call graph for this function:


Variable Documentation

struct PyMethodDef[] [static]
Initial value:
 {
    {"sha1",(PyCFunction)SHA1_new, METH_VARARGS|METH_KEYWORDS,SHA1_new__doc__},
    {NULL,      NULL}            
}

Definition at line 530 of file sha1module.c.

struct PyModuleDef [static]
Initial value:
 {
        PyModuleDef_HEAD_INIT,
        "_sha1",
        NULL,
        -1,
        SHA1_functions,
        NULL,
        NULL,
        NULL,
        NULL
}

Definition at line 541 of file sha1module.c.

Initial value:
 {
    {"block_size",
     (getter)SHA1_get_block_size, NULL,
     NULL,
     NULL},
    {"name",
     (getter)SHA1_get_name, NULL,
     NULL,
     NULL},
    {"digest_size",
     (getter)sha1_get_digest_size, NULL,
     NULL,
     NULL},
    {NULL}  
}

Definition at line 433 of file sha1module.c.

Initial value:
 {
    {"copy",      (PyCFunction)SHA1_copy,      METH_NOARGS,  SHA1_copy__doc__},
    {"digest",    (PyCFunction)SHA1_digest,    METH_NOARGS,  SHA1_digest__doc__},
    {"hexdigest", (PyCFunction)SHA1_hexdigest, METH_NOARGS,  SHA1_hexdigest__doc__},
    {"update",    (PyCFunction)SHA1_update,    METH_VARARGS, SHA1_update__doc__},
    {NULL,        NULL}         
}

Definition at line 406 of file sha1module.c.

static PyTypeObject SHA1type [static]

Definition at line 292 of file sha1module.c.