如何生成在C#中8字节的GUID值? [英] How to generate 8 byte GUID value in c#?
本文介绍了如何生成在C#中8字节的GUID值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
如何从GUID产生8个字节的唯一ID?
我需要一个独特的密钥识别用户的普遍和关键的长度刚好8个字节,我怎样才能做到这在C#?
I need a unique key to identify a user at universal and key's length is just 8 byte, How can I do this in c# ?
推荐答案
以下code会生成加密唯一8字符串:
the following code will generate cryptographically unique 8 character strings:
using System;
using System.Security.Cryptography;
using System.Text;
namespace JustForFun
{
public class UniqueId
{
public static string GetUniqueKey()
{
int maxSize = 8;
char[] chars = new char[62];
string a;
a = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
chars = a.ToCharArray();
int size = maxSize;
byte[] data = new byte[1];
RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
crypto.GetNonZeroBytes(data);
size = maxSize;
data = new byte[size];
crypto.GetNonZeroBytes(data);
StringBuilder result = new StringBuilder(size);
foreach (byte b in data)
{ result.Append(chars[b % (chars.Length - 1)]); }
return result.ToString();
}
}
}
这篇关于如何生成在C#中8字节的GUID值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文