Skip to content

Commit

Permalink
Switch 'pycryptodome' -> 'cryptography'
Browse files Browse the repository at this point in the history
  • Loading branch information
ValueRaider committed Dec 19, 2022
1 parent 097c76a commit f398f46
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ To install `yfinance` using `conda`, see
- [frozendict](https://pypi.org/project/frozendict) \>= 2.3.4
- [beautifulsoup4](https://pypi.org/project/beautifulsoup4) \>= 4.11.1
- [html5lib](https://pypi.org/project/html5lib) \>= 1.1
- [pycryptodome](pycryptodome>=3.6.6) \>= 3.6.6
- [cryptography](https://pypi.org/project/cryptography) \>= 3.3.2

### Optional (if you want to use `pandas_datareader`)

Expand Down
6 changes: 4 additions & 2 deletions meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ requirements:
- frozendict >=2.3.4
- beautifulsoup4 >=4.11.1
- html5lib >=1.1
- pycryptodome >=3.6.6
# - pycryptodome >=3.6.6
- cryptography >=3.3.2
- pip
- python

Expand All @@ -41,7 +42,8 @@ requirements:
- frozendict >=2.3.4
- beautifulsoup4 >=4.11.1
- html5lib >=1.1
- pycryptodome >=3.6.6
# - pycryptodome >=3.6.6
- cryptography >=3.3.2
- python

test:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ pytz>=2022.5
frozendict>=2.3.4
beautifulsoup4>=4.11.1
html5lib>=1.1
pycryptodome>=3.6.6
cryptography>=3.3.2
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@
install_requires=['pandas>=1.3.0', 'numpy>=1.16.5',
'requests>=2.26', 'multitasking>=0.0.7',
'lxml>=4.9.1', 'appdirs>=1.4.4', 'pytz>=2022.5',
'frozendict>=2.3.4', 'pycryptodome>=3.6.6',
'frozendict>=2.3.4',
# 'pycryptodome>=3.6.6',
'cryptography>=3.3.2',
'beautifulsoup4>=4.11.1', 'html5lib>=1.1'],
entry_points={
'console_scripts': [
Expand Down
36 changes: 22 additions & 14 deletions yfinance/data.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import functools
from functools import lru_cache

import hashlib
from base64 import b64decode
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
usePycryptodome = False # slightly faster
# usePycryptodome = True
if usePycryptodome:
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
else:
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes

import requests as requests
import re
Expand Down Expand Up @@ -52,14 +59,7 @@ def decrypt_cryptojs_aes(data):
salt = encrypted_stores[8:16]
encrypted_stores = encrypted_stores[16:]

def EVPKDF(
password,
salt,
keySize=32,
ivSize=16,
iterations=1,
hashAlgorithm="md5",
) -> tuple:
def EVPKDF(password, salt, keySize=32, ivSize=16, iterations=1, hashAlgorithm="md5") -> tuple:
"""OpenSSL EVP Key Derivation Function
Args:
password (Union[str, bytes, bytearray]): Password to generate key from.
Expand Down Expand Up @@ -100,14 +100,22 @@ def EVPKDF(

key, iv = EVPKDF(password, salt, keySize=32, ivSize=16, iterations=1, hashAlgorithm="md5")

cipher = AES.new(key, AES.MODE_CBC, iv=iv)
plaintext = cipher.decrypt(encrypted_stores)
plaintext = unpad(plaintext, 16, style="pkcs7")
if usePycryptodome:
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
plaintext = cipher.decrypt(encrypted_stores)
plaintext = unpad(plaintext, 16, style="pkcs7")
else:
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
decryptor = cipher.decryptor()
plaintext = decryptor.update(encrypted_stores) + decryptor.finalize()
unpadder = padding.PKCS7(128).unpadder()
plaintext = unpadder.update(plaintext) + unpadder.finalize()
plaintext = plaintext.decode("utf-8")

decoded_stores = json.loads(plaintext)
return decoded_stores



_SCRAPE_URL_ = 'https://finance.yahoo.com/quote'


Expand Down

0 comments on commit f398f46

Please sign in to comment.