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
4a5eedbf
Commit
4a5eedbf
authored
8 years ago
by
Adrien Béraud
Browse files
Options
Downloads
Patches
Plain Diff
value: add common crypto ops
parent
fdfa10bd
Branches
multidevice
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
include/opendht/value.h
+43
-0
43 additions, 0 deletions
include/opendht/value.h
src/securedht.cpp
+2
-11
2 additions, 11 deletions
src/securedht.cpp
with
45 additions
and
11 deletions
include/opendht/value.h
+
43
−
0
View file @
4a5eedbf
...
...
@@ -248,6 +248,43 @@ struct Value
return
owner
and
not
signature
.
empty
();
}
/**
* Sign the value using the provided private key.
* Afterward, checkSignature() will return true and owner will
* be set to the corresponding public key.
*/
void
sign
(
const
crypto
::
PrivateKey
&
key
)
{
if
(
isEncrypted
())
throw
DhtException
(
"Can't sign encrypted data."
);
owner
=
std
::
make_shared
<
crypto
::
PublicKey
>
(
key
.
getPublicKey
());
signature
=
key
.
sign
(
getToSign
());
}
/**
* Check that the value is signed and that the signature matches.
* If true, the owner field will contain the signer public key.
*/
bool
checkSignature
()
const
{
return
isSigned
()
and
owner
->
checkSignature
(
getToSign
(),
signature
);
}
std
::
shared_ptr
<
const
crypto
::
PublicKey
>
getOwner
()
const
{
return
std
::
static_pointer_cast
<
const
crypto
::
PublicKey
>
(
owner
);
}
/**
*
*/
Value
encrypt
(
const
crypto
::
PrivateKey
&
from
,
const
crypto
::
PublicKey
&
to
)
{
if
(
isEncrypted
())
throw
DhtException
(
"Data is already encrypted."
);
setRecipient
(
to
.
getId
());
sign
(
from
);
Value
nv
{
id
};
nv
.
setCypher
(
to
.
encrypt
(
getToEncrypt
()));
return
nv
;
}
Value
()
{}
Value
(
Id
id
)
:
id
(
id
)
{}
...
...
@@ -380,6 +417,12 @@ struct Value
void
msgpack_unpack
(
msgpack
::
object
o
);
void
msgpack_unpack_body
(
const
msgpack
::
object
&
o
);
Blob
getPacked
()
const
{
msgpack
::
sbuffer
buffer
;
msgpack
::
packer
<
msgpack
::
sbuffer
>
pk
(
&
buffer
);
pk
.
pack
(
*
this
);
return
{
buffer
.
data
(),
buffer
.
data
()
+
buffer
.
size
()};
}
Id
id
{
INVALID_ID
};
...
...
This diff is collapsed.
Click to expand it.
src/securedht.cpp
+
2
−
11
View file @
4a5eedbf
...
...
@@ -362,22 +362,13 @@ SecureDht::putEncrypted(const InfoHash& hash, const InfoHash& to, std::shared_pt
void
SecureDht
::
sign
(
Value
&
v
)
const
{
if
(
v
.
isEncrypted
())
throw
DhtException
(
"Can't sign encrypted data."
);
v
.
owner
=
std
::
make_shared
<
crypto
::
PublicKey
>
(
key_
->
getPublicKey
());
v
.
signature
=
key_
->
sign
(
v
.
getToSign
());
v
.
sign
(
*
key_
);
}
Value
SecureDht
::
encrypt
(
Value
&
v
,
const
crypto
::
PublicKey
&
to
)
const
{
if
(
v
.
isEncrypted
())
throw
DhtException
(
"Data is already encrypted."
);
v
.
setRecipient
(
to
.
getId
());
sign
(
v
);
Value
nv
{
v
.
id
};
nv
.
setCypher
(
to
.
encrypt
(
v
.
getToEncrypt
()));
return
nv
;
return
v
.
encrypt
(
*
key_
,
to
);
}
Value
...
...
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