它是安全的假设提振:: UUID不会返回重复? [英] Is it safe to assume boost::uuid won't return a duplicate?

查看:508
本文介绍了它是安全的假设提振:: UUID不会返回重复?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用升压UUID生成会话ID。

I'm using boost uuid to generate session ids.

std::string SessionGenerator::generate()
{
    boost::uuids::uuid id = m_rgen();
    m_ss.clear();
    m_ss.str("");
    m_ss << id;

    return m_ss.str();
}

它是安全的假设,我永远不会得到重复的或者我应该做的检查,对活动的会话?

Is it safe to assume that I'm never going to get a duplicate or should I be doing checks against active sessions?

感谢

推荐答案

嗯,这要看情况。

当的UUID被定义的机制中的一个产生,他们要么保证是唯一 与其他所有产生的UUID不同(即,它从来没有之前生成的,它绝不会再次生成),或者它的极有可能是唯一的(取决于机制)。

When UUIDs are generated by one of the defined mechanisms, they are either guaranteed to be unique, different from all other generated UUIDs (that is, it has never been generated before and it will never be generated again), or it is extremely likely to be unique (depending on the mechanism).

这意味着,该问题可能是您正在使用发电机。他们说,他们正在使用 ITU-T规范

It means that the problem may be on the generator you're using. They say they're using ITU-T specification.

让我们去文档的第7页。如果你使用的时间,你可以假设:

Let's go to page 7 of the document. If you're using time and you can assume that:


  • 系统时间不会改变。

  • 节点ID用来识别​​机器将不会改变。

然后你至少可以断言:

的UUID将是所有其他产生不同的UUID的因为时间流量和粒度为100纳秒。

"The UUID will be different from all other generated UUIDs" because time flows and the granularity is 100 ns.

有可能会发生碰撞,如果你需要与其他机器或时间会改变产生的共享UUID(不要忘记,每年两​​次在许多国家有时间调整)。这就是为什么有一个的时钟序列的领域。此外,它是pretty小,所以在这种情况下,你属于断言:

There could be a collision if you need to share generated UUID with other machines or the time will change (do not forget that twice per year in many country there is time adjustment). That's why there is a Clock sequence field. Moreover it's pretty small so in this case you fall to assert that:

UUID是极有可能是唯一的。

如果您正在使用,而不是这个,一个随机数生成器,那么你只能在断言:

If you're using, instead of this, a random number generator then you can assert only that:

UUID是极有可能是唯一的。的,因为一个随机数发生器的要求是不产生唯一的数字(但具有良好的随机数发生器的极有可能的将是的极有可能的)。

"The UUID is extremely likely to be unique." because the requirement of a random number generator is not to generate unique numbers (but with a good random number generator your extremely likely will be EXTREMELY likely).

所以的正常的条件(例如,如果你不从一台计算机移动一个网卡对方和你改变的时候回到过去)我想你可以假设它们是唯一的(使用时间)。如果您使用的是随机数发生器你不能假设他们是独一无二的,但只是极有可能是唯一的(约碰撞概率...好...如果它具有良好的随机数生成器,你应该留在发生的下家流星雨)。

So in normal conditions (for example if you do not move one network card from one computer to the other and you change the time back to the past) I suppose that you can assume they're unique (using time). If you're using a random number generator you can't assume they're unique but just extremely likely to be unique (about probability of collisions...well...if it happens with a good random number generator you should stay at home for the next meteor shower).

参考

http://blogs.msdn.com/b/oldnewthing /archive/2008/06/27/8659071.aspx

http://en.wikipedia.org/wiki/Birthday_attack

这篇关于它是安全的假设提振:: UUID不会返回重复?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