13
13
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
14
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
15
*/
16
-
16
+ #define PY_SSIZE_T_CLEAN
17
17
#include "Python.h"
18
18
19
+ #define PYBCRYPT_VERSION "0.4"
20
+
19
21
#if defined(_WIN32 )
20
22
typedef unsigned __int8 u_int8_t ;
21
23
typedef unsigned __int16 u_int16_t ;
22
24
typedef unsigned __int32 u_int32_t ;
23
25
#define strdup _strdup /* strdup is not ANSI C. _strdup */
24
26
#endif
25
27
26
- /* $Id$ */
27
28
28
29
/* Import */
29
30
int pybc_bcrypt (const char * , const char * , char * , size_t );
@@ -39,7 +40,7 @@ bcrypt_encode_salt(PyObject *self, PyObject *args, PyObject *kw_args)
39
40
{
40
41
static char * keywords [] = { "csalt" , "log_rounds" , NULL };
41
42
char * csalt = NULL ;
42
- int csaltlen = -1 ;
43
+ Py_ssize_t csaltlen = -1 ;
43
44
long log_rounds = -1 ;
44
45
char ret [64 ];
45
46
@@ -54,8 +55,12 @@ bcrypt_encode_salt(PyObject *self, PyObject *args, PyObject *kw_args)
54
55
PyErr_SetString (PyExc_ValueError , "Invalid number of rounds" );
55
56
return NULL ;
56
57
}
57
- encode_salt (ret , csalt , csaltlen , log_rounds );
58
- return PyString_FromString (ret );
58
+ encode_salt (ret , (u_int8_t * ) csalt , csaltlen , log_rounds );
59
+ #if PY_MAJOR_VERSION >= 3
60
+ return PyUnicode_FromString (ret );
61
+ #else
62
+ return PyString_FromString (ret );
63
+ #endif
59
64
}
60
65
61
66
PyDoc_STRVAR (bcrypt_hashpw_doc ,
@@ -72,7 +77,7 @@ bcrypt_hashpw(PyObject *self, PyObject *args, PyObject *kw_args)
72
77
int ret ;
73
78
char * password_copy ;
74
79
char * salt_copy ;
75
- if (!PyArg_ParseTupleAndKeywords (args , kw_args , "ss:hashpw" , keywords ,
80
+ if (!PyArg_ParseTupleAndKeywords (args , kw_args , "ss:hashpw" , keywords ,
76
81
& password , & salt ))
77
82
return NULL ;
78
83
@@ -93,8 +98,11 @@ bcrypt_hashpw(PyObject *self, PyObject *args, PyObject *kw_args)
93
98
PyErr_SetString (PyExc_ValueError , "Invalid salt" );
94
99
return NULL ;
95
100
}
96
-
101
+ #if PY_MAJOR_VERSION >= 3
102
+ return PyUnicode_FromString (hashed );
103
+ #else
97
104
return PyString_FromString (hashed );
105
+ #endif
98
106
}
99
107
100
108
static PyMethodDef bcrypt_methods [] = {
@@ -107,12 +115,38 @@ static PyMethodDef bcrypt_methods[] = {
107
115
108
116
PyDoc_STRVAR (module_doc , "Internal module used by bcrypt.\n" );
109
117
110
- PyMODINIT_FUNC
111
- init_bcrypt (void )
112
- {
113
- PyObject * m ;
114
-
115
- m = Py_InitModule3 ("bcrypt._bcrypt" , bcrypt_methods , module_doc );
116
- PyModule_AddStringConstant (m , "__version__" , "0.3.1" );
117
- }
118
-
118
+ #if PY_MAJOR_VERSION >= 3
119
+ static struct PyModuleDef bcrypt_module = {
120
+ PyModuleDef_HEAD_INIT ,
121
+ "bcrypt._bcrypt" , /* m_name */
122
+ module_doc , /* m_doc */
123
+ -1 , /* m_size */
124
+ bcrypt_methods , /* m_methods */
125
+ NULL , /* m_reload */
126
+ NULL , /* m_traverse */
127
+ NULL , /* m_clear */
128
+ NULL , /* m_free */
129
+ };
130
+
131
+ PyMODINIT_FUNC
132
+ PyInit__bcrypt (void )
133
+ {
134
+ PyObject * m ;
135
+
136
+ m = PyModule_Create (& bcrypt_module );
137
+ PyModule_AddStringConstant (m , "__version__" , PYBCRYPT_VERSION );
138
+ return m ;
139
+ }
140
+
141
+ #else
142
+
143
+
144
+ PyMODINIT_FUNC
145
+ init_bcrypt (void )
146
+ {
147
+ PyObject * m ;
148
+
149
+ m = Py_InitModule3 ("bcrypt._bcrypt" , bcrypt_methods , module_doc );
150
+ PyModule_AddStringConstant (m , "__version__" , PYBCRYPT_VERSION );
151
+ }
152
+ #endif
0 commit comments