Skip to content

Commit f398f46

Browse files
committed
Switch 'pycryptodome' -> 'cryptography'
1 parent 097c76a commit f398f46

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ To install `yfinance` using `conda`, see
306306
- [frozendict](https://pypi.org/project/frozendict) \>= 2.3.4
307307
- [beautifulsoup4](https://pypi.org/project/beautifulsoup4) \>= 4.11.1
308308
- [html5lib](https://pypi.org/project/html5lib) \>= 1.1
309-
- [pycryptodome](pycryptodome>=3.6.6) \>= 3.6.6
309+
- [cryptography](https://pypi.org/project/cryptography) \>= 3.3.2
310310

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

meta.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ requirements:
2626
- frozendict >=2.3.4
2727
- beautifulsoup4 >=4.11.1
2828
- html5lib >=1.1
29-
- pycryptodome >=3.6.6
29+
# - pycryptodome >=3.6.6
30+
- cryptography >=3.3.2
3031
- pip
3132
- python
3233

@@ -41,7 +42,8 @@ requirements:
4142
- frozendict >=2.3.4
4243
- beautifulsoup4 >=4.11.1
4344
- html5lib >=1.1
44-
- pycryptodome >=3.6.6
45+
# - pycryptodome >=3.6.6
46+
- cryptography >=3.3.2
4547
- python
4648

4749
test:

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ pytz>=2022.5
88
frozendict>=2.3.4
99
beautifulsoup4>=4.11.1
1010
html5lib>=1.1
11-
pycryptodome>=3.6.6
11+
cryptography>=3.3.2

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@
6262
install_requires=['pandas>=1.3.0', 'numpy>=1.16.5',
6363
'requests>=2.26', 'multitasking>=0.0.7',
6464
'lxml>=4.9.1', 'appdirs>=1.4.4', 'pytz>=2022.5',
65-
'frozendict>=2.3.4', 'pycryptodome>=3.6.6',
65+
'frozendict>=2.3.4',
66+
# 'pycryptodome>=3.6.6',
67+
'cryptography>=3.3.2',
6668
'beautifulsoup4>=4.11.1', 'html5lib>=1.1'],
6769
entry_points={
6870
'console_scripts': [

yfinance/data.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import functools
22
from functools import lru_cache
3+
34
import hashlib
45
from base64 import b64decode
5-
from Crypto.Cipher import AES
6-
from Crypto.Util.Padding import unpad
6+
usePycryptodome = False # slightly faster
7+
# usePycryptodome = True
8+
if usePycryptodome:
9+
from Crypto.Cipher import AES
10+
from Crypto.Util.Padding import unpad
11+
else:
12+
from cryptography.hazmat.primitives import padding
13+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
714

815
import requests as requests
916
import re
@@ -52,14 +59,7 @@ def decrypt_cryptojs_aes(data):
5259
salt = encrypted_stores[8:16]
5360
encrypted_stores = encrypted_stores[16:]
5461

55-
def EVPKDF(
56-
password,
57-
salt,
58-
keySize=32,
59-
ivSize=16,
60-
iterations=1,
61-
hashAlgorithm="md5",
62-
) -> tuple:
62+
def EVPKDF(password, salt, keySize=32, ivSize=16, iterations=1, hashAlgorithm="md5") -> tuple:
6363
"""OpenSSL EVP Key Derivation Function
6464
Args:
6565
password (Union[str, bytes, bytearray]): Password to generate key from.
@@ -100,14 +100,22 @@ def EVPKDF(
100100

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

103-
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
104-
plaintext = cipher.decrypt(encrypted_stores)
105-
plaintext = unpad(plaintext, 16, style="pkcs7")
103+
if usePycryptodome:
104+
cipher = AES.new(key, AES.MODE_CBC, iv=iv)
105+
plaintext = cipher.decrypt(encrypted_stores)
106+
plaintext = unpad(plaintext, 16, style="pkcs7")
107+
else:
108+
cipher = Cipher(algorithms.AES(key), modes.CBC(iv))
109+
decryptor = cipher.decryptor()
110+
plaintext = decryptor.update(encrypted_stores) + decryptor.finalize()
111+
unpadder = padding.PKCS7(128).unpadder()
112+
plaintext = unpadder.update(plaintext) + unpadder.finalize()
113+
plaintext = plaintext.decode("utf-8")
114+
106115
decoded_stores = json.loads(plaintext)
107116
return decoded_stores
108117

109118

110-
111119
_SCRAPE_URL_ = 'https://finance.yahoo.com/quote'
112120

113121

0 commit comments

Comments
 (0)