Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Dunitrust
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
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
nodes
rust
Dunitrust
Commits
1a44728f
Commit
1a44728f
authored
7 years ago
by
Éloïs
Browse files
Options
Downloads
Patches
Plain Diff
[enh]
#13
add keypair type
parent
1417b5dc
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!9
Resolve "Add keyPair type"
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
keys/ed25519.rs
+68
-13
68 additions, 13 deletions
keys/ed25519.rs
keys/lib.rs
+12
-0
12 additions, 0 deletions
keys/lib.rs
with
80 additions
and
13 deletions
keys/ed25519.rs
+
68
−
13
View file @
1a44728f
...
@@ -211,6 +211,45 @@ impl super::PrivateKey for PrivateKey {
...
@@ -211,6 +211,45 @@ impl super::PrivateKey for PrivateKey {
}
}
}
}
/// Store a ed25519 cryptographic key pair (`PublicKey` + `PrivateKey`)
#[derive(Debug,
Copy,
Clone)]
pub
struct
KeyPair
{
/// Store a Ed25519 public key.
pub
pubkey
:
PublicKey
,
/// Store a Ed25519 private key.
privkey
:
PrivateKey
,
}
impl
Display
for
KeyPair
{
fn
fmt
(
&
self
,
f
:
&
mut
Formatter
)
->
Result
<
(),
Error
>
{
write!
(
f
,
"{}"
,
self
.pubkey
.to_base58
())
}
}
impl
PartialEq
<
KeyPair
>
for
KeyPair
{
fn
eq
(
&
self
,
other
:
&
KeyPair
)
->
bool
{
self
.pubkey
.eq
(
&
other
.pubkey
)
&&
self
.privkey
.eq
(
&
other
.privkey
)
}
}
impl
Eq
for
KeyPair
{}
impl
super
::
KeyPair
for
KeyPair
{
type
Signature
=
Signature
;
fn
new
(
password
:
&
[
u8
],
salt
:
&
[
u8
])
->
KeyPair
{
let
generator
=
self
::
KeyPairGenerator
::
with_default_parameters
();
let
(
privkey
,
pubkey
)
=
generator
.generate
(
password
,
salt
);
KeyPair
{
pubkey
,
privkey
}
}
fn
sign
(
&
self
,
message
:
&
[
u8
])
->
Signature
{
Signature
(
crypto
::
ed25519
::
signature
(
message
,
&
self
.privkey
.0
))
}
}
/// Keypair generator with given parameters for `scrypt` keypair function.
/// Keypair generator with given parameters for `scrypt` keypair function.
#[derive(Debug,
Copy,
Clone)]
#[derive(Debug,
Copy,
Clone)]
pub
struct
KeyPairGenerator
{
pub
struct
KeyPairGenerator
{
...
@@ -264,7 +303,7 @@ impl KeyPairGenerator {
...
@@ -264,7 +303,7 @@ impl KeyPairGenerator {
#[cfg(test)]
#[cfg(test)]
mod
tests
{
mod
tests
{
use
super
::
*
;
use
super
::
*
;
use
{
PrivateKey
,
PublicKey
,
Signature
};
use
{
PrivateKey
,
PublicKey
,
Signature
,
KeyPair
};
#[test]
#[test]
fn
base58_private_key
()
{
fn
base58_private_key
()
{
...
@@ -394,4 +433,20 @@ Timestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
...
@@ -394,4 +433,20 @@ Timestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
assert_eq!
(
sig
,
expected_signature
);
assert_eq!
(
sig
,
expected_signature
);
assert!
(
pubkey
.verify
(
message
.as_bytes
(),
&
sig
));
assert!
(
pubkey
.verify
(
message
.as_bytes
(),
&
sig
));
}
}
#[test]
fn
keypair_generate_sign_and_verify
()
{
let
keypair
=
super
::
KeyPair
::
new
(
"password"
.as_bytes
(),
"salt"
.as_bytes
());
let
message
=
"Version: 10
Type: Identity
Currency: duniter_unit_test_currency
Issuer: DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV
UniqueID: tic
Timestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
"
;
let
sig
=
keypair
.sign
(
message
.as_bytes
());
assert!
(
keypair
.pubkey
.verify
(
message
.as_bytes
(),
&
sig
));
}
}
}
This diff is collapsed.
Click to expand it.
keys/lib.rs
+
12
−
0
View file @
1a44728f
...
@@ -147,3 +147,15 @@ pub trait PrivateKey: Clone + Display + Debug + PartialEq + Eq + ToBase58 {
...
@@ -147,3 +147,15 @@ pub trait PrivateKey: Clone + Display + Debug + PartialEq + Eq + ToBase58 {
/// Sign a message with this private key.
/// Sign a message with this private key.
fn
sign
(
&
self
,
message
:
&
[
u8
])
->
Self
::
Signature
;
fn
sign
(
&
self
,
message
:
&
[
u8
])
->
Self
::
Signature
;
}
}
/// Store a cryptographic key pair (`PublicKey` + `PrivateKey`)
pub
trait
KeyPair
:
Clone
+
Display
+
Debug
+
PartialEq
+
Eq
{
/// Signature type of associated cryptosystem.
type
Signature
:
Signature
;
/// Create a new KeyPair
fn
new
(
password
:
&
[
u8
],
salt
:
&
[
u8
])
->
Self
;
/// Sign a message with privkey.
fn
sign
(
&
self
,
message
:
&
[
u8
])
->
Self
::
Signature
;
}
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