Skip to content

Commit 61daa92

Browse files
committed
add crypto
1 parent 813d506 commit 61daa92

File tree

1 file changed

+258
-0
lines changed

1 file changed

+258
-0
lines changed
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "63f1e1e1",
6+
"metadata": {},
7+
"source": [
8+
"## 暗号関連\n"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "c4285c12",
14+
"metadata": {},
15+
"source": [
16+
"### 安全な乱数を生成する\n",
17+
"\n",
18+
"- [secrets snippets](https://github.com/akagikouzanh/python-snippets-hub/blob/master/snippets/snippets_secrets.ipynb)\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 1,
24+
"id": "a9e82ffd",
25+
"metadata": {},
26+
"outputs": [
27+
{
28+
"name": "stdout",
29+
"output_type": "stream",
30+
"text": [
31+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\n",
32+
"3ivjDzOo\n"
33+
]
34+
}
35+
],
36+
"source": [
37+
"# パスワード乱数の生成\n",
38+
"import secrets\n",
39+
"import string\n",
40+
"\n",
41+
"alpha_num = string.ascii_letters + string.digits\n",
42+
"print(alpha_num)\n",
43+
"\n",
44+
"password = \"\".join(secrets.choice(alpha_num) for i in range(8))\n",
45+
"print(password)"
46+
]
47+
},
48+
{
49+
"cell_type": "code",
50+
"execution_count": 4,
51+
"id": "e890b7e1",
52+
"metadata": {},
53+
"outputs": [
54+
{
55+
"name": "stdout",
56+
"output_type": "stream",
57+
"text": [
58+
"secrets.token_bytes()=b'@h\\xee\\x80N\\x8c\\x81\\x1b\\xfa\\x0b^VX\\x1e\\xce\\xd3`\\x18_\\xc3)N\\xc3gpjj\\xc1\"\\xba\\rt'\n",
59+
"secrets.token_bytes(8)=b'%\\x00\\xfa\\x9dt\\xd1]\\x94'\n",
60+
"secrets.token_hex()='23156d16fc33b5f8f0c52da3f296b7c691a3291be42c381ad10f8886c0ebb5dd'\n",
61+
"secrets.token_hex(8)='2c5d445fbd19b33d'\n",
62+
"secrets.token_urlsafe()='Znglx1V9s5GE9swmcigY8TUJgwQpf5SLbXOsi_5xuj0'\n",
63+
"secrets.token_urlsafe(8)='APQeyLH4TOE'\n"
64+
]
65+
}
66+
],
67+
"source": [
68+
"# トークンの生成\n",
69+
"print(f\"{secrets.token_bytes()=}\") # バイト列を返す\n",
70+
"print(f\"{secrets.token_bytes(8)=}\") # 8バイト列を返す\n",
71+
"print(f\"{secrets.token_hex()=}\")\n",
72+
"print(f\"{secrets.token_hex(8)=}\") # 16進数文字列を返す\n",
73+
"print(f\"{secrets.token_urlsafe()=}\")\n",
74+
"print(f\"{secrets.token_urlsafe(8)=}\") # Base64エンコードされた文字列を返す"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": 5,
80+
"id": "edb4fa59",
81+
"metadata": {},
82+
"outputs": [
83+
{
84+
"name": "stdout",
85+
"output_type": "stream",
86+
"text": [
87+
"url='https://example.com/?reset=OyQuw4Tks9UeZmCGhcw1q-5QBAsaPgGYxhEA6nAA8_Y'\n",
88+
"qs={'reset': ['OyQuw4Tks9UeZmCGhcw1q-5QBAsaPgGYxhEA6nAA8_Y']}\n",
89+
"True\n"
90+
]
91+
}
92+
],
93+
"source": [
94+
"from urllib import parse\n",
95+
"\n",
96+
"reset_token = secrets.token_urlsafe()\n",
97+
"url = \"https://example.com/?reset=\" + reset_token\n",
98+
"print(f\"{url=}\")\n",
99+
"\n",
100+
"url_parse = parse.urlparse(url)\n",
101+
"qs = parse.parse_qs(url_parse.query)\n",
102+
"print(f\"{qs=}\")\n",
103+
"\n",
104+
"print(secrets.compare_digest(reset_token, qs[\"reset\"][0]))"
105+
]
106+
},
107+
{
108+
"cell_type": "markdown",
109+
"id": "cd1f335b",
110+
"metadata": {},
111+
"source": [
112+
"### ハッシュ値を生成する - hashlib\n"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": 6,
118+
"id": "68a6c30f",
119+
"metadata": {},
120+
"outputs": [
121+
{
122+
"data": {
123+
"text/plain": [
124+
"{'blake2b',\n",
125+
" 'blake2s',\n",
126+
" 'md5',\n",
127+
" 'md5-sha1',\n",
128+
" 'ripemd160',\n",
129+
" 'sha1',\n",
130+
" 'sha224',\n",
131+
" 'sha256',\n",
132+
" 'sha384',\n",
133+
" 'sha3_224',\n",
134+
" 'sha3_256',\n",
135+
" 'sha3_384',\n",
136+
" 'sha3_512',\n",
137+
" 'sha512',\n",
138+
" 'sha512_224',\n",
139+
" 'sha512_256',\n",
140+
" 'shake_128',\n",
141+
" 'shake_256',\n",
142+
" 'sm3'}"
143+
]
144+
},
145+
"execution_count": 6,
146+
"metadata": {},
147+
"output_type": "execute_result"
148+
}
149+
],
150+
"source": [
151+
"import hashlib\n",
152+
"\n",
153+
"hashlib.algorithms_available"
154+
]
155+
},
156+
{
157+
"cell_type": "code",
158+
"execution_count": 8,
159+
"id": "8c1c9936",
160+
"metadata": {},
161+
"outputs": [
162+
{
163+
"name": "stdout",
164+
"output_type": "stream",
165+
"text": [
166+
"hash_sha256.hexdigest()='feb7058093ca35f79765685fffb806583683718714502f2faafbcc188c1885b3'\n",
167+
"hashlib.sha256(b\"Python library book 2\").hexdigest()='feb7058093ca35f79765685fffb806583683718714502f2faafbcc188c1885b3'\n"
168+
]
169+
}
170+
],
171+
"source": [
172+
"import hashlib\n",
173+
"\n",
174+
"hash_sha256 = hashlib.sha256()\n",
175+
"hash_sha256.update(b\"Python library book 2\")\n",
176+
"\n",
177+
"# 出力は同一\n",
178+
"print(f\"{hash_sha256.hexdigest()=}\")\n",
179+
"print(f\"{hashlib.sha256(b\"Python library book 2\").hexdigest()=}\")"
180+
]
181+
},
182+
{
183+
"cell_type": "code",
184+
"execution_count": 9,
185+
"id": "ad8cc799",
186+
"metadata": {},
187+
"outputs": [
188+
{
189+
"name": "stdout",
190+
"output_type": "stream",
191+
"text": [
192+
"hashlib.md5(b\"Python library book 2\").hexdigest()='6a68d97b9bd04b673b12d354078aea27'\n",
193+
"hashlib.sha1(b\"Python library book 2\").hexdigest()='d52bd15b58fe7761d8dd1e4b4f2b27f2dc0aa0b2'\n",
194+
"hashlib.sha512(b\"Python library book 2\").hexdigest()='594b43bb099f8845c5ab85dd7a12a400a234f50424f962a6dc6681716f85aae33570744b7d8bd138a93e42965bf341823e810fbde4e08568d2957d0c0d1a1369'\n",
195+
"hashlib.sha3_512(b\"Python library book 2\").hexdigest()='e6366d61a6b2705e0291ea144434dc7396b9919a303d9d3f4ed553aea10d8f146937e64a10501a6bc6fb4a2dbe9754061f73ec2af50c1bbd2d94e559cb720837'\n"
196+
]
197+
}
198+
],
199+
"source": [
200+
"print(f\"{hashlib.md5(b\"Python library book 2\").hexdigest()=}\")\n",
201+
"print(f\"{hashlib.sha1(b\"Python library book 2\").hexdigest()=}\")\n",
202+
"print(f\"{hashlib.sha512(b\"Python library book 2\").hexdigest()=}\")\n",
203+
"print(f\"{hashlib.sha3_512(b\"Python library book 2\").hexdigest()=}\")"
204+
]
205+
},
206+
{
207+
"cell_type": "code",
208+
"execution_count": 10,
209+
"id": "97155700",
210+
"metadata": {},
211+
"outputs": [
212+
{
213+
"name": "stdout",
214+
"output_type": "stream",
215+
"text": [
216+
"hashed_password='68edb4f6fa8d876b6b479c002dc618efe739ed96c986420e8e57cb7d67278699'\n"
217+
]
218+
}
219+
],
220+
"source": [
221+
"password = b\"user_password\"\n",
222+
"salt = b\"your_secret_salt\"\n",
223+
"iterations = 100000\n",
224+
"hashed_password = hashlib.pbkdf2_hmac(\"sha256\", password, salt, iterations).hex()\n",
225+
"print(f\"{hashed_password=}\")"
226+
]
227+
},
228+
{
229+
"cell_type": "code",
230+
"execution_count": null,
231+
"id": "e500a26a",
232+
"metadata": {},
233+
"outputs": [],
234+
"source": []
235+
}
236+
],
237+
"metadata": {
238+
"kernelspec": {
239+
"display_name": "Python (snippets-hub)",
240+
"language": "python",
241+
"name": "python-snippets-hub"
242+
},
243+
"language_info": {
244+
"codemirror_mode": {
245+
"name": "ipython",
246+
"version": 3
247+
},
248+
"file_extension": ".py",
249+
"mimetype": "text/x-python",
250+
"name": "python",
251+
"nbconvert_exporter": "python",
252+
"pygments_lexer": "ipython3",
253+
"version": "3.13.0"
254+
}
255+
},
256+
"nbformat": 4,
257+
"nbformat_minor": 5
258+
}

0 commit comments

Comments
 (0)