xrpld
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
9#include <exception>
10
11namespace xrpl {
12
13namespace RPC {
14struct JsonContext;
15} // namespace RPC
16
17// Result:
18// {
19// random: <uint256>
20// }
21json::Value
23{
24 // TODO(tom): the try/catch is almost certainly redundant, we catch at the
25 // top level too.
26 try
27 {
28 uint256 rand;
29 beast::rngfill(rand.begin(), rand.size(), cryptoPrng());
30
31 json::Value jvResult;
32 jvResult[jss::random] = to_string(rand);
33 return jvResult;
34 }
35 catch (std::exception const&)
36 {
37 return rpcError(RpcInternal); // LCOV_EXCL_LINE
38 }
39}
40
41} // namespace xrpl
Represents a JSON value.
Definition json_value.h:130
void rngfill(void *const buffer, std::size_t const bytes, Generator &g)
Definition rngfill.h:14
API version numbers used in later API versions.
Definition ApiVersion.h:35
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ RpcInternal
Definition ErrorCodes.h:112
CsprngEngine & cryptoPrng()
The default cryptographically secure PRNG.
std::string to_string(BaseUInt< Bits, Tag > const &a)
Definition base_uint.h:633
json::Value rpcError(ErrorCodeI iError)
Definition RPCErr.cpp:13
json::Value doRandom(RPC::JsonContext &)
Definition Random.cpp:22
BaseUInt< 256 > uint256
Definition base_uint.h:562