-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfnv.pyx
80 lines (64 loc) · 2.17 KB
/
fnv.pyx
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Author: Suresh Sundriyal
# License: CC0 - No rights reserved.
import cython
cdef extern from "<stdint.h>" nogil:
ctypedef unsigned long long uint64_t
cdef extern from "fnv.h" nogil:
void c_fnv1_64 "fnv1_64"(const char *msg,
const int length,
uint64_t *hash)
void c_fnv1a_64 "fnv1a_64"(const char *msg,
const int length,
uint64_t *hash)
@cython.final
cdef class fnv1_64:
cdef uint64_t h
cdef readonly char *name
cdef readonly uint64_t blocksize
cdef readonly uint64_t digestsize
cdef readonly uint64_t digest_size
def __cinit__(self, s=None):
self.h = <uint64_t>0
self.name = "fnv1_64"
self.blocksize = <uint64_t>64
self.digestsize = <uint64_t>8
self.digest_size = <uint64_t>8
if s is not None:
self.update(s)
cpdef void update(self, bytes s):
cdef const char * c_string = s
cdef Py_ssize_t length = len(s)
with nogil:
c_fnv1_64(c_string, length, &self.h)
cpdef uint64_t digest(self) nogil:
return self.h
cpdef char * hexdigest(self):
return b'%x' % self.h
cpdef void reset(self) nogil:
self.h = <uint64_t>0
@cython.final
cdef class fnv1a_64:
cdef uint64_t h
cdef readonly char *name
cdef readonly uint64_t blocksize
cdef readonly uint64_t digestsize
cdef readonly uint64_t digest_size
def __cinit__(self, s=None):
self.h = <uint64_t>0
self.name = "fnv1a_64"
self.blocksize = <uint64_t>64
self.digestsize = <uint64_t>8
self.digest_size = <uint64_t>8
if s is not None:
self.update(s)
cpdef void update(self, bytes s):
cdef const char * c_string = s
cdef Py_ssize_t length = len(s)
with nogil:
c_fnv1a_64(c_string, length, &self.h)
cpdef uint64_t digest(self) nogil:
return self.h
cpdef char * hexdigest(self):
return b'%x' % self.h
cpdef void reset(self):
self.h = <uint64_t>0