在JavaScript中创建GUID / UUID? [英] Create GUID / UUID in JavaScript?

查看:232
本文介绍了在JavaScript中创建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.

推荐答案

UUIDs(普遍唯一) IDentifier),也称为GUID(全球唯一标识符),根据 RFC 4122 ,是具有特定唯一性保证的标识符。

UUIDs (Universally Unique IDentifier), also known as GUIDs (Globally Unique IDentifier), according to RFC 4122, are identifiers with a certain uniqueness guarantee.

生成它们的最佳方法是遵循上述RFC中的实现说明,使用众多社区审查的开源实现之一或者,对于拥有它的语言,使用内置实现。

The best way to generate them, is to follow implementation instructions in the said RFC, use one of the many community vetted open source implementations, or, for the languages that have it, use the built-in implementation.

这里列出了一些用于处理UUID的开源工具,对于一些流行的编程语言。

Some example open source tools for working with UUIDs, for some popular programming languages are listed here.

JavaScript

  • https://github.com/kelektiv/node-uuid

PHP

  • https://github.com/ramsey/uuid

Go


  • < a href =https://github.com/satori/go.uuid =nofollow noreferrer> https://github.com/satori/go.uuid

  • https://github.com/satori/go.uuid

Ruby

  • Part of the Standard Library

Python

  • In the UUID module

请注意,只是随机逐字节或逐字符生成标识符不会为符合要求的实现提供相同的保证。此外,非常重要的是,使用兼容UUID的系统可能选择不接受随机生成的系统,并且许多开源验证器实际上将检查有效结构。

Note that just randomly generating the identifiers byte by byte, or character by character, will not give you the same guarantees as a conforming implementation. Also, very important, systems working with compliant UUIDs may choose not to accept randomly generated ones, and many open source validators will actually check for a valid structure.

UUID必须有这种格式:

An UUID must have this format:

xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

M N 位置可能只有某些值。此时,M的唯一有效值是1,2,3,4和5,因此随机生成该位置会使大多数结果无法接受。

Where the M and N positions may only have certain values. At this time, the only valid values for M are 1, 2, 3, 4 and 5, so randomly generating that position would make most results unacceptable.

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

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