Re: primitive password cracker

2021-01-10 Thread Bischoop
On 2021-01-08, Stefan Ram  wrote:
> Bischoop  writes:
>>What I want to learn is if I need get for example four combinations, so
>>how to get in a loop first letter 'a',then another step'a' and again 'a'
>>and 'a', to have '' later on'abaa' etc.
>
>   I can only guess what you want, maybe something like
>
> word = []
> p = 0
>
> def inc_at( position ):
> word[ position ]= chr( ord( word[ position ])+ 1 )
>
> while True:
> if len( word )< p + 1:
> word =[ "a" ]+ word
> print( "".join( word ))
> p = 0
> o = len( word )- 1 - p
> inc_at( o )
> while p < len( word ) and word[ o ]== '{':
> word[ o ]= "a"
> p += 1
> o -= 1
> if p < len( word ): inc_at( o )
>
>   ?
>
>


Yes, it must generate 4 characters long all combination of alphabet,
equivalent to: s = list(''.join(seq) for seq 
initertools.product(string.ascii_lowercase, repeat=4)).
I must say that's quite coding you've done.

--
Thanks


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: primitive password cracker

2021-01-09 Thread Michael F. Stemper

Is this "code golf"? Can anybody play? Here's another approach:

Define a function like this:

def embiggen(wordlist):
  alpha = 'abc'
  return [x for x in alpha] + [x+y for x in alpha for y in wordlist]

It makes every word in its input one letter longer in each possible
way, and returns all of the words thus created as well as all of the
one-letter words. (I'm using a very short alphabet, due to the
exponential growth.)

In REPL:
>>> words = []
>>> words = embiggen(words)
>>> words
['a', 'b', 'c']
>>> words = embiggen(words)
>>> words
['a', 'b', 'c', 'aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
>>> words = embiggen(words)
>>> words
['a', 'b', 'c', 'aa', 'ab', 'ac', 'aaa', 'aab', 'aac', 'aba', 'abb', 
'abc', 'aca', 'acb', 'acc', 'ba', 'bb', 'bc', 'baa', 'bab', 'bac', 
'bba', 'bbb', 'bbc', 'bca', 'bcb', 'bcc', 'ca', 'cb', 'cc', 'caa', 
'cab', 'cac', 'cba', 'cbb', 'cbc', 'cca', 'ccb', 'ccc']

>>> words = embiggen(words)
>>> len(words)
120
>>>

It's obvious that this could be called from a for-loop with no
modification, which I believe is what the OP wanted.

--
Michael F. Stemper
Deuteronomy 24:17
--
https://mail.python.org/mailman/listinfo/python-list


Re: primitive password cracker

2021-01-07 Thread Chris Angelico
On Fri, Jan 8, 2021 at 11:31 AM Greg Ewing  wrote:
>
> Another way to approach this problem is using recursion, e.g.
>
> def find_combinations(items, n, comb, result):
>if n == 0:
>  result.append(comb)
>else:
>  for item in items:
>find_combinations(items, n - 1, comb + [item], result)
>
> words = []
> find_combinations(['a', 'b', 'c'], 3, [], words)
> print(words)
>

True, but I'd much rather write it as a generator:

def permute(items, n):
if not n:
yield ""
return
# Optional performance enhancement:
if n == 1: return (yield from items)
for item in items:
for rest in permute(items, n - 1):
yield item + rest

words = list(permute("abc", 3))

I'm not sure how performance would go, but this is a lot cleaner.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: primitive password cracker

2021-01-07 Thread Greg Ewing

Another way to approach this problem is using recursion, e.g.

def find_combinations(items, n, comb, result):
  if n == 0:
result.append(comb)
  else:
for item in items:
  find_combinations(items, n - 1, comb + [item], result)

words = []
find_combinations(['a', 'b', 'c'], 3, [], words)
print(words)

For this particular problem it's less efficient than the
technique used by itertools.product, because it generates
sub-combinations multiple times. However, it's a useful
technique to keep in mind whenever you have a "variable
number of nested for-loops" kind of problem.

--
Greg

--
https://mail.python.org/mailman/listinfo/python-list


Re: primitive password cracker

2021-01-07 Thread dn via Python-list
On 08/01/2021 05.52, Bischoop wrote:
> On 2021-01-07, Chris Angelico  wrote:
>>
>> True. Unfortunately, it doesn't work, so what you'd have is something
>> that can be easily parameterized to not work on other numbers of
>> characters too. :)
>>
> 
> My bad is I'm kinda maniac and have to know how to, I know best solution
> itertool but... I just must know, was thinking that studing the itertool
> code would give me an answer but found it's in C.
> Now I try list comprehension for it, so it's another day on it for me.


