Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
sakia
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
clients
python
sakia
Commits
8a5a2924
Commit
8a5a2924
authored
12 years ago
by
Donald Stufft
Browse files
Options
Downloads
Patches
Plain Diff
Make Box Encodable to bytes
parent
d457878b
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
nacl/public.py
+25
-12
25 additions, 12 deletions
nacl/public.py
with
25 additions
and
12 deletions
nacl/public.py
+
25
−
12
View file @
8a5a2924
...
...
@@ -82,7 +82,7 @@ class PrivateKey(encoding.Encodable, six.StringFixer, object):
)
class
Box
(
object
):
class
Box
(
encoding
.
Encodable
,
six
.
StringFixer
,
object
):
"""
The Box class boxes and unboxes messages between a pair of keys
...
...
@@ -105,20 +105,33 @@ class Box(object):
NONCE_SIZE
=
nacl
.
lib
.
crypto_box_NONCEBYTES
def
__init__
(
self
,
public_key
,
private_key
):
self
.
_public_key
=
public_key
self
.
_private_key
=
private_key
if
public_key
and
private_key
:
_shared_key_size
=
nacl
.
lib
.
crypto_box_BEFORENMBYTES
_shared_key
=
nacl
.
ffi
.
new
(
"
unsigned char[]
"
,
_shared_key_size
)
_shared_key_size
=
nacl
.
lib
.
crypto_box_BEFORENMBYTES
_shared_key
=
nacl
.
ffi
.
new
(
"
unsigned char[]
"
,
_shared_key_size
)
if
not
nacl
.
lib
.
crypto_box_beforenm
(
_shared_key
,
public_key
.
encode
(
encoder
=
encoding
.
RawEncoder
),
private_key
.
encode
(
encoder
=
encoding
.
RawEncoder
),
):
raise
CryptoError
(
"
Failed to derive shared key
"
)
if
not
nacl
.
lib
.
crypto_box_beforenm
(
_shared_key
,
self
.
_public_key
.
encode
(
encoder
=
encoding
.
RawEncoder
),
self
.
_private_key
.
encode
(
encoder
=
encoding
.
RawEncoder
),
):
raise
CryptoError
(
"
Failed to derive shared key
"
)
self
.
_shared_key
=
nacl
.
ffi
.
buffer
(
_shared_key
,
_shared_key_size
)[:]
else
:
self
.
_shared_key
=
None
def
__bytes__
(
self
):
return
self
.
_shared_key
@classmethod
def
decode
(
cls
,
encoded
,
encoder
=
encoding
.
RawEncoder
):
# Create an empty box
box
=
cls
(
None
,
None
)
# Assign our decoded value to the shared key of the box
box
.
_shared_key
=
encoder
.
decode
(
encoded
)
self
.
_shared_key
=
nacl
.
ffi
.
buffer
(
_shared_key
,
_shared_key_size
)[:]
return
box
def
encrypt
(
self
,
plaintext
,
nonce
,
encoder
=
encoding
.
RawEncoder
):
"""
...
...
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