0

I am implementing a Python code wherein I need to generate an SHA key. To do this I use an online SHA generator. I send the input (the data for which the Hash is needed), through selenium, which works successfully. However, after that I am unable to get the output that is generated(the text string). I use the find_element_by_xpath function to get this data but it only returns an empty string. I don't understand what i am doing wrong. Can somebody tell me how I can do this? Or if there is any other method, besides using Selenium to achieve this?

I have used the following code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC

def driver_init():

    driver = webdriver.Chrome(executable_path='chromedriver.exe')
    driver.wait=WebDriverWait(driver,5)
    return driver

def get_data(driver, val):

    driver.get('https://passwordsgenerator.net/sha1-hash-generator/')
    xpath='//*[@id="txt1"]'
    box=driver.find_element_by_xpath(xpath)
    box.send_keys(val)
    element=driver.wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="txt2"]')))
    return element.text

driver=driver_init()
w=get_data(driver, '100011')

`

illu109
  • 1
  • 3
  • Have a look at https://docs.python.org/3/library/hashlib.html – JGK Jul 06 '20 at 20:02
  • I did take a look at this. But it only takes small strings as input, and I need the hash for an entire image(510*510). Is there a way I can use it to get the hash for an image? – illu109 Jul 07 '20 at 06:57
  • https://stackoverflow.com/questions/22058048/hashing-a-file-in-python – JGK Jul 07 '20 at 07:10

1 Answers1

0

The reason you're not seeing that text populate to your variable is because it's value is being set by scripts running on the page. You can get the value by using element.get_attribute('value') to get the element's current value.

A word of caution though: these types of hashes are generally used for security purposes, and unless you strongly trust this website, I would suggest using a local solution like an SSL (openSSL, cryptography, etc.) library to resolve these hashes instead of sending requests over the web. If something were to happen to this site's server or if it was taken over by a malicious actor, they could modify the site to send them all your (previously) secure data.

Stephen
  • 1,498
  • 17
  • 26
  • This worked, thank you so much! And also, thanks for the suggestion, actually I'm working on a project where I have to generate the SHA hash for an image, so I am not really passing sensitive information to the site. I did try implementing my own SHA1 generation code, however, it was unable to take very large strings as input. So I thought this would be the most convenient way to do it. – illu109 Jul 07 '20 at 06:54