summaryrefslogtreecommitdiffstats
path: root/Objects
diff options
context:
space:
mode:
Diffstat (limited to 'Objects')
-rw-r--r--Objects/object.c13
-rw-r--r--Objects/unicodeobject.c12
2 files changed, 19 insertions, 6 deletions
diff --git a/Objects/object.c b/Objects/object.c
index 8134825..bb18d47 100644
--- a/Objects/object.c
+++ b/Objects/object.c
@@ -759,10 +759,19 @@ _Py_HashBytes(unsigned char *p, Py_ssize_t len)
Py_uhash_t x;
Py_ssize_t i;
- x = (Py_uhash_t) *p << 7;
+ /*
+ We make the hash of the empty string be 0, rather than using
+ (prefix ^ suffix), since this slightly obfuscates the hash secret
+ */
+ if (len == 0) {
+ return 0;
+ }
+ x = (Py_uhash_t) _Py_HashSecret.prefix;
+ x ^= (Py_uhash_t) *p << 7;
for (i = 0; i < len; i++)
x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *p++;
x ^= (Py_uhash_t) len;
+ x ^= (Py_uhash_t) _Py_HashSecret.suffix;
if (x == -1)
x = -2;
return x;
@@ -776,6 +785,8 @@ PyObject_HashNotImplemented(PyObject *v)
return -1;
}
+_Py_HashSecret_t _Py_HashSecret;
+
Py_hash_t
PyObject_Hash(PyObject *v)
{
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 07d3eb8..716ca3f 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -11221,11 +11221,12 @@ unicode_hash(PyObject *self)
len = PyUnicode_GET_LENGTH(self);
/* The hash function as a macro, gets expanded three times below. */
-#define HASH(P) \
- x = (Py_uhash_t)*P << 7; \
- while (--len >= 0) \
- x = (_PyHASH_MULTIPLIER*x) ^ (Py_uhash_t)*P++;
+#define HASH(P) \
+ x ^= (Py_uhash_t) *P << 7; \
+ while (--len >= 0) \
+ x = (_PyHASH_MULTIPLIER * x) ^ (Py_uhash_t) *P++; \
+ x = (Py_uhash_t) _Py_HashSecret.prefix;
switch (PyUnicode_KIND(self)) {
case PyUnicode_1BYTE_KIND: {
const unsigned char *c = PyUnicode_1BYTE_DATA(self);
@@ -11246,7 +11247,8 @@ unicode_hash(PyObject *self)
break;
}
}
- x ^= (Py_uhash_t)PyUnicode_GET_LENGTH(self);
+ x ^= (Py_uhash_t) PyUnicode_GET_LENGTH(self);
+ x ^= (Py_uhash_t) _Py_HashSecret.suffix;
if (x == -1)
x = -2;