关于 GUID 的规律
作者:雄鹿 @
- 2024-06-01 广东
本文字数:1953 字
阅读完需:约 6 分钟

先决条件
根据代码看规律
如果是代码,可以看到 Guid 是非常有规律的
byte[] bytes;bytes = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };Console.WriteLine(new Guid(bytes)); // 00000000-0000-0000-0000-000000000000bytes = new byte[16] { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };Console.WriteLine(new Guid(bytes)); // ffffffff-ffff-ffff-ffff-ffffffffffff
bytes = new byte[16] { 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };Console.WriteLine(new Guid(bytes)); // 04030201-0000-0000-0000-000000000000bytes = new byte[16] { 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };Console.WriteLine(new Guid(bytes)); // 00000000-0201-0000-0000-000000000000bytes = new byte[16] { 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0 };Console.WriteLine(new Guid(bytes)); // 00000000-0000-0201-0000-000000000000bytes = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0 };Console.WriteLine(new Guid(bytes)); // 00000000-0000-0000-0102-000000000000bytes = new byte[16] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6 };Console.WriteLine(new Guid(bytes)); // 00000000-0000-0000-0000-010203040506复制代码
Guid 与 int 互相转换
未混淆
static Guid Int2Guid(int value){ byte[] bytes = new byte[16]; BitConverter.GetBytes(value).CopyTo(bytes, 1); var result = new Guid(bytes); return result;}
static int Guid2Int(Guid value){ byte[] b = value.ToByteArray(); int bint = BitConverter.ToInt32(b, 1); return bint;}
for (int i = 1; i < 10; i++){ var guid = Int2Guid(i); var id = Guid2Int(guid); Console.WriteLine(guid); Console.WriteLine(id);}复制代码
控制台打印的结果如下,可以看到非常有规律
00000100-0000-0000-0000-000000000000100000200-0000-0000-0000-000000000000200000300-0000-0000-0000-000000000000300000400-0000-0000-0000-000000000000400000500-0000-0000-0000-000000000000500000600-0000-0000-0000-000000000000600000700-0000-0000-0000-000000000000700000800-0000-0000-0000-000000000000800000900-0000-0000-0000-0000000000009复制代码
增加混淆
static Guid Int2Guid(int value){ var r = new Random(value); byte[] bytes = new byte[16]; r.NextBytes(bytes); BitConverter.GetBytes(value).CopyTo(bytes, 1); var result = new Guid(bytes); return result;}
static int Guid2Int(Guid value){ byte[] b = value.ToByteArray(); int bint = BitConverter.ToInt32(b, 1); return bint;}
for (int i = 1; i < 10; i++){ var guid = Int2Guid(i); var id = Guid2Int(guid); Console.WriteLine(guid); Console.WriteLine(id);}复制代码
控制台打印的结果如下,虽然混淆,但是还能看到其规律
00000146-9700-a3e4-95cf-ff46699c73c4100000271-b900-6fe3-7c38-98b7dd200bc120000039d-db00-3ae3-63a0-322750a4a3bd3000004c8-fd00-06e2-4a08-cc98c4273aba4000005f4-1e00-d1e2-3171-650938abd2b750000061f-4000-9de1-18d9-ff7aab2e6ab360000074b-6200-68e1-ff41-99ea1fb201b0700000876-8400-34e0-e6aa-335b933599ad8000009a2-a600-ffdf-ce12-cccc06b931a99复制代码
混淆复杂化
static Guid Int2Guid(int value){ var r = new Random(value); byte[] bytes = new byte[16]; r.NextBytes(bytes); BitConverter.GetBytes(value).CopyTo(bytes, 1 + (bytes[0] % 6)); var result = new Guid(bytes); return result;}
static int Guid2Int(Guid value){ byte[] bytes = value.ToByteArray(); int bint = BitConverter.ToInt32(bytes, 1 + (bytes[0] % 6)); return bint;}
for (int i = 1; i < 10; i++){ var guid = Int2Guid(i); var id = Guid2Int(guid); Console.WriteLine(guid); Console.WriteLine(id);}复制代码
控制台打印的结果如下,很难看到规律
8286d046-0140-0000-00cf-ff46699c73c4195c69371-b924-0002-0000-98b7dd200bc120003569d-0000-3ae3-63a0-322750a4a3bd3044519c8-0000-0600-4a08-cc98c4273aba4cf85ddf4-05ce-0000-0071-650938abd2b750006a01f-0000-9de1-18d9-ff7aab2e6ab36f604634b-0007-0000-ff41-99ea1fb201b0709442776-0878-0000-00aa-335b933599ad8000009a2-a600-ffdf-ce12-cccc06b931a99复制代码
划线
评论
复制
发布于: 刚刚阅读数: 3
版权声明: 本文为 InfoQ 作者【雄鹿 @】的原创文章。
原文链接:【http://xie.infoq.cn/article/74414e3243e9d21d18d25eaad】。文章转载请联系作者。
雄鹿 @
关注
心像开满花的树。 2019-01-04 加入
一名全栈开发工程师,热爱编程、徒步、登山和摄影,对新技术充满好奇心,专注于使用ASP.NET Core和Angular进行Web应用的开发。







评论