File: pysecstr.c

package info (click to toggle)
securestring 0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68 kB
  • sloc: ansic: 54; python: 14; makefile: 3
file content (61 lines) | stat: -rw-r--r-- 1,378 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#define PY_SSIZE_T_CLEAN

#include <Python.h>
#include <openssl/crypto.h>

#if PY_MAJOR_VERSION >= 3
	static PyObject* SecureString_clearmem(PyObject *self, PyObject *args) {
		char *buffer;
		Py_ssize_t length;

		if(!PyArg_ParseTuple(args, "s#", &buffer, &length)) {
			return NULL;
		}
		OPENSSL_cleanse(buffer, length);
		return Py_BuildValue("");
	}
#else
	static PyObject* SecureString_clearmem(PyObject *self, PyObject *str) {
		char *buffer;
		Py_ssize_t length;

		if (PyString_AsStringAndSize(str, &buffer, &length) != -1) {
			OPENSSL_cleanse(buffer, length);
		}
		return Py_BuildValue("");
	}
#endif

#if PY_MAJOR_VERSION >= 3
	static PyMethodDef SecureStringMethods[] = {
		{"clearmem", SecureString_clearmem, METH_VARARGS, "clear the memory of the string"},
		{NULL, NULL, 0, NULL},
	};
#else
	static PyMethodDef SecureStringMethods[] = {
		{"clearmem", SecureString_clearmem, METH_O,
		PyDoc_STR("clear the memory of the string")},
		{NULL, NULL, 0, NULL},
	};
#endif

#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef SecureStringDef = {
	PyModuleDef_HEAD_INIT,
	"SecureString",
	NULL,
	-1,
	SecureStringMethods,
};
#endif

#if PY_MAJOR_VERSION >= 3
	PyMODINIT_FUNC PyInit_SecureString(void) {
		return PyModule_Create(&SecureStringDef);
	}
#else
	PyMODINIT_FUNC initSecureString(void)
	{
		(void) Py_InitModule("SecureString", SecureStringMethods);
	}
#endif