Skip to content

Commit b8372ff

Browse files
committed
update File IO
1 parent 8304f43 commit b8372ff

File tree

2 files changed

+18
-36
lines changed

2 files changed

+18
-36
lines changed

demos/users.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# list of players information
12
23
lost: 2
34
name: John Doe

notebooks/Ch10-1-Files.ipynb

+17-36
Original file line numberDiff line numberDiff line change
@@ -1822,32 +1822,31 @@
18221822
},
18231823
{
18241824
"cell_type": "code",
1825-
"execution_count": 16,
1825+
"execution_count": 32,
18261826
"metadata": {},
18271827
"outputs": [],
18281828
"source": [
18291829
"def get_hashed_password(password, salt):\n",
18301830
" import hashlib\n",
1831-
" return hashlib.sha256((password + salt).encode('utf-8')).hexdigest()"
1831+
" round = 100_000\n",
1832+
" return hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), salt.encode('utf-8'), round).hex().upper()"
18321833
]
18331834
},
18341835
{
18351836
"cell_type": "code",
1836-
"execution_count": 17,
1837+
"execution_count": null,
18371838
"metadata": {},
18381839
"outputs": [
18391840
{
18401841
"name": "stdout",
18411842
"output_type": "stream",
18421843
"text": [
1843-
"salt = 'PJLLqVpW4qpa1hIu'\n",
1844-
"hashed_password = 'bc0951eff5171243056cd7a3a073ba8b2366a8230c997c5fac8cd3052d20fcec'\n"
1844+
"salt = 'MPWAcxbAyiVXdpeU'\n",
1845+
"hashed_password = 'AFE4AD9E6FF039DCD90C82705C94AC3EB231AB6B8045D2B1B519640DA8518B22'\n"
18451846
]
18461847
}
18471848
],
18481849
"source": [
1849-
"import hashlib\n",
1850-
"\n",
18511850
"salt = get_random_salt()\n",
18521851
"print(f'{salt = }')\n",
18531852
"salted_password = 'P@ssw0rd'+salt\n",
@@ -1857,49 +1856,31 @@
18571856
},
18581857
{
18591858
"cell_type": "code",
1860-
"execution_count": null,
1859+
"execution_count": 34,
18611860
"metadata": {},
18621861
"outputs": [],
18631862
"source": [
18641863
"# create a new player\n",
18651864
"new_user = {}\n",
1866-
"new_user['name']= 'Michael Jordan'\n",
1867-
"new_user['email']= '[email protected]'\n",
1868-
"new_user['password']= hashed_password\n",
1869-
"new_user['salt']= salt\n",
1865+
"new_user['name'] = 'Michael Jordan'\n",
1866+
"new_user['email'] = '[email protected]'\n",
1867+
"new_user['password'] = hashed_password\n",
1868+
"new_user['salt'] = salt\n",
18701869
"new_user['played'] = 20\n",
18711870
"new_user['lost'] = 5"
18721871
]
18731872
},
18741873
{
18751874
"cell_type": "code",
1876-
"execution_count": null,
1875+
"execution_count": 35,
18771876
"metadata": {},
18781877
"outputs": [
18791878
{
1880-
"data": {
1881-
"text/plain": [
1882-
"[{'name': 'John Doe',\n",
1883-
" 'email': '[email protected]',\n",
1884-
" 'password': 'password',\n",
1885-
" 'played': 10,\n",
1886-
" 'lost': 2},\n",
1887-
" {'name': 'Jane Doe',\n",
1888-
" 'email': '[email protected]',\n",
1889-
" 'password': 'password123',\n",
1890-
" 'played': 5,\n",
1891-
" 'lost': 1},\n",
1892-
" {'name': 'Michael Jordan',\n",
1893-
" 'email': '[email protected]',\n",
1894-
" 'password': 'bc0951eff5171243056cd7a3a073ba8b2366a8230c997c5fac8cd3052d20fcec',\n",
1895-
" 'salt': 'PJLLqVpW4qpa1hIu',\n",
1896-
" 'played': 20,\n",
1897-
" 'lost': 5}]"
1898-
]
1899-
},
1900-
"execution_count": 23,
1901-
"metadata": {},
1902-
"output_type": "execute_result"
1879+
"name": "stdout",
1880+
"output_type": "stream",
1881+
"text": [
1882+
"[{'name': 'John Doe', 'email': '[email protected]', 'password': 'password', 'played': 10, 'lost': 2}, {'name': 'Jane Doe', 'email': '[email protected]', 'password': 'password123', 'played': 5, 'lost': 1}, {'name': 'Michael Jordan', 'email': '[email protected]', 'password': 'bc0951eff5171243056cd7a3a073ba8b2366a8230c997c5fac8cd3052d20fcec', 'salt': 'PJLLqVpW4qpa1hIu', 'played': 20, 'lost': 5}, {'name': 'Michael Jordan', 'email': '[email protected]', 'password': 'AFE4AD9E6FF039DCD90C82705C94AC3EB231AB6B8045D2B1B519640DA8518B22', 'salt': 'MPWAcxbAyiVXdpeU', 'played': 20, 'lost': 5}]\n"
1883+
]
19031884
}
19041885
],
19051886
"source": [

0 commit comments

Comments
 (0)