如何创建GUID/UUID [英] How to create a GUID / UUID

查看:106
本文介绍了如何创建GUID/UUID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在JavaScript中创建全局唯一标识符.我不确定在所有浏览器上都可以使用哪些例程,如何确定它们的随机性"?并植入内置的随机数发生器等.

I'm trying to create globally-unique identifiers in JavaScript. I'm not sure what routines are available on all browsers, how "random" and seeded the built-in random number generator is, etc.

GUID/UUID至少应包含32个字符,并且应保持在ASCII范围内,以免在传递它们时遇到麻烦.

The GUID / UUID should be at least 32 characters and should stay in the ASCII range to avoid trouble when passing them around.

推荐答案

UUID(通用唯一IDentifier),也称为GUID(全局唯一IDentifier),根据

UUIDs (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDentifier), according to RFC 4122, are identifiers designed to provide certain uniqueness guarantees.

虽然可以在几行JavaScript代码中实现符合RFC的UUID(例如,请参见 @broofa的答案,下面)有几个常见的陷阱:

While it is possible to implement an RFC-compliant UUIDs in a few lines of JavaScript code (e.g., see @broofa's answer, below) there are several common pitfalls:

  • 无效的ID格式(UUID的格式必须为"xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx",其中x是[0-9,af] M 之一,[1-5]之一,并且 N 是[8、9,a或b]
  • 使用低质量的随机性源(例如Math.random)
  • Invalid id format (UUIDs must be of the form "xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx", where x is one of [0-9, a-f] M is one of [1-5], and N is [8, 9, a, or b]
  • Use of a low-quality source of randomness (such as Math.random)

因此,鼓励为生产环境编写代码的开发人员使用严格且维护良好的实现,例如 uuid 模块.

Thus, developers writing code for production environments are encouraged to use a rigorous, well-maintained implementation such as the uuid module.

这篇关于如何创建GUID/UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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