-1

Here is my code. It gets a list of hashes, which are leaked. I want to check my password against it. What I want it to do, is to, when it finds it to throw me back the number of occurrences it has been leaked, if at all. How can this be accomplished? For example sake, let's say our necessary hash happens to be the 2nd one and thus we want to extract the number 3. What we have already is the hash infront of it. It is named "ending" as you can see in the code.

import hashlib
import requests


password = input("Enter password: ")

encoded_str = password.encode()
hash_obj = hashlib.sha1(encoded_str)
hashed = hash_obj.hexdigest().upper()

beginning = hashed[:5]
ending = hashed[5:].strip()
response = requests.get("https://api.pwnedpasswords.com/range/"+beginning)
output = response.text
listing = output.split()
print(listing)

output:

['0015711CF2308DD93DC449B888F9805B728:1', '0083F4473656452B43073DF2861FD289F63:3', '0DE17FB8EC56DD673FF3AF94BAB5029BFF2:1', '0DEC778F27B49DECF0E7C3B8AB2DD152990:15', '0E8EEF1620F095A7A26F679388A02EFEA4C:2', '0FD09EF75E6654D1E2FB5FC715A11331B6D:2', '11CFB41389B28F08B74A17851292D086922:1', '12A7DE6568963683AA7D21E3FBA1A1B5D39:1', '12B602E54A280622E21FC57607D70F9E3D6:4', '133B5AFB8798339FF1BF29DBBD068DFB556:2912', '13723F1F53E4468943870CA48E2093C0531:5', '139946DFB7AA0936F96DFB9B27931508AC3:1', '13AB10DBA939781F0416361A25024EF0D8C:4', '13E2A779A5F3F6C4BA21F23A5FB949DE347:2', '52CFB9745616A23A369EA5AD9D480DFE8E9:1', '52F07FB24866744C9E7D7460A04C143AAA3:2']

Our goal output: 3

Nebroska
  • 3
  • 5
  • Wait, so you just need to know how to get the part of the string after the colon? Have you tried `listing[1].split(':')[1]`? – wjandrea Apr 18 '21 at 19:01
  • the problem is, we don't know the position of the hash. It can be also the 200th or 25th. – Nebroska Apr 18 '21 at 19:03
  • Then you'll need to search for it first... I'm confused, what do you need help with exactly? – wjandrea Apr 18 '21 at 19:05
  • In that case I need help in searching for it. – Nebroska Apr 18 '21 at 19:06
  • ^ That's one possible solution. Another could be to convert `listing` to a dict by splitting each string into key (hash) and value (occurrences). Then you would just look up your hash. See [Convert list of strings to dictionary](https://stackoverflow.com/q/22980977/4518341) – wjandrea Apr 18 '21 at 19:09
  • this finds us now the string but now I would need to print out the number of occurrences connected to the hash – Nebroska Apr 18 '21 at 19:09
  • See [How to get a string after a specific substring?](https://stackoverflow.com/q/12572362/4518341) – wjandrea Apr 18 '21 at 19:10
  • Could you please write it out. Started with coding just a few days ago and am not too sure how anything works here. – Nebroska Apr 18 '21 at 19:10
  • OK, then please first take the [tour] and read [ask]. – wjandrea Apr 18 '21 at 19:20

1 Answers1

0

try to use this code:

num = 0
for line in listing:
    if ending in line:
        num = line.split(':')[1]
        break
else:
    print("the 'ending' is not in 'listing'")
Ido
  • 168
  • 9