How to generate 8 byte GUID value in c#?
.net, asp.net, c#
Solution
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();
}
}
}
Problem
Possible Duplicate: How to generate 8 bytes unique id from GUID? 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# ?