Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
how to limit the length of GUID in c# to send password
Posted
Comments
DaveAuld 28-May-12 8:50am    
You are not making a lot of sense with that question. A GUID is a specific type, http://msdn.microsoft.com/en-us/library/system.guid.newguid.aspx, so you cannot limit its length. You would need to come up with your own identifier scheme. Just use a simple int.
Keith Barrow 28-May-12 8:56am    
Guids are Fixed length. They are this long to make it massively unlikely that two will be the same, reducing the length reduces their effectiveness.
Can you explain what it is you are trying to acheive (rather than what it is you are doing), sending a guid to the user is probably wrong especially if (I assume) you want to make it shorter and easier for them to read.
Sinisa Hajnal 4-Feb-16 4:02am    
Guids are internally just 16 bytes of random data- you could convert them to base64 strings and cut last two characters (which are == anyhow)

If you are looking to generate random passwords why not use the following method which is part of the .Net Framework (from V2 onwards)

System.Web.Security.Membership.GeneratePassword

See here;
http://msdn.microsoft.com/en-us/library/ms152017[^]
 
Share this answer
 
If you are using the Guid to generate a random password, then use the Substring() function to get a part of it.

C#
string strPassword = Guid.NewGuid().ToString("N").Substring(0, 12);       


However , keep in mind that a Guid is unique and the part of it may not be unique.

http://msdn.microsoft.com/en-us/library/s6tk2z69[^]
 
Share this answer
 
Comments
DaveAuld 28-May-12 10:09am    
They are not guaranteed to be unique, even the link you provided links through to the GUID page which states, 'A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated.'
As you can see, it states, very low probability!
Keith Barrow 28-May-12 10:33am    
I wish I could 5 this comment!
bbirajdar 29-May-12 2:38am    
I think we should talk in terms of numbers. Your comment is 1/2^128 % correct .

Guids are designed be unique and not random. Using a decent computer which generates a Guid in every 100 nanoseconds, 2^128 guids can be generated and it takes about 1.6 billion years before it runs out of Guids. So the probability of generating the same Guid twice is 1/2^128. And yes, it guaranteed that the same Guid is not generated on the same computer in 1.6 billion years (unless you turn back your system clock ).

The most important thing is that , guid should not be used to generate passwords, since they can be predicted.
bbirajdar 29-May-12 4:14am    
http://stackoverflow.com/questions/1705008/simple-proof-that-guid-is-not-unique
string strPassword = Guid.NewGuid().ToString().Substring(0, Guid.NewGuid().ToString().IndexOf("-"));

This will generate an unique alpha numeric password.

For example if the generated GUID is "0f8fad5b-d9cb-469f-a165-70867728950e" then it will retrieve only "0f8fad5b". It will be useful for password generation.

Try this..
 
Share this answer
 
v2
Comments
Member 14735464 21-Feb-22 5:20am    
not unique

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900