猿问

如何使用 C# 生成 UUID 版本 4

我的要求是从 C# 代码为谷歌 API 会话令牌生成版本 4 UUID,我不确定Guid.NewGuid()方法,它返回哪个版本的 GUID。喜欢的版本

阅读谷歌和博客但没有得到确定的答案 Guid.NewGuid() 是否根据 RFC4122 生成 UUID 版本 4?

提前致谢


喵喔喔
浏览 246回答 1
1回答

Helenr

GUID 是 V4...您生成的每个 GUID 都将如下所示18acac20-991e-437e-9529-a441452f6b7ed6d68639-64c2-452e-95b7-16cf6dbf5301b0943b6d-4779-4771-92bf-cc2d634fb671218b5620-d30d-46d9-9c88-38a4ac64266ede03042c-792f-4689-80ca-26287ceb21291175bb5d-d35e-4a46-aaac-0825c749dc3a42864583-c0f6-4e44-8710-39c9a9146d43223ca924-4b77-4931-bb94-c1d3718946832c4495ab-19e4-4aeb-b647-10db8625791cf5894345-cbe3-4fc7-92c3-d6d863f70411&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ^&nbsp; &nbsp; ^&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; 21上面位置的数字总是位置上4的数字2总是8, 9,A或中的一个B。您可以通过以下方式确认var cts = new CancellationTokenSource();var parallelOptions = new ParallelOptions() { MaxDegreeOfParallelism = 8, CancellationToken = cts.Token };Parallel.For(0, int.MaxValue, parallelOptions, (i) =>{&nbsp; &nbsp; var guid = Guid.NewGuid().ToString();&nbsp; &nbsp; var four = guid.ElementAt(14);&nbsp; &nbsp; var ab89 = guid.ElementAt(19);&nbsp; &nbsp; if (four != '4') cts.Cancel();&nbsp; &nbsp; if (ab89 != 'a' && ab89 != 'b' && ab89 != '8' && ab89 != '9') cts.Cancel();&nbsp; &nbsp; if ((i % 100000) == 0 && i < (int.MaxValue / 8))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"{i * 8:n}"); // roughly&nbsp; &nbsp;&nbsp; &nbsp; }});如果您有疑问,这将在合理的时间内进行 40 亿次尝试
随时随地看视频慕课网APP
我要回答