Keyple Card Calypso C++ Library - 3.2.2
Component of the Keyple C++ middleware
SecureRandom.cpp
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (c) 2025 Calypso Networks Association https://calypsonet.org/ *
3 * *
4 * See the NOTICE file(s) distributed with this work for additional *
5 * information regarding copyright ownership. *
6 * *
7 * This program and the accompanying materials are made available under the *
8 * terms of the Eclipse Public License 2.0 which is available at *
9 * http://www.eclipse.org/legal/epl-2.0 *
10 * *
11 * SPDX-License-Identifier: EPL-2.0 *
12 ******************************************************************************/
13
14#include "keyple/card/calypso/cpp/SecureRandom.hpp"
15
16namespace keyple {
17namespace card {
18namespace calypso {
19namespace cpp {
20
21SecureRandom::SecureRandom()
22: mSeeded(false)
23{
24}
25
26void
27SecureRandom::setSeed()
28{
29 std::random_device rd;
30
31 mGen = std::mt19937(rd());
32 mDist = std::uniform_int_distribution<int>(0, 255);
33
34 mSeeded = true;
35}
36
37void
38SecureRandom::nextBytes(std::vector<std::uint8_t>& bytes)
39{
40 if (!mSeeded) {
41 setSeed();
42 }
43
44 for (int i = 0; i < static_cast<int>(bytes.size()); i++) {
45 bytes[i] = static_cast<std::uint8_t>(mDist(mGen));
46 }
47}
48
49} /* namespace cpp */
50} /* namespace calypso */
51} /* namespace card */
52} /* namespace keyple */