Page 1 of 1
How to generate a number of unique/random strings?
Posted: Wed Jun 17, 2015 1:33 pm
by IdeasVacuum
Can we do this with PB? :
http://www.den4b.com/?x=tools&tool=password
I'm looking for a way to produce Keys and Initialisation Vectors for AESEncoder()
Re: How to generate a number of unique/random strings?
Posted: Wed Jun 17, 2015 2:34 pm
by Little John
Hi,
this is a basic version, not too slow.
Of course it can be adapted, so that there will be different probabilities for different characters and whatnot.
Code: Select all
EnableExplicit
Procedure.s RandomString (numChars.i, allowedCharacters$="ABCabc123")
; -- works in ASCII mode and Unicode mode
Protected *sourceBase, *source.Character, *dest.Character
Protected lastAllowed.i=Len(allowedCharacters$)-1
Protected ret$ = Space(numChars)
*sourceBase = @allowedCharacters$
*dest = @ret$
While *dest\c <> 0
*source = *sourceBase + Random(lastAllowed) * SizeOf(Character)
*dest\c = *source\c
*dest + SizeOf(Character)
Wend
ProcedureReturn ret$
EndProcedure
Debug RandomString(10)
Re: How to generate a number of unique/random strings?
Posted: Wed Jun 17, 2015 2:48 pm
by Tenaja
This does the easy part:
Code: Select all
For i = 1 To 10
s$ + Chr(Random(126,32))
Next
Debug s$
This shows one way to eliminate a stretch of characters.
Code: Select all
i = 0
Repeat
t$ = Chr(Random(126,32))
a = Asc(t$)
If a <90 Or a > 96
s$ + t$
i+1
EndIf
Until i = 10
Debug s$
Re: How to generate a number of unique/random strings?
Posted: Wed Jun 17, 2015 3:06 pm
by Zebuddi123
Hi IdeasVacuum Just show`s Little John`s version using pointer`s is twice as fast. 100,000 iterations average 450ms, mine 850ms. on my i3 lappy.
Zebuddi.
Code: Select all
Procedure.s GenData(maxlen)
Protected rn, string.s, number
RandomSeed(Random(100000, 1))
While number<maxlen
rn =Random(122, 48)
Select rn
Case 48 To 57, 65 To 90, 97 To 122
string+Chr(rn)
number+1
EndSelect
Wend
ProcedureReturn string
EndProcedure
For i=1 To 10
Debug GenData(10)
Next
Re: How to generate a number of unique/random strings?
Posted: Wed Jun 17, 2015 3:31 pm
by IdeasVacuum
whoosh! thanks guys

Re: How to generate a number of unique/random strings?
Posted: Thu Jun 18, 2015 12:57 am
by JackWebb
I've been using this in my password manager for years. It's not fast but if you're just generating passwords speed is not an issue. I took care to print only character that I could actually read with my old eyes.. I can't tell the difference between ! and I without a magnifying glass LOL!
Code: Select all
Procedure.s KeyGen(KeyLen)
Define OldNum = Random(3)
Define Use, x
Define LongKey$, PassKey$
For X = 1 To 1000
Repeat
Use = Random(3) ; numeric 0-3
Until Use <> OldNum
OldNum = Use
Select Use
Case 0
LongKey$ + Str(Random(9)) ; numbers 0 to 9
Case 1
LongKey$ + Chr(Random(25) + 65) ; uppercase A to Z
Case 2
LongKey$ + Chr(Random(25) + 97) ; lowercase a to z
Case 3
LongKey$ + Mid("#$%&+/<=>?@\~", Random(12), 1) ; special chars
EndSelect
Next
For X = 1 To KeyLen
PassKey$ + Mid(LongKey$, Random(999) + 1, 1)
Next
ProcedureReturn PassKey$
EndProcedure
For i = 1 To 10
Debug KeyGen(10)
Next i
Good Luck!
Jack

Re: How to generate a number of unique/random strings?
Posted: Thu Jun 18, 2015 10:52 am
by IdeasVacuum
Nice one Jack, thanks for sharing.
Re: How to generate a number of unique/random strings?
Posted: Thu Jun 18, 2015 7:46 pm
by freak
You should really use CryptRandom() for such purposes. The output of Random() can be predictable.
Re: How to generate a number of unique/random strings?
Posted: Thu Jun 18, 2015 9:20 pm
by IdeasVacuum
Good advice, thanks freak.
Re: How to generate a number of unique/random strings?
Posted: Wed Jul 29, 2015 3:00 am
by citystate
slight adjustment to Little John's code to ensure unique strings
Code: Select all
Procedure.s RandomString (numChars.i, allowedCharacters$="ABCabc123")
; -- works in ASCII mode and Unicode mode
Static NewMap unique.b()
Protected *sourceBase, *source.Character, *dest.Character
Protected lastAllowed.i=Len(allowedCharacters$)-1
Protected ret$ = Space(numChars)
*sourceBase = @allowedCharacters$
*dest = @ret$
While *dest\c <> 0
*source = *sourceBase + Random(lastAllowed) * SizeOf(Character)
*dest\c = *source\c
*dest + SizeOf(Character)
Wend
; -- check that generated string is unique
If FindMapElement(unique(),ret$)
; -- if not, then regenerate it
ret$=RandomString (numChars,allowedCharacters$)
Else
; -- otherwise, add the generated string to the existing 'list'
AddMapElement(unique(),ret$)
EndIf
ProcedureReturn ret$
EndProcedure
Re: How to generate a number of unique/random strings?
Posted: Wed Jul 29, 2015 1:15 pm
by IdeasVacuum
actually, CryptRandom() delivers uniqueness - good to have a long list of allowed chars too.
Re: How to generate a number of unique/random strings?
Posted: Thu Jul 30, 2015 2:11 am
by citystate
IdeasVacuum wrote:- good to have a long list of allowed chars too.
good to know