BTW have you realised a second 'typo' - that there are only 24 letters
in this 'alphabet'?


It is not difficult to parametrise the building of a "password cracking
dictionary"! (NB not same definition as a Python dictionary) Take it one
step (one character) at a time...

We start from our list of "letters". This is every permutation of the
alphabet, taken one at a time. Call it the one_character_list.
[Let's not distract from the problem by arguing about the definition of
"permutation"!]

Next, we'll take the one_character_list and permute that, with each of
our letter[s]. Thus 'a, b, c' expands to 'aa, ab, ac, ... ba, bb, bc,
... ca, cb, cc, ...'.

For every one of the 24 elements of the one_character_list we now have
24 elements in the two_character_list, ie len( two_character_list )
should be 24*24 (== 24**2) and each element should be unique (ie same
rule as a set).
NB above equation varies if the rule for what may be the first character
of a password differs for the rule of which letters/characters may follow!

Similarly, if we take the two_character_list, we can permute that
(again/further) with each letter. Thus 'aaa, aab, aac, ...baa, bab, bac,
...'. Thus, len( three_character_list ) == 24 * 24 * 24 == 24 ** 3 ==
len( two_character_list ) * 24 == len( one_character_list ) * 24 * 24

Carrying-on we can build a "password cracking dictionary" of any length.
NB the three-character_list contains exactly 'what it says on the tin'
(English-English expression meaning that the label is entirely correct).
Thus if the password rules say 'up to three characters in length', then
the password-cracker would have to test each of the three lists in-turn
too look for one-character passwords, then two-character passwords, and
finally three...!


Disclaimers:

Permutations and combinations are an interesting [learning] exercise,
and we're getting used to your preference for step-by-step
experimentation and learning-mastery.

That said, @David is correct - once understood, use the tool! Because
these multiple loops are "expensive" (in terms of execution-time).

Per @Chris and comments above, consider when and where to use
character-strings, tuples, lists, and/or sets - for 'best results'
and/or 'best practice'.

Per @yourself, once you have coded, tested, and debugged a solution
featuring explicit for-loops, it will be a valuable exercise to upgrade
(the code and quite probably the execution speed) using
comprehensions/expressions...



Sample code:

def permute_lists( existing_list:list, expansion_list:list )->list:
'''Expand each of the existing list's elements
with every element of a provided list.
'''
permuted_list = list()
for existing_element in existing_list:
for expansion_element in expansion_list:
permuted_list.append( existing_element + expansion_element )
return permuted_list


# establish character-set to be used
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'r',
   's', 't', 'u', 'w', 'q', 'y', 'z']

# taking each of the letters once is the character-set
one_character_list = letters
print( len( one_character_list ) )
print( one_character_list, end="\n" )

# taking the letters two at a time
two_character_list = permute_lists( one_character_list, letters )
print( len( two_character_list ), len( one_character_list ) * len(
letters ) )
print( two_character_list, end="\n" )

# taking the letters three at a time
three_character_list = permute_lists( two_character_list, letters )
print( len( three_character_list ), len( two_character_list ) * len(
letters ) )
print( three_character_list, end="\n" )

-- 
Regards =dn
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: primitive password cracker

2021-01-07 Thread David Kolovratník
I find a parallel to counting useful. Let letters be '0' to '9'
and think of manual (in contrast to formatting range(0, 10))
construction of list of strings "" to "" 

Let's start with . How do you compute the next item? Start
looking from its end. I stop here in order to leave space for 
Bischoop's deliberation.

To sum up in order words, the clue is to stop thinking of inner
loops for each letter and find a way to get the next combination.

Best,
David

On Thu, Jan 07, 2021 at 04:52:56PM -, Bischoop wrote:
> On 2021-01-07, Chris Angelico  wrote:
> >
> > True. Unfortunately, it doesn't work, so what you'd have is something
> > that can be easily parameterized to not work on other numbers of
> > characters too. :)
> >
> 
> My bad is I'm kinda maniac and have to know how to, I know best solution
> itertool but... I just must know, was thinking that studing the itertool
> code would give me an answer but found it's in C.
> Now I try list comprehension for it, so it's another day on it for me.
> 
> 
> --
> Thanks
> -- 
> https://mail.python.org/mailman/listinfo/python-list
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: primitive password cracker

