关于 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-000000000000
bytes = 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-000000000000
bytes = 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-000000000000
bytes = 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-000000000000
bytes = 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-000000000000
bytes = 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-000000000000
1
00000200-0000-0000-0000-000000000000
2
00000300-0000-0000-0000-000000000000
3
00000400-0000-0000-0000-000000000000
4
00000500-0000-0000-0000-000000000000
5
00000600-0000-0000-0000-000000000000
6
00000700-0000-0000-0000-000000000000
7
00000800-0000-0000-0000-000000000000
8
00000900-0000-0000-0000-000000000000
9
复制代码
增加混淆
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-ff46699c73c4
1
00000271-b900-6fe3-7c38-98b7dd200bc1
2
0000039d-db00-3ae3-63a0-322750a4a3bd
3
000004c8-fd00-06e2-4a08-cc98c4273aba
4
000005f4-1e00-d1e2-3171-650938abd2b7
5
0000061f-4000-9de1-18d9-ff7aab2e6ab3
6
0000074b-6200-68e1-ff41-99ea1fb201b0
7
00000876-8400-34e0-e6aa-335b933599ad
8
000009a2-a600-ffdf-ce12-cccc06b931a9
9
复制代码
混淆复杂化
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-ff46699c73c4
1
95c69371-b924-0002-0000-98b7dd200bc1
2
0003569d-0000-3ae3-63a0-322750a4a3bd
3
044519c8-0000-0600-4a08-cc98c4273aba
4
cf85ddf4-05ce-0000-0071-650938abd2b7
5
0006a01f-0000-9de1-18d9-ff7aab2e6ab3
6
f604634b-0007-0000-ff41-99ea1fb201b0
7
09442776-0878-0000-00aa-335b933599ad
8
000009a2-a600-ffdf-ce12-cccc06b931a9
9
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 3
版权声明: 本文为 InfoQ 作者【雄鹿 @】的原创文章。
原文链接:【http://xie.infoq.cn/article/74414e3243e9d21d18d25eaad】。文章转载请联系作者。
雄鹿 @
关注
心像开满花的树。 2019-01-04 加入
一名全栈开发工程师,热爱编程、徒步、登山和摄影,对新技术充满好奇心,专注于使用ASP.NET Core和Angular进行Web应用的开发。
评论