FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
BitShares/src/bitchat_message.cpp at master · Xenland/BitShares · GitHub
Xenland
BitShares
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
BitShares
/
src
/
bitchat_message.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
134 lines (110 loc) · 3.56 KB
Breadcrumbs
BitShares
/
src
/
bitchat_message.cpp
Copy path
File metadata and controls
134 lines (110 loc) · 3.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#
include
<
bts/bitchat_message.hpp
>
#
include
<
fc/crypto/blowfish.hpp
>
#
include
<
fc/crypto/sha1.hpp
>
#
include
<
fc/exception/exception.hpp
>
#
include
<
fc/io/raw.hpp
>
namespace
bts
{
bitchat_message::bitchat_message
()
:nonce(
0
),decrypted(
false
){}
mini_pow
bitchat_message::calculate_id
()
const
{
std::vector<
char
> dat =
fc::raw::pack
( *
this
);
return
mini_pow_hash
( dat.
data
(), dat.
size
() );
}
bitchat_message&
bitchat_message::sign
(
const
fc::ecc::private_key& from )
{
if
( !private_content ) { private_content =
signed_content
(); }
private_content->
timestamp
=
fc::time_point::now
();
fc::sha256::encoder enc;
fc::raw::pack
( enc,
static_cast
<
const
content&>(*private_content) );
private_content->
from_sig
= from.
sign_compact
( enc.
result
() );
private_content->
from
= from.
get_public_key
();
return
*
this
;
}
bitchat_message&
bitchat_message::body
(
const
std::vector<
char
>& body )
{
if
( !private_content ) { private_content =
signed_content
(); }
private_content->
body
= body;
return
*
this
;
}
bitchat_message&
bitchat_message::reply_channel
(
const
network::channel_id& c )
{
if
( !private_content ) { private_content =
signed_content
(); }
private_content->
reply_channel
= c;
return
*
this
;
}
/*
*
* Encrypts the message using a random / newly generated one-time private
* key.
*/
void
bitchat_message::encrypt
(
const
fc::ecc::public_key& to)
{
if
( !private_content )
{
FC_THROW_EXCEPTION
( exception,
"
attempt to encrypt message with no content
"
);
}
auto
priv_dh_key =
fc::ecc::private_key::generate
();
auto
bf_key = priv_dh_key.
get_shared_secret
( to );
dh_key = priv_dh_key.
get_public_key
();
dh_check =
fc::sha1::hash
( bf_key ).
_hash
[
0
];
fc::blowfish bf;
bf.
start
( (
unsigned
char
*)&bf_key,
sizeof
(bf_key) );
data =
fc::raw::pack
( *private_content );
int
extra =
8
- data.
size
() %
8
;
data.
resize
( data.
size
() + extra );
bf.
encrypt
( (
unsigned
char
*)data.
data
(), data.
size
() );
decrypted =
false
;
this
->
to
=
nullptr
;
}
fc::optional<fc::ecc::private_key>
bitchat_message::get_decryption_key
()
const
{
return
to;
}
/*
*
*
* @return true if the message could be decrypted with private_key k
*/
bool
bitchat_message::decrypt
(
const
fc::ecc::private_key& k )
{
try
{
FC_ASSERT
( !decrypted );
FC_ASSERT
( data.
size
() >
0
);
FC_ASSERT
( data.
size
() %
8
==
0
);
auto
bf_key = k.
get_shared_secret
( dh_key );
auto
check =
fc::sha1::hash
( bf_key ).
_hash
[
0
];
if
( check != dh_check )
{
to =
nullptr
;
return
false
;
}
fc::blowfish bf;
bf.
start
( (
unsigned
char
*)&bf_key,
sizeof
(bf_key) );
bf.
decrypt
( (
unsigned
char
*)data.
data
(), data.
size
() );
private_content = fc::raw::unpack<signed_content>(data);
fc::sha256::encoder enc;
fc::raw::pack
( enc,
static_cast
<
const
content&>(*private_content) );
auto
digest = enc.
result
();
private_content->
from
=
fc::ecc::public_key
( private_content->
from_sig
, digest );
to = k;
return
decrypted =
true
;
}
FC_RETHROW_EXCEPTIONS
( warn,
"
error decrypting message
"
);
}
bool
bitchat_message::is_encrypted
()
const
{
return
!decrypted;
}
const
bitchat_message::signed_content&
bitchat_message::get_content
()
const
{
if
( !private_content )
{
FC_THROW_EXCEPTION
( exception,
"
no private content set in message
"
);
}
return
*private_content;
}
void
bitchat_message::set_content
(
const
signed_content& c)
{
private_content = c;
}
}
//
namespace bts
Back
|
FazBrowse Home
|
New Git URL