[text] RandomStringGenerator as replacement for RandomStringUtils

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[text] RandomStringGenerator as replacement for RandomStringUtils

Wilson MacGyver
Hi,

After upgrading to commons-lang 3.6 I noticed RandomStringUtils has been
deprecated in favor of RandomStringGenerator in commons-text

currently I'm using it by passing a character array that has a mix of
characters and symbols. I'm also using SecureRandom as the random source

I'm not at all clear on how to do this in RandomStringGenerator.

I notice in the builder you can pass a range, but this implies characters
you want to use must be continuous within unicode. The only thing I can
think of is to then
use filteredBy to filter out characters that I don't want?


At first glance, it also looks like usingRandom() only works with the
apache common RNG?

Thanks


--
Omnem crede diem tibi diluxisse supremum.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [text] RandomStringGenerator as replacement for RandomStringUtils

Gilles Sadowski
Hi.

On Tue, 20 Jun 2017 00:12:58 -0400, Wilson MacGyver wrote:

> Hi,
>
> After upgrading to commons-lang 3.6 I noticed RandomStringUtils has
> been
> deprecated in favor of RandomStringGenerator in commons-text
>
> currently I'm using it by passing a character array that has a mix of
> characters and symbols. I'm also using SecureRandom as the random
> source
>
> I'm not at all clear on how to do this in RandomStringGenerator.
>
> I notice in the builder you can pass a range, but this implies
> characters
> you want to use must be continuous within unicode. The only thing I
> can
> think of is to then
> use filteredBy to filter out characters that I don't want?

That would not be very efficient.
Perhaps we should implement a "selectFrom" feature:
   RandomStringGenerator gen = new RandomStringGenerator.Builder()
     .selectFrom(charArray)
     .build();

>
> At first glance, it also looks like usingRandom() only works with the
> apache common RNG?

No. You can pass any class that implement the "TextRandomProvider"
interface (there is an example in the "RandomStringGeneratorTest"
unit test class).

If you use Java 8, you can also do:
   final SecureRandom rand = new SecureRandom();
   RandomStringGenerator gen = new RandomStringGenerator.Builder()
     .usingRandom(rand::nextInt)
     .build();


HTH,
Gilles

>
> Thanks


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [text] RandomStringGenerator as replacement for RandomStringUtils

Wilson MacGyver
On Tue, Jun 20, 2017 at 8:22 AM, Gilles <[hidden email]>
wrote:

> Hi.
>
>
> That would not be very efficient.
> Perhaps we should implement a "selectFrom" feature:
>   RandomStringGenerator gen = new RandomStringGenerator.Builder()
>     .selectFrom(charArray)
>     .build();



that would be great. I would imagine using this as password generator is a
pretty common use case. Limiting passwords to allowed character list is a
fairly standard practice.

>
>
>
>> At first glance, it also looks like usingRandom() only works with the
>> apache common RNG?
>>
>
> No. You can pass any class that implement the "TextRandomProvider"
> interface (there is an example in the "RandomStringGeneratorTest"
> unit test class).
>
> If you use Java 8, you can also do:
>   final SecureRandom rand = new SecureRandom();
>   RandomStringGenerator gen = new RandomStringGenerator.Builder()
>     .usingRandom(rand::nextInt)
>     .build();
>
>
>
ah, I see. need to pass nextInt. got it. Thank you very much for that.



>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


--
Omnem crede diem tibi diluxisse supremum.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [text] RandomStringGenerator as replacement for RandomStringUtils

Gilles Sadowski
On Tue, 20 Jun 2017 09:06:57 -0400, Wilson MacGyver wrote:

> On Tue, Jun 20, 2017 at 8:22 AM, Gilles
> <[hidden email]>
> wrote:
>
>> Hi.
>>
>>
>> That would not be very efficient.
>> Perhaps we should implement a "selectFrom" feature:
>>   RandomStringGenerator gen = new RandomStringGenerator.Builder()
>>     .selectFrom(charArray)
>>     .build();
>
>
>
> that would be great. I would imagine using this as password generator
> is a
> pretty common use case. Limiting passwords to allowed character list
> is a
> fairly standard practice.

Could you then please file a "wish" request on the issue-tracking
system:
   https://issues.apache.org/jira/browse/TEXT
?

Thanks,
Gilles

> [...]


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: [text] RandomStringGenerator as replacement for RandomStringUtils

Wilson MacGyver
done.

https://issues.apache.org/jira/browse/TEXT-93

Thanks!

On Tue, Jun 20, 2017 at 9:13 AM, Gilles <[hidden email]>
wrote:

> On Tue, 20 Jun 2017 09:06:57 -0400, Wilson MacGyver wrote:
>
>> On Tue, Jun 20, 2017 at 8:22 AM, Gilles <[hidden email]>
>> wrote:
>>
>> Hi.
>>>
>>>
>>> That would not be very efficient.
>>> Perhaps we should implement a "selectFrom" feature:
>>>   RandomStringGenerator gen = new RandomStringGenerator.Builder()
>>>     .selectFrom(charArray)
>>>     .build();
>>>
>>
>>
>>
>> that would be great. I would imagine using this as password generator is a
>> pretty common use case. Limiting passwords to allowed character list is a
>> fairly standard practice.
>>
>
> Could you then please file a "wish" request on the issue-tracking
> system:
>   https://issues.apache.org/jira/browse/TEXT
> ?
>
> Thanks,
> Gilles
>
> [...]
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>


--
Omnem crede diem tibi diluxisse supremum.
Loading...