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
274e2192
Commit
274e2192
authored
12 years ago
by
Donald Stufft
Browse files
Options
Downloads
Patches
Plain Diff
Generate the shared key upfront
parent
b5cd3774
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
nacl/public.py
+13
-21
13 additions, 21 deletions
nacl/public.py
with
13 additions
and
21 deletions
nacl/public.py
+
13
−
21
View file @
274e2192
...
@@ -108,7 +108,17 @@ class Box(object):
...
@@ -108,7 +108,17 @@ class Box(object):
self
.
_public_key
=
public_key
self
.
_public_key
=
public_key
self
.
_private_key
=
private_key
self
.
_private_key
=
private_key
self
.
_shared_key
=
None
_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
,
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
)[:]
def
encrypt
(
self
,
plaintext
,
nonce
,
encoder
=
encoding
.
RawEncoder
):
def
encrypt
(
self
,
plaintext
,
nonce
,
encoder
=
encoding
.
RawEncoder
):
"""
"""
...
@@ -136,7 +146,7 @@ class Box(object):
...
@@ -136,7 +146,7 @@ class Box(object):
padded
,
padded
,
len
(
padded
),
len
(
padded
),
nonce
,
nonce
,
self
.
_
beforenm
()
self
.
_
shared_key
,
):
):
raise
CryptoError
(
"
Encryption failed
"
)
raise
CryptoError
(
"
Encryption failed
"
)
...
@@ -170,7 +180,7 @@ class Box(object):
...
@@ -170,7 +180,7 @@ class Box(object):
padded
,
padded
,
len
(
padded
),
len
(
padded
),
nonce
,
nonce
,
self
.
_
beforenm
()
self
.
_
shared_key
,
):
):
raise
CryptoError
(
raise
CryptoError
(
"
Decryption failed. Ciphertext failed verification
"
)
"
Decryption failed. Ciphertext failed verification
"
)
...
@@ -179,21 +189,3 @@ class Box(object):
...
@@ -179,21 +189,3 @@ class Box(object):
plaintext
=
nacl
.
ffi
.
buffer
(
plaintext
,
len
(
padded
))[
box_zeros
:]
plaintext
=
nacl
.
ffi
.
buffer
(
plaintext
,
len
(
padded
))[
box_zeros
:]
return
plaintext
return
plaintext
def
_beforenm
(
self
):
if
self
.
_shared_key
is
not
None
:
return
self
.
_shared_key
sharedkey_size
=
nacl
.
lib
.
crypto_box_BEFORENMBYTES
k
=
nacl
.
ffi
.
new
(
"
unsigned char[]
"
,
sharedkey_size
)
if
not
nacl
.
lib
.
crypto_box_beforenm
(
k
,
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
(
k
,
sharedkey_size
)[:]
return
self
.
_shared_key
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