Article View: borland.public.jbuilder.java.language
Article #8119Re: How to check if random password is unique ??
From: Torsten Welches
Date: Thu, 19 Apr 2001 11:33
Date: Thu, 19 Apr 2001 11:33
53 lines
2424 bytes
2424 bytes
Russell,
probably the easiest solution is to put all passwords into a
static set and check each new password.
Declare the generatePassword method synchonized to handle
multiple threads:
private static HashSet passwords = new HashSet();
public static synchonized String generatePassword() {
StringBuffer strbuff = new StringBuffer();
String result = null;
do {
for(int i = 0 ; i < 6 ; i++) {
strbuff.append(alphabets.charAt((int)(Math.random()*
(alphabets.length()- 1))));
}
result = strbuff.toString();
} while (!passwords.add(result)); //will fail for duplicates
return result;
}
Note that this is a transient solution, your password set will
be lost when your app finishes. You could for instance
serialize the set to get a persistent solution. Or you use a
database table for that.
Torsten
Russell <weetat@cesmainternational.com> wrote:
>
> Hi all ,
> I am using RedHat6.1 and jdk1.3.
>
> I have code below :
>
> public void generatePassword(){
>
> StringBuffer strbuff = new StringBuffer();
>
> for(int i = 0 ; i < 6 ; i++){
>
>strbuff.append(alphabets.charAt((int)(Math.random()*(alphabets.length()
>- 1))));
> }
> }
>
> As you all know ,the code below random generate password . However how
>i am going to check if the generate password is
> unique if the code is run by two threads ??
>
> Anybody have any ideas ?? Thanks wt.
Technical Details
Message-ID:
<3adeccf7$1_1@dnews>Path:
arc1-ca.pugleaf.net!supernews.google.com!newsfeed.google.com!sn-xit-02!supernews.com!news.tele.dk!193.174.75.178!news-fra1.dfn.de!news-koe1.dfn.de!do.de.uu.net!newsfeed01.sul.t-online.de!t-online.de!dnewsReferences:
<3ADEC40F.88A0B489@cesmainternational.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,ce64f4cb2825b970 X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-04-19 04:47:18 PST From: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Sender: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Reply-To: "Torsten Welches" <torsten.welches@who.cares.netcologne.de> Subject: Re: How to check if random password is unique ?? Newsgroups: borland.public.jbuilder.java.language X-User-Info: 193.158.55.74 193.158.55.74 References: <3ADEC40F.88A0B489@cesmainternational.com> NNTP-Posting-Host: 207.105.83.65 Message-ID: <3adeccf7$1_1@dnews> Date: 19 Apr 2001 04:33:11 -0700 X-Trace: dnews 987679991 207.105.83.65 (19 Apr 2001 04:33:11 -0700) Lines: 54 Path: arc1-ca.pugleaf.net!supernews.google.com!newsfeed.google.com!sn-xit-02!supernews.com!news.tele.dk!193.174.75.178!news-fra1.dfn.de!news-koe1.dfn.de!do.de.uu.net!newsfeed01.sul.t-online.de!t-online.de!dnews