Newer
Older
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
#include <vereign/crypto/bio.hh>
#include <vereign/crypto/rsa.hh>
#include <vereign/encoding/base64.hh>
namespace {
constexpr int rsaKeySizeBits = 2048;
}
namespace vereign::identity {
Provider::Provider(kvstore::CryptoStorage& storage)
: storage_{storage}
{}
Provider::~Provider() = default;
auto Provider::ResetIdentity(const std::string& pin) -> std::string {
std::lock_guard<std::mutex> l{mu_};
storage_.Reset(pin);
auto rsa = crypto::rsa::GenerateKey(rsaKeySizeBits);
auto private_key = crypto::rsa::ExportPrivateKeyToPEM(rsa.get());
storage_.PutBytes("identity_private_key", crypto::bio::View(private_key.get()));
auto public_key = crypto::rsa::ExportPublicKeyToPEM(rsa.get());
storage_.PutBytes("identity_public_key", crypto::bio::View(public_key.get()));
bytes::Buffer encoded;
encoding::base64::Encode(crypto::bio::View(public_key.get()), encoded);
return std::string{encoded.View().String()};
}
auto Provider::LoadIdentity(const std::string& pin) -> std::string {
std::lock_guard<std::mutex> l{mu_};
storage_.Open(pin);
bytes::Buffer public_key;
storage_.GetBytes("identity_public_key", public_key);
bytes::Buffer encoded;
encoding::base64::Encode(public_key.View(), encoded);
return std::string(encoded.View().String());
}
auto Provider::GetIdentityPublicKeyBase64() -> std::string {
std::lock_guard<std::mutex> l{mu_};
bytes::Buffer public_key;
storage_.GetBytes("identity_public_key", public_key);
bytes::Buffer encoded;
encoding::base64::Encode(public_key.View(), encoded);
return std::string(encoded.View().String());
}
auto Provider::GetDeviceHash() -> std::string {
std::lock_guard<std::mutex> l{mu_};
bytes::Buffer public_key;
storage_.GetBytes("identity_public_key", public_key);
bytes::Buffer hash;
crypto::digest::sha1(public_key.View(), hash);
bytes::Buffer encoded;
encoding::base64::Encode(hash.View(), encoded);
return std::string(encoded.View().String());
}
} // namespace vereign::identity