Tor*_*zki 5 security boost-random
对于 C++ Web 服务器,我必须生成会话 ID。我考虑使用某种随机数和哈希值,以及会话的初始 IP 地址和时间戳。
这会产生一个合理且不可猜测的 ID 吗?什么是一种好的随机生成器算法(最优选由 boost-random 实现的算法)?
亲切的问候托斯顿
我的解决方案现在看起来像:
std::string secure_session_generator::operator()( const char* /* network_connection_name */ )
{
std::stringstream out;
out << std::hex << distribution_( generator_ );
return out.str();
}
Run Code Online (Sandbox Code Playgroud)
成员是默认构造的:
boost::random::random_device generator_;
boost::random::uniform_int_distribution< boost::uint_least64_t > distribution_;
Run Code Online (Sandbox Code Playgroud)
您可以使用此处的示例:Boost example。然后只需将大小增加到更适合会话 ID 的值,例如 64 个字符或其他字符。这样你就不必使用散列或任何东西的计算,并且它已经是可读的。
或者不使用 boost-random 而仅使用 ctime 和 stdio.h
string getRandom(int ip)
{
srand(time(NULL) + ip + rand());
stringstream ss;
for(int i = 0;i < 64;i++)
{
int i = rand() % 127;
while(i < 32)
i = rand() % 127;
ss << char(i);
}
return ss.str();
}
Run Code Online (Sandbox Code Playgroud)
或者,在不使用 IP 的情况下,您可以直接用 rand() 代替 IP,只需确保您用某些东西作为种子 srand 即可。
另外,无论如何,我不是密码学家,因此使用风险由您自行承担。
| 归档时间: |
|
| 查看次数: |
8330 次 |
| 最近记录: |