C# 根据 Guid 与 40Char 十六进制字符串 (UUID) 结合创建身份验证令牌
我正在编写一个驱动 iPhone 应用程序的 asp.net MVC 应用程序。
我希望 Iphone 向我发送其 UUID,如下所示:
2b6f0cc904d137be2e1730235f5664094b831186
在服务器上,我想生成一个 Guid:
466853EB-157D-4795-B4D4-32658D85A0E0
在 Iphone 和服务器上,我需要一个简单的算法将这两个值组合成可以传递的身份验证令牌。 iPhone 和 ASP.NET MVC 应用程序都需要能够根据 UUID 和 GUID 反复计算该值。
因此,这需要是一个简单的算法,没有来自 .net 框架的库。
完整解决方案在这里
public void Test()
{
var DeviceId = Guid.NewGuid();
var newId = "2b6f0cc904d137be2e1730235f5664094b831186";
var guidBytes = DeviceId.ToByteArray();
var iphoneBytes = StringToByteArray(newId);
byte[] xor = new byte[guidBytes.Length];
for (int i=0;i<guidBytes.Length;i++)
{
xor[i] = (byte) (guidBytes[i] ^ iphoneBytes[i]);
}
var result = ByteArrayToString(xor);
}
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars / 2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
public static string ByteArrayToString(byte[] ba)
{
StringBuilder hex = new StringBuilder(ba.Length * 2);
foreach (byte b in ba)
hex.AppendFormat("{0:x2}", b);
return hex.ToString();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
嗯,iPhone ID 看起来像一个十六进制字符串,因此将两者转换为二进制并对字节进行异或操作应该可以做到这一点。您可以根据需要将结果存储为数组、十六进制字符串或 base-64 编码字符串。
然而,您将其称为“身份验证令牌”的方式有点令人担忧。会话 ID 必须是不可预测的。您可以考虑在服务器上生成一组加密随机数据而不是 GUID。
编辑
遗憾的是,.NET 似乎没有内置方法来转换十六进制字符串或从十六进制字符串转换,但这个主题之前已经介绍过:将字节数组转换为十六进制字符串,反之亦然
Well, the iPhone ID looks like a hex string, so converting both to binary and XORing the bytes ought to do it. You could store the result as an array, hex string, or base-64 encoded string as appropriate.
The way you refer to this as an "auth token" is a little concerning, however. Session ids must be unpredictable. You might consider generating an array of cryptographically random data on the server instead of a GUID.
Edit
Sadly, it seems .NET doesn't have a built-in method to convert to/from hex strings, but this subject has been covered before: Convert byte array to hex string and vice versa
作为旁注,我使用过的所有“身份验证令牌”机制(至少)将一个常量值(“秘密”)与当前时间连接起来,然后将它们散列在一起,然后发送散列和日期。然后,服务器根据接收到的日期和已知的“秘密”重建哈希值,然后与接收到的哈希值(签名)进行比较。
我的观点是“与日期连接”——这使得生成的签名每次都不同,理论上应该更安全。
Just as a side note, all the "auth token" mechanisms I've worked with (at least) concatenated a constant value (a "secret") with the current time, then hashed them together then sent the hash and the date. The server then reconstructed the hash from the received date and known "secret" and then compared to the received hash (signature).
My point here was "concatenated with date" - this allows the resulting signature to be different every time which in theory should be more secure.
您可以直接连接这些十六进制数字,而不是进行会丢失信息的异或运算。
Rather than XORing, which loses information, you could just concatenate these hex digits.
为什么还需要 GUID?电话 ID 是唯一的,GUID 似乎没有增加任何价值。
Why do you even need the GUID? The phone ID is unique, the GUID seems to add no value.
我认为你可以使用两种算法。这意味着该算法可以像 Base64、SHA256、AES 一样进行编码和解码
I thought you can use two way algorithm. It mean the algorithm can be encode and decode like Base64, SHA256, AES