2021-01-07 Thread Bischoop
On 2021-01-07, Bischoop  wrote:
> On 2021-01-07, Chris Angelico  wrote:
>>
>> True. Unfortunately, it doesn't work, so what you'd have is something
>> that can be easily parameterized to not work on other numbers of
>> characters too. :)
>>
>
> My bad is I'm kinda maniac and have to know how to, I know best solution
> itertool but... I just must know, was thinking that studing the itertool
> code would give me an answer but found it's in C.
> Now I try list comprehension for it, so it's another day on it for me.
>
>

So after checking itertools doc I found what itertools.product is
quivalent for on: 
https://docs.python.org/3/library/itertools.html#itertools.product

Now I'll sleep.

--
Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: primitive password cracker

2021-01-07 Thread Bischoop
On 2021-01-07, David Raymond  wrote:
> I think you might want to check out itertools.product()
> https://docs.python.org/3.9/library/itertools.html#itertools.product


Thanks David for contribution I find it very convenient but not knowing
how to solve solution without itertools for:
for a i :
for b in :
for c in :
for d in :


doesn't give me a peace.

--
Thanks



-- 
https://mail.python.org/mailman/listinfo/python-list


Re: primitive password cracker

2021-01-07 Thread Bischoop
On 2021-01-07, Chris Angelico  wrote:
>
> True. Unfortunately, it doesn't work, so what you'd have is something
> that can be easily parameterized to not work on other numbers of
> characters too. :)
>

My bad is I'm kinda maniac and have to know how to, I know best solution
itertool but... I just must know, was thinking that studing the itertool
code would give me an answer but found it's in C.
Now I try list comprehension for it, so it's another day on it for me.


--
Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: primitive password cracker

2021-01-07 Thread Chris Angelico
On Fri, Jan 8, 2021 at 2:12 AM Bischoop  wrote:
>
> On 2021-01-07, Chris Angelico  wrote:
>
> This could allow easy to change the number of  characters in
> combination, just pass n argument to range where n is how many characters.
>

True. Unfortunately, it doesn't work, so what you'd have is something
that can be easily parameterized to not work on other numbers of
characters too. :)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: primitive password cracker

2021-01-07 Thread David Raymond
I think you might want to check out itertools.product()
https://docs.python.org/3.9/library/itertools.html#itertools.product

import itertools
import string
passe = 'pass'
for p in itertools.product(string.ascii_lowercase, repeat = 4):
p = "".join(p)
if p == passe:
print("Found it:", p)
break
else:
print("Didn't find it.")

Or for different lengths:

foundIt = False
for repeat in range(1, 6):
for p in itertools.product(string.ascii_lowercase, repeat = repeat):
p = "".join(p)
if p == passe:
print("Found it:", p)
foundIt = True
break
if foundIt:
break
else:
print("Didn't find it.")
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: primitive password cracker

2021-01-07 Thread Bischoop
On 2021-01-07, Chris Angelico  wrote:

This could allow easy to change the number of  characters in
combination, just pass n argument to range where n is how many characters.


-- 
https://mail.python.org/mailman/listinfo/python-list


Re: primitive password cracker

2021-01-07 Thread Bischoop
On 2021-01-07, Chris Angelico  wrote:
>
> I'd recommend having just a string, rather than a list; it'll behave
> identically for what you're doing, and it'll be a lot easier to see
> when you have all the right letters.
>

Yes that's definitely better, I've done so.
>> mineset= set()
>> for a in letters:
>> for b in letters:
>> for c in letters:
>> for d in letters:
>> s = a + b + c + b
>
> Why is b at the end? :) That's why "zulu" could be found, but "pass" couldn't.
>

ha, that's typo I was working so long on it that got mixed up.
and yes with 'd' it works :-)

>> k = sorted(mineset)
>> print(k)
>> for i in k:
>> if i == passe:
>> print('got it: ', i )
>> print(passe in k)
>> --
>> It works in someway but the problems are:
>> Not all combination are made, I change password to: 'pass' and that 
>> combination is not in results.
>> Another thing is, I had to made a set from list because combinations
>> were repeated.
>
> Yep. I'd recommend looking at the original list rather than setting
> and sorting, and you might notice patterns that hint at problems.
>
The problem was with that 'b' at the end, now it solved.


>> for x in range(4):  
>> for y in letters:
>> combin +=y
>> lista.append(combin)
>> combin=''
>
> Not entirely sure what your intended logic is here, but I'd advise
> planning things out in pseudocode and knowing what you're actually
> building.
>
Well :-) I tried to do shorter version of the previous code to do not do
for loop for every character, range
supposed to be for how long the password is, in this case four
characters, once the 4character combination is made add it to the list,
similarly to the previous code but my logic is wrong here.
I don't have clue hot to tell: get a letter for 1 get for 2 get for 3
and get for 4.




> Debugging code can be very hard, especially if you don't know what
> it's doing. Fortunately, Python comes with a number of tools to help
> you figure out your code; the simplest is the print function - just
> add a few useful print calls to your code and trace through things
> that way. You can also look at the pdb module, and various other
> techniques. Explore your code, get to know how it works at each point,
> and you should be able to figure things out.
>
> Good luck, have fun! And remember IIDPIO: If In Doubt, Print It Out!
>

For me is even not debbuging code is important but to do
what I want and in that case is that what I mention earlier four
letters picked and add them to the list, and today again I seat on it
and just nothing lol.

--
Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: primitive password cracker

2021-01-07 Thread Chris Angelico
On Thu, Jan 7, 2021 at 8:46 PM Bischoop  wrote:
> What I want to learn is if I need get for example four combinations, so
> how to get in a loop first letter 'a',then another step'a' and again 'a'
> and 'a', to have '' later on'abaa' etc.

So you want every possible four-letter combination?

> So I wrote that:
> --
> import string
> passe = 'zulu'
> mylist = []
> #letters = string.ascii_lowercase
> letters = ['a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 
> 'n', 'o', 'p', 'r', 's', 't', 'u', 'w', 'q', 'y', 'z']

I'd recommend having just a string, rather than a list; it'll behave
identically for what you're doing, and it'll be a lot easier to see
when you have all the right letters.

I'd also recommend having the letters in some well-known order, such
as lexicographic (aka "alphabetical order"), or running straight
across the rows of your keyboard. Keyboards aren't all the same, but
at least you'll know that your own keyboard is consistent.

> mineset= set()
> for a in letters:
> for b in letters:
> for c in letters:
> for d in letters:
> s = a + b + c + b

Why is b at the end? :) That's why "zulu" could be found, but "pass" couldn't.

> k = sorted(mineset)
> print(k)
> for i in k:
> if i == passe:
> print('got it: ', i )
> print(passe in k)
> --
> It works in someway but the problems are:
> Not all combination are made, I change password to: 'pass' and that 
> combination is not in results.
> Another thing is, I had to made a set from list because combinations
> were repeated.

Yep. I'd recommend looking at the original list rather than setting
and sorting, and you might notice patterns that hint at problems.

> for x in range(4):
> for y in letters:
> combin +=y
> lista.append(combin)
> combin=''

Not entirely sure what your intended logic is here, but I'd advise
planning things out in pseudocode and knowing what you're actually
building.

Debugging code can be very hard, especially if you don't know what
it's doing. Fortunately, Python comes with a number of tools to help
you figure out your code; the simplest is the print function - just
add a few useful print calls to your code and trace through things
that way. You can also look at the pdb module, and various other
techniques. Explore your code, get to know how it works at each point,
and you should be able to figure things out.

Good luck, have fun! And remember IIDPIO: If In Doubt, Print It Out!

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


primitive password cracker

2021-01-07 Thread Bischoop


I was practising for educational purpose with simply password cracker.
I know that I could use itertool but in my case when I'm learning I
would miss facing problems and learning on them and indeed I've met one
which is not giving me a peace.

What I want to learn is if I need get for example four combinations, so
how to get in a loop first letter 'a',then another step'a' and again 'a'
and 'a', to have '' later on'abaa' etc.

So I wrote that:
--
import string
passe = 'zulu'
mylist = []
#letters = string.ascii_lowercase
letters = ['a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
'o', 'p', 'r', 's', 't', 'u', 'w', 'q', 'y', 'z']
mineset= set()
for a in letters:
for b in letters:
for c in letters:
for d in letters:
s = a + b + c + b
mylist.append(s)
mineset=set(mylist)

k = sorted(mineset)
print(k)
for i in k:
if i == passe:
print('got it: ', i )
print(passe in k)
--
It works in someway but the problems are:
Not all combination are made, I change password to: 'pass' and that combination 
is not in results.
Another thing is, I had to made a set from list because combinations
were repeated.
And finally, I was struglling with making it in without creating four
loops for four letters, something like that:

To try to solve those I went with that approach:

-
letters = ['a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 
'o', 'p', 'r', 's', 't', 'u', 'w', 'q', 'y', 'z']
password = 'pass'
combin = ''
lista=[]

for x in range(4):
for y in letters:
combin +=y
lista.append(combin)
combin=''
mineset=set(lista)
print(mineset)
for i in lista:
if i == password:
print('pass:', i)
---
But the results are disspainting:
{'abc', 'a', 'ab', 'abcd', 'abcde'}

I was seating on it for a long day but can't even closely achieve
similar effect to 4 loops in previous code.

--
Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list