⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cryptlib_py.c

📁 老外写的加密库cryptlib(版本3.1)
💻 C
📖 第 1 页 / 共 5 页
字号:
#include <Python.h>#include "cryptlib.h"	static PyObject* cryptHandleClass;static PyObject* cryptQueryInfoClass;static PyObject* cryptObjectInfoClass;static PyObject *CryptException;static int getPointerWrite(PyObject* objPtr, unsigned char** bytesPtrPtr, int* lengthPtr){	if (objPtr == Py_None)	{		*bytesPtrPtr = NULL;		*lengthPtr = 0;		return 1;	}	/*See if it's an array object*/	if (PyObject_AsWriteBuffer(objPtr, bytesPtrPtr, lengthPtr) == -1)		return 0;	return 1;}static int getPointerRead(PyObject* objPtr, unsigned char** bytesPtrPtr, int* lengthPtr){	if (objPtr == Py_None)	{		*bytesPtrPtr = NULL;		*lengthPtr = 0;		return 1;	}	/*See if it's an array object*/	if (PyObject_AsWriteBuffer(objPtr, bytesPtrPtr, lengthPtr) == -1)	{		PyErr_Clear();		/*See if it's a string object*/        /*This returns the length excluding the NULL if it's a string,          which is what we want*/		if (PyObject_AsCharBuffer(objPtr, bytesPtrPtr, lengthPtr) == -1)			return 0;	}	return 1;}static int getPointerReadNoLength(PyObject* objPtr, unsigned char** bytesPtrPtr){	int length;	return getPointerRead(objPtr, bytesPtrPtr, &length);}static int getPointerWriteCheckIndices(PyObject* objPtr, unsigned char** bytesPtrPtr, int* lengthPtr){	int checkLength = *lengthPtr;	if (getPointerWrite(objPtr, bytesPtrPtr, lengthPtr) == 0)		return 0;    //If sequence is non-NULL and too short...	if (*bytesPtrPtr && (*lengthPtr < checkLength))	{		PyErr_SetString(PyExc_IndexError, "A sequence passed to cryptlib was too small");		return 0;	}	return 1;}static int getPointerReadString(PyObject* objPtr, char** charPtrPtr){	int length = 0;	char* newPtr = NULL;	if (objPtr == Py_None)	{		*charPtrPtr = NULL;		return 1;	}	/*See if it's a string or a buffer object*/	if (PyObject_AsCharBuffer(objPtr, charPtrPtr, &length) == -1)	{		/*See if it's an array*/		PyErr_Clear();		if (PyObject_AsWriteBuffer(objPtr, charPtrPtr, &length) == -1)			return 0;	}	/*This code isn't necessary for a string, but it is for arrays and buffers, 	  so we do it always anyway, since the PyObject_AsCharBuffer apparently doesn't 	  guarantee us null-terminated data, and this way releasePointerString() doesn't      have to differentiate */	newPtr = malloc(length+1);	if (newPtr == NULL)	{		PyErr_NoMemory();		return 0;	}    	memcpy(newPtr, *charPtrPtr, length);	newPtr[length] = 0;	*charPtrPtr = newPtr;	return 1;}static void releasePointer(PyObject* objPtr, unsigned char* bytesPtr){}static void releasePointerString(PyObject* objPtr, char* charPtr){	free(charPtr);}static PyObject* processStatus(int status){	PyObject* o = NULL;    /* If an error has already occurred, ignore the status and just fall through */    if (PyErr_Occurred())        return(NULL);	if (status >= CRYPT_OK)		return(Py_BuildValue(""));    else if (status == CRYPT_ERROR_PARAM1)        o = Py_BuildValue("(is)", CRYPT_ERROR_PARAM1, "Bad argument, parameter 1");    else if (status == CRYPT_ERROR_PARAM2)        o = Py_BuildValue("(is)", CRYPT_ERROR_PARAM2, "Bad argument, parameter 2");    else if (status == CRYPT_ERROR_PARAM3)        o = Py_BuildValue("(is)", CRYPT_ERROR_PARAM3, "Bad argument, parameter 3");    else if (status == CRYPT_ERROR_PARAM4)        o = Py_BuildValue("(is)", CRYPT_ERROR_PARAM4, "Bad argument, parameter 4");    else if (status == CRYPT_ERROR_PARAM5)        o = Py_BuildValue("(is)", CRYPT_ERROR_PARAM5, "Bad argument, parameter 5");    else if (status == CRYPT_ERROR_PARAM6)        o = Py_BuildValue("(is)", CRYPT_ERROR_PARAM6, "Bad argument, parameter 6");    else if (status == CRYPT_ERROR_PARAM7)        o = Py_BuildValue("(is)", CRYPT_ERROR_PARAM7, "Bad argument, parameter 7");    else if (status == CRYPT_ERROR_MEMORY)        o = Py_BuildValue("(is)", CRYPT_ERROR_MEMORY, "Out of memory");    else if (status == CRYPT_ERROR_NOTINITED)        o = Py_BuildValue("(is)", CRYPT_ERROR_NOTINITED, "Data has not been initialised");    else if (status == CRYPT_ERROR_INITED)        o = Py_BuildValue("(is)", CRYPT_ERROR_INITED, "Data has already been init'd");    else if (status == CRYPT_ERROR_NOSECURE)        o = Py_BuildValue("(is)", CRYPT_ERROR_NOSECURE, "Opn.not avail.at requested sec.level");    else if (status == CRYPT_ERROR_RANDOM)        o = Py_BuildValue("(is)", CRYPT_ERROR_RANDOM, "No reliable random data available");    else if (status == CRYPT_ERROR_FAILED)        o = Py_BuildValue("(is)", CRYPT_ERROR_FAILED, "Operation failed");    else if (status == CRYPT_ERROR_NOTAVAIL)        o = Py_BuildValue("(is)", CRYPT_ERROR_NOTAVAIL, "This type of opn.not available");    else if (status == CRYPT_ERROR_PERMISSION)        o = Py_BuildValue("(is)", CRYPT_ERROR_PERMISSION, "No permiss.to perform this operation");    else if (status == CRYPT_ERROR_WRONGKEY)        o = Py_BuildValue("(is)", CRYPT_ERROR_WRONGKEY, "Incorrect key used to decrypt data");    else if (status == CRYPT_ERROR_INCOMPLETE)        o = Py_BuildValue("(is)", CRYPT_ERROR_INCOMPLETE, "Operation incomplete/still in progress");    else if (status == CRYPT_ERROR_COMPLETE)        o = Py_BuildValue("(is)", CRYPT_ERROR_COMPLETE, "Operation complete/can't continue");    else if (status == CRYPT_ERROR_TIMEOUT)        o = Py_BuildValue("(is)", CRYPT_ERROR_TIMEOUT, "Operation timed out before completion");    else if (status == CRYPT_ERROR_INVALID)        o = Py_BuildValue("(is)", CRYPT_ERROR_INVALID, "Invalid/inconsistent information");    else if (status == CRYPT_ERROR_SIGNALLED)        o = Py_BuildValue("(is)", CRYPT_ERROR_SIGNALLED, "Resource destroyed by extnl.event");    else if (status == CRYPT_ERROR_OVERFLOW)        o = Py_BuildValue("(is)", CRYPT_ERROR_OVERFLOW, "Resources/space exhausted");    else if (status == CRYPT_ERROR_UNDERFLOW)        o = Py_BuildValue("(is)", CRYPT_ERROR_UNDERFLOW, "Not enough data available");    else if (status == CRYPT_ERROR_BADDATA)        o = Py_BuildValue("(is)", CRYPT_ERROR_BADDATA, "Bad/unrecognised data format");    else if (status == CRYPT_ERROR_SIGNATURE)        o = Py_BuildValue("(is)", CRYPT_ERROR_SIGNATURE, "Signature/integrity check failed");    else if (status == CRYPT_ERROR_OPEN)        o = Py_BuildValue("(is)", CRYPT_ERROR_OPEN, "Cannot open object");    else if (status == CRYPT_ERROR_READ)        o = Py_BuildValue("(is)", CRYPT_ERROR_READ, "Cannot read item from object");    else if (status == CRYPT_ERROR_WRITE)        o = Py_BuildValue("(is)", CRYPT_ERROR_WRITE, "Cannot write item to object");    else if (status == CRYPT_ERROR_NOTFOUND)        o = Py_BuildValue("(is)", CRYPT_ERROR_NOTFOUND, "Requested item not found in object");    else if (status == CRYPT_ERROR_DUPLICATE)        o = Py_BuildValue("(is)", CRYPT_ERROR_DUPLICATE, "Item already present in object");    else if (status == CRYPT_ENVELOPE_RESOURCE)        o = Py_BuildValue("(is)", CRYPT_ENVELOPE_RESOURCE, "Need resource to proceed");	PyErr_SetObject(CryptException, o);	Py_DECREF(o);	return(NULL);}static int processStatusBool(int status){    PyObject* o = processStatus(status);    if (o == NULL)        return(0);    else    {        Py_DECREF(o);        return(1);    }        }static PyObject* processStatusReturnInt(int status, int returnValue){    PyObject* o = processStatus(status);    if (o == NULL)        return(0);    else    {        Py_DECREF(o);        o = Py_BuildValue("i", returnValue);        return(o);    }        }static PyObject* processStatusReturnCryptHandle(int status, int returnValue){	PyObject* o2;    PyObject* o = processStatus(status);    if (o == NULL)        return(0);    else    {        Py_DECREF(o);        o2  = Py_BuildValue("(i)", returnValue);		o = PyObject_CallObject(cryptHandleClass, o2);		Py_DECREF(o2);        return(o);    }        }static PyObject* processStatusReturnCryptQueryInfo(int status, CRYPT_QUERY_INFO returnValue){	PyObject* o2;    PyObject* o = processStatus(status);    if (o == NULL)        return(0);    else    {        Py_DECREF(o);        o2  = Py_BuildValue("(siiii)", returnValue.algoName, returnValue.blockSize, returnValue.minKeySize, returnValue.keySize, returnValue.maxKeySize);		o = PyObject_CallObject(cryptQueryInfoClass, o2);		Py_DECREF(o2);        return(o);    }        }static PyObject* processStatusReturnCryptObjectInfo(int status, CRYPT_OBJECT_INFO returnValue){	PyObject* o2;    PyObject* o = processStatus(status);    if (o == NULL)        return(0);    else    {        Py_DECREF(o);        o2  = Py_BuildValue("(iiiis#)", returnValue.objectType, returnValue.cryptAlgo, returnValue.cryptMode, returnValue.hashAlgo, returnValue.salt, returnValue.saltSize);		o = PyObject_CallObject(cryptObjectInfoClass, o2);		Py_DECREF(o2);        return(o);    }        }static PyObject* python_cryptInit(PyObject* self, PyObject* args){	int status = 0;		status = cryptInit();		return(processStatus(status));}static PyObject* python_cryptEnd(PyObject* self, PyObject* args){	int status = 0;		status = cryptEnd();		return(processStatus(status));}static PyObject* python_cryptQueryCapability(PyObject* self, PyObject* args){	int status = 0;	CRYPT_QUERY_INFO cryptQueryInfo;	int cryptAlgo = 0;		if (!PyArg_ParseTuple(args, "i", &cryptAlgo))	    return(NULL);		status = cryptQueryCapability(cryptAlgo, &cryptQueryInfo);		return(processStatusReturnCryptQueryInfo(status, cryptQueryInfo));}static PyObject* python_cryptCreateContext(PyObject* self, PyObject* args){	int status = 0;	int cryptContext = 0;	int cryptUser = 0;	int cryptAlgo = 0;		if (!PyArg_ParseTuple(args, "ii", &cryptUser, &cryptAlgo))	    return(NULL);		status = cryptCreateContext(&cryptContext, cryptUser, cryptAlgo);		return(processStatusReturnCryptHandle(status, cryptContext));}static PyObject* python_cryptDestroyContext(PyObject* self, PyObject* args){	int status = 0;	int cryptContext = 0;		if (!PyArg_ParseTuple(args, "i", &cryptContext))	    return(NULL);		status = cryptDestroyContext(cryptContext);		return(processStatus(status));}static PyObject* python_cryptDestroyObject(PyObject* self, PyObject* args){	int status = 0;	int cryptObject = 0;		if (!PyArg_ParseTuple(args, "i", &cryptObject))	    return(NULL);		status = cryptDestroyObject(cryptObject);		return(processStatus(status));}static PyObject* python_cryptGenerateKey(PyObject* self, PyObject* args){	int status = 0;	int cryptContext = 0;		if (!PyArg_ParseTuple(args, "i", &cryptContext))	    return(NULL);		status = cryptGenerateKey(cryptContext);		return(processStatus(status));}static PyObject* python_cryptGenerateKeyAsync(PyObject* self, PyObject* args){	int status = 0;	int cryptContext = 0;		if (!PyArg_ParseTuple(args, "i", &cryptContext))	    return(NULL);		status = cryptGenerateKeyAsync(cryptContext);		return(processStatus(status));}static PyObject* python_cryptAsyncQuery(PyObject* self, PyObject* args){	int status = 0;	int cryptObject = 0;		if (!PyArg_ParseTuple(args, "i", &cryptObject))	    return(NULL);		status = cryptAsyncQuery(cryptObject);		return(processStatus(status));}static PyObject* python_cryptAsyncCancel(PyObject* self, PyObject* args){	int status = 0;	int cryptObject = 0;		if (!PyArg_ParseTuple(args, "i", &cryptObject))	    return(NULL);		status = cryptAsyncCancel(cryptObject);		return(processStatus(status));}static PyObject* python_cryptEncrypt(PyObject* self, PyObject* args){	int status = 0;	int cryptContext = 0;	PyObject* buffer = NULL;	int length = 0;	unsigned char* bufferPtr = 0;		if (!PyArg_ParseTuple(args, "iO", &cryptContext, &buffer))	    return(NULL);		if (!getPointerWrite(buffer, &bufferPtr, &length))		goto finish;		status = cryptEncrypt(cryptContext, bufferPtr, length);		finish:	releasePointer(buffer, bufferPtr);	return(processStatus(status));}static PyObject* python_cryptDecrypt(PyObject* self, PyObject* args){	int status = 0;	int cryptContext = 0;	PyObject* buffer = NULL;	int length = 0;	unsigned char* bufferPtr = 0;		if (!PyArg_ParseTuple(args, "iO", &cryptContext, &buffer))	    return(NULL);		if (!getPointerWrite(buffer, &bufferPtr, &length))		goto finish;		status = cryptDecrypt(cryptContext, bufferPtr, length);		finish:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -