Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
opendht
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
savoirfairelinux
opendht
Commits
a952ddec
Commit
a952ddec
authored
8 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
python: add crypto encrypt/decrypt
parent
fa7efd08
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
python/opendht.pyx
+18
-0
18 additions, 0 deletions
python/opendht.pyx
python/opendht_cpp.pxd
+4
-0
4 additions, 0 deletions
python/opendht_cpp.pxd
with
22 additions
and
0 deletions
python/opendht.pyx
+
18
−
0
View file @
a952ddec
...
...
@@ -251,6 +251,15 @@ cdef class PrivateKey(_WithID):
pk
=
PublicKey
()
pk
.
_key
=
self
.
_key
.
get
().
getPublicKey
()
return
pk
def
decrypt
(
self
,
bytes
dat
):
cdef
size_t
d_len
=
len
(
dat
)
cdef
cpp
.
uint8_t
*
d_ptr
=
<
cpp
.
uint8_t
*>
dat
cdef
cpp
.
Blob
indat
indat
.
assign
(
d_ptr
,
<
cpp
.
uint8_t
*>
(
d_ptr
+
d_len
))
cdef
cpp
.
Blob
decrypted
=
self
.
_key
.
get
().
decrypt
(
indat
)
cdef
char
*
decrypted_c_str
=
<
char
*>
decrypted
.
data
()
cdef
Py_ssize_t
length
=
decrypted
.
size
()
return
decrypted_c_str
[:
length
]
def
__str__
(
self
):
return
self
.
getId
().
toString
().
decode
()
@staticmethod
...
...
@@ -265,6 +274,15 @@ cdef class PublicKey(_WithID):
h
=
InfoHash
()
h
.
_infohash
=
self
.
_key
.
getId
()
return
h
def
encrypt
(
self
,
bytes
dat
):
cdef
size_t
d_len
=
len
(
dat
)
cdef
cpp
.
uint8_t
*
d_ptr
=
<
cpp
.
uint8_t
*>
dat
cdef
cpp
.
Blob
indat
indat
.
assign
(
d_ptr
,
<
cpp
.
uint8_t
*>
(
d_ptr
+
d_len
))
cdef
cpp
.
Blob
encrypted
=
self
.
_key
.
encrypt
(
indat
)
cdef
char
*
encrypted_c_str
=
<
char
*>
encrypted
.
data
()
cdef
Py_ssize_t
length
=
encrypted
.
size
()
return
encrypted_c_str
[:
length
]
cdef
class
Certificate
(
_WithID
):
cdef
shared_ptr
[
cpp
.
Certificate
]
_cert
...
...
This diff is collapsed.
Click to expand it.
python/opendht_cpp.pxd
+
4
−
0
View file @
a952ddec
...
...
@@ -77,6 +77,8 @@ cdef extern from "opendht/sockaddr.h" namespace "dht":
sa_family_t
getFamily
()
const
void
setFamily
(
sa_family_t
f
)
ctypedef
vector
[
uint8_t
]
Blob
cdef
extern
from
"
opendht/crypto.h
"
namespace
"
dht::crypto
"
:
ctypedef
pair
[
shared_ptr
[
PrivateKey
],
shared_ptr
[
Certificate
]]
Identity
cdef
Identity
generateIdentity
(
string
name
,
Identity
ca
,
unsigned
bits
)
...
...
@@ -84,12 +86,14 @@ cdef extern from "opendht/crypto.h" namespace "dht::crypto":
cdef
cppclass
PrivateKey
:
PrivateKey
()
PublicKey
getPublicKey
()
const
Blob
decrypt
(
Blob
data
)
const
@staticmethod
PrivateKey
generate
()
cdef
cppclass
PublicKey
:
PublicKey
()
InfoHash
getId
()
const
Blob
encrypt
(
Blob
data
)
const
cdef
cppclass
Certificate
:
Certificate
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment