Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
DuniterPy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
clients
python
DuniterPy
Commits
40b94813
Commit
40b94813
authored
6 years ago
by
Vincent Texier
Browse files
Options
Downloads
Patches
Plain Diff
[enh]
#78
Remove useless Scrypt field in armor messages
parent
1592d425
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
duniterpy/key/ascii_armor.py
+6
-38
6 additions, 38 deletions
duniterpy/key/ascii_armor.py
examples/save_cleartext_ascii_armor_message.py
+1
-2
1 addition, 2 deletions
examples/save_cleartext_ascii_armor_message.py
tests/key/test_ascii_armor.py
+9
-13
9 additions, 13 deletions
tests/key/test_ascii_armor.py
with
16 additions
and
53 deletions
duniterpy/key/ascii_armor.py
+
6
−
38
View file @
40b94813
...
...
@@ -4,7 +4,6 @@ from re import compile
from
typing
import
Optional
,
List
,
Dict
,
Any
from
duniterpy.key
import
SigningKey
,
PublicKey
,
VerifyingKey
from
duniterpy.key.scrypt_params
import
ScryptParams
# Headers constants
BEGIN_MESSAGE_HEADER
=
"
-----BEGIN DUNITER MESSAGE-----
"
...
...
@@ -66,8 +65,7 @@ class AsciiArmor:
@staticmethod
def
create
(
message
:
str
,
pubkey
:
Optional
[
str
]
=
None
,
signing_keys
:
Optional
[
List
[
SigningKey
]]
=
None
,
message_comment
:
Optional
[
str
]
=
None
,
signatures_comment
:
Optional
[
str
]
=
None
,
scrypt_params
:
Optional
[
ScryptParams
]
=
None
)
->
str
:
message_comment
:
Optional
[
str
]
=
None
,
signatures_comment
:
Optional
[
str
]
=
None
)
->
str
:
"""
Encrypt a message in ascii armor format, optionally signing it
...
...
@@ -76,8 +74,6 @@ class AsciiArmor:
:param signing_keys: Optional list of SigningKey instances
:param message_comment: Optional message comment field
:param signatures_comment: Optional signatures comment field
:param scrypt_params: Optional ScryptParams instance
:return:
"""
# if no public key and no signing key...
...
...
@@ -85,9 +81,6 @@ class AsciiArmor:
# We can not create an Ascii Armor Message
raise
MISSING_PUBLIC_KEY_AND_SIGNING_KEY_EXCEPTION
if
scrypt_params
is
None
:
scrypt_params
=
ScryptParams
()
# keep only one newline at the end of the message
message
=
message
.
rstrip
(
"
\n\r
"
)
+
"
\n
"
...
...
@@ -99,8 +92,7 @@ class AsciiArmor:
if
pubkey
:
# add encrypted message fields
ascii_armor_block
+=
"""
{version_field}
{script_field}
"""
.
format
(
version_field
=
AsciiArmor
.
_get_version_field
(),
script_field
=
AsciiArmor
.
_get_scrypt_field
(
scrypt_params
))
"""
.
format
(
version_field
=
AsciiArmor
.
_get_version_field
())
# add message comment if specified
if
message_comment
:
...
...
@@ -132,7 +124,7 @@ class AsciiArmor:
count
=
1
for
signing_key
in
signing_keys
:
ascii_armor_block
+=
AsciiArmor
.
_get_signature_block
(
message
,
signing_key
,
count
==
len
(
signing_keys
),
signatures_comment
,
scrypt_params
)
signatures_comment
)
count
+=
1
return
ascii_armor_block
...
...
@@ -177,20 +169,6 @@ class AsciiArmor:
"""
return
"
Version: {version}
"
.
format
(
version
=
VERSION_FIELD_VALUE
)
@staticmethod
def
_get_scrypt_field
(
scrypt_params
:
Optional
[
ScryptParams
]
=
None
)
->
str
:
"""
Return the Scrypt field
:param scrypt_params: Optional ScryptParams instance
:return:
"""
if
scrypt_params
is
None
:
scrypt_params
=
ScryptParams
()
return
"
Scrypt: N={0};r={1};p={2};len={3}
"
.
format
(
scrypt_params
.
N
,
scrypt_params
.
r
,
scrypt_params
.
p
,
scrypt_params
.
seed_length
)
@staticmethod
def
_get_comment_field
(
comment
:
str
)
->
str
:
"""
...
...
@@ -203,8 +181,7 @@ class AsciiArmor:
@staticmethod
def
_get_signature_block
(
message
:
str
,
signing_key
:
SigningKey
,
close_block
:
bool
=
True
,
comment
:
Optional
[
str
]
=
None
,
scrypt_params
:
Optional
[
ScryptParams
]
=
None
)
->
str
:
comment
:
Optional
[
str
]
=
None
)
->
str
:
"""
Return a signature block
...
...
@@ -212,20 +189,13 @@ class AsciiArmor:
:param signing_key: The libnacl SigningKey instance of the keypair
:param close_block: Optional flag to close the signature block with the signature tail header
:param comment: Optional comment field content
:param scrypt_params: Optional ScriptParams instance
:return:
"""
if
scrypt_params
is
None
:
scrypt_params
=
ScryptParams
()
base64_signature
=
base64
.
b64encode
(
signing_key
.
signature
(
message
))
block
=
"""
{begin_signature_header}
{version_field}
{script_field}
"""
.
format
(
begin_signature_header
=
BEGIN_SIGNATURE_HEADER
,
version_field
=
AsciiArmor
.
_get_version_field
(),
script_field
=
AsciiArmor
.
_get_scrypt_field
(
scrypt_params
))
"""
.
format
(
begin_signature_header
=
BEGIN_SIGNATURE_HEADER
,
version_field
=
AsciiArmor
.
_get_version_field
())
# add message comment if specified
if
comment
:
...
...
@@ -243,8 +213,6 @@ class AsciiArmor:
return
block
# TODO: add parse from credentials to use scrypt field creating SigningKey
@staticmethod
def
parse
(
ascii_armor_message
:
str
,
signing_key
:
Optional
[
SigningKey
]
=
None
,
sender_pubkeys
:
Optional
[
List
[
str
]]
=
None
)
->
dict
:
...
...
@@ -275,7 +243,7 @@ class AsciiArmor:
regex_end_message
=
compile
(
END_MESSAGE_HEADER
)
regex_begin_signature
=
compile
(
BEGIN_SIGNATURE_HEADER
)
regex_end_signature
=
compile
(
END_SIGNATURE_HEADER
)
regex_fields
=
compile
(
"
^(Version|
Scrypt|
Comment): (.+)$
"
)
regex_fields
=
compile
(
"
^(Version|Comment): (.+)$
"
)
# trim message to get rid of empty lines
ascii_armor_message
=
ascii_armor_message
.
strip
(
"
\t\n\r
"
)
...
...
This diff is collapsed.
Click to expand it.
examples/save_cleartext_ascii_armor_message.py
+
1
−
2
View file @
40b94813
...
...
@@ -29,8 +29,7 @@ if __name__ == '__main__':
comment
=
"
generated by Duniterpy {0}
"
.
format
(
__version__
)
# Dash escape the message and sign it
aa_cleartext_message
=
AsciiArmor
.
create
(
message
,
None
,
[
signing_key
],
None
,
signatures_comment
=
comment
)
aa_cleartext_message
=
AsciiArmor
.
create
(
message
,
None
,
[
signing_key
],
None
,
signatures_comment
=
comment
)
# Save cleartext ascii armor message in a file
with
open
(
CLEARTEXT_AA_MESSAGE_PATH
,
'
w
'
)
as
file_handler
:
...
...
This diff is collapsed.
Click to expand it.
tests/key/test_ascii_armor.py
+
9
−
13
View file @
40b94813
...
...
@@ -30,15 +30,13 @@ This is a utf-8 message...
# check before message
self
.
assertEqual
(
aa_message_lines
[
0
],
BEGIN_MESSAGE_HEADER
)
self
.
assertTrue
(
aa_message_lines
[
1
].
startswith
(
"
Version:
"
))
self
.
assertTrue
(
aa_message_lines
[
2
].
startswith
(
"
Scrypt:
"
))
self
.
assertEqual
(
""
,
aa_message_lines
[
3
].
strip
())
self
.
assertEqual
(
""
,
aa_message_lines
[
2
].
strip
())
# check after message
self
.
assertEqual
(
aa_message_lines
[
5
],
BEGIN_SIGNATURE_HEADER
)
self
.
assertTrue
(
aa_message_lines
[
6
].
startswith
(
"
Version:
"
))
self
.
assertTrue
(
aa_message_lines
[
7
].
startswith
(
"
Scrypt:
"
))
self
.
assertEqual
(
""
,
aa_message_lines
[
8
].
strip
())
self
.
assertEqual
(
aa_message_lines
[
10
],
END_SIGNATURE_HEADER
)
self
.
assertEqual
(
aa_message_lines
[
4
],
BEGIN_SIGNATURE_HEADER
)
self
.
assertTrue
(
aa_message_lines
[
5
].
startswith
(
"
Version:
"
))
self
.
assertEqual
(
""
,
aa_message_lines
[
6
].
strip
())
self
.
assertEqual
(
aa_message_lines
[
8
],
END_SIGNATURE_HEADER
)
# parse ascii armor message
result
=
AsciiArmor
.
parse
(
encrypted_and_signed_aa_message
,
alice_signing_key
,
[
bob_signing_key
.
pubkey
])
...
...
@@ -69,11 +67,10 @@ This is a utf-8 message...
# check before message
self
.
assertEqual
(
aa_message_lines
[
0
],
BEGIN_MESSAGE_HEADER
)
self
.
assertTrue
(
aa_message_lines
[
1
].
startswith
(
"
Version:
"
))
self
.
assertTrue
(
aa_message_lines
[
2
].
startswith
(
"
Scrypt:
"
))
self
.
assertEqual
(
""
,
aa_message_lines
[
3
].
strip
())
self
.
assertEqual
(
""
,
aa_message_lines
[
2
].
strip
())
# check after message
self
.
assertEqual
(
aa_message_lines
[
5
],
END_MESSAGE_HEADER
)
self
.
assertEqual
(
aa_message_lines
[
4
],
END_MESSAGE_HEADER
)
# parse ascii armor message
result
=
AsciiArmor
.
parse
(
encrypted_aa_message
,
alice_signing_key
)
...
...
@@ -107,9 +104,8 @@ This is a utf-8 message...
# check after message
self
.
assertEqual
(
aa_message_lines
[
10
],
BEGIN_SIGNATURE_HEADER
)
self
.
assertTrue
(
aa_message_lines
[
11
].
startswith
(
"
Version:
"
))
self
.
assertTrue
(
aa_message_lines
[
12
].
startswith
(
"
Scrypt:
"
))
self
.
assertEqual
(
""
,
aa_message_lines
[
13
].
strip
())
self
.
assertEqual
(
aa_message_lines
[
15
],
END_SIGNATURE_HEADER
)
self
.
assertEqual
(
""
,
aa_message_lines
[
12
].
strip
())
self
.
assertEqual
(
aa_message_lines
[
14
],
END_SIGNATURE_HEADER
)
# parse ascii armor message
result
=
AsciiArmor
.
parse
(
signed_cleartext_aa_message
,
None
,
[
bob_signing_key
.
pubkey
])
...
...
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