rippled
Loading...
Searching...
No Matches
Random.cpp
1#include <xrpl/basics/base_uint.h>
2#include <xrpl/beast/utility/rngfill.h>
3#include <xrpl/crypto/csprng.h>
4#include <xrpl/json/json_value.h>
5#include <xrpl/protocol/ErrorCodes.h>
6#include <xrpl/protocol/RPCErr.h>
7#include <xrpl/protocol/jss.h>
8
9namespace ripple {
10
11namespace RPC {
12struct JsonContext;
13}
14
15// Result:
16// {
17// random: <uint256>
18// }
21{
22 // TODO(tom): the try/catch is almost certainly redundant, we catch at the
23 // top level too.
24 try
25 {
26 uint256 rand;
27 beast::rngfill(rand.begin(), rand.size(), crypto_prng());
28
29 Json::Value jvResult;
30 jvResult[jss::random] = to_string(rand);
31 return jvResult;
32 }
33 catch (std::exception const&)
34 {
35 return rpcError(rpcINTERNAL); // LCOV_EXCL_LINE
36 }
37}
38
39} // namespace ripple
Represents a JSON value.
Definition json_value.h:131
void rngfill(void *const buffer, std::size_t const bytes, Generator &g)
Definition rngfill.h:15
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ rpcINTERNAL
Definition ErrorCodes.h:111
csprng_engine & crypto_prng()
The default cryptographically secure PRNG.
Json::Value rpcError(int iError)
Definition RPCErr.cpp:12
Json::Value doRandom(RPC::JsonContext &)
Definition Random.cpp:20
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611