试用Cursor AI自动编程有感

放假窝在家里拔草、写博客。一个偶然的机会,接触到Cursor AI,一个会写代码的AI。

Cursor AI官网下载回来、安装,过程挺简单的,并且是以VS Code插件的形式直接站在强大的VS Code的基础上工作,无须另起炉灶,从零造轮子。打开后VS Code的界面并没有大的变化,Ctrl + L调出对话窗。Cursor首先是调用 claude 3.5 sonnet与人沟通了解需求, 中文、英文都可以;然后就根据人的需求自己生成代码,再不厌其烦地修改,写代码的同时还生成各种帮助文本;也可以扔一段代码让Cursor帮忙检查、修改。


在网上找了一个大厂算法面试题,要求用python写一段代码,解决下面的问题: 有15个瓶子,其中最多有一瓶有毒,现在有四只老鼠,喝了有毒的水之后,第二天就会死。如何在第二天就可以判断出哪个瓶子有毒?扔给Cursor,一段20行的代码瞬间完成,并给出解题思路,便于理解代码。

这个显然太小儿科,于是,给Cursor出了第二题:

用python写一段代码,生成一个利用ASE256保存个人密码的安卓APP。需要用户用主帐号和密码登录,也可以用人脸识别登录。个人密码需要分成不用的类型;对个人弱密码进行提醒。有产生随机强密码的功能。把密码分为银行帐户、网站和邮箱登录两大类。每个银行帐户需要有对应的银行名称、帐户、支付密码和登录密码。网站和邮箱登录需要有记录对应的网址、用户名和密码。所有密码都需要记住生成或修改的日期,并且记录修改的三次履历。

也是瞬间得到了原代码,可惜没有安卓环境测试测试。于是告诉Cursor修改为windows平台,不停增加功能,不停重新修改代码,反复N轮;总共不足15分钟,一个Windows APP诞生。

除了近400行代码,Cursor自己还生成了下面的温馨提示:

这是完整的密码管理器代码。你可以直接复制这个代码到一个Python文件中运行。

记得安装所需的依赖:
pip install kivy cryptography

这个密码管理器现在可以:

  1. 注册新用户和登录
  2. 添加和查看银行账户信息
  3. 添加和查看网站/邮箱登录信息
  4. 生成强密码
  5. 加密存储所有密码信息
  6. 显示密码创建日期、最后修改日期和修改历史

使用时请注意保护你的主密码,因为它是解密所有其他密码的关键。

问题来了,面对掌握N门人类语言和N门编程语言、如此快速、又绝无怨言的AI,程序猿朋友们有没有在瑟瑟发抖呢?

——————

下面是Cursor生成的单机版密码管理器Python代码,复制回去就可以用啦:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
import os
import hashlib
import base64
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.scrollview import ScrollView
from kivy.uix.spinner import Spinner
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.backends import default_backend
from cryptography.fernet import Fernet
import sqlite3
import random
import string
import re
from datetime import datetime

# 用户类,用于处理用户认证
class User:
def __init__(self, username, password):
self.username = username
self.salt = os.urandom(16) # 生成随机盐值
self.password_hash = self._hash_password(password)

# 使用PBKDF2算法哈希密码
def _hash_password(self, password):
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=self.salt,
iterations=100000,
backend=default_backend()
)
return base64.urlsafe_b64encode(kdf.derive(password.encode()))

# 验证密码
def verify_password(self, password):
return self._hash_password(password) == self.password_hash

# 数据库类,处理所有数据库操作
class Database:
def __init__(self):
self.conn = sqlite3.connect('passwords.db')
self.create_tables()

# 创建必要的数据表
def create_tables(self):
cursor = self.conn.cursor()
# 创建用户表
cursor.execute('''
CREATE TABLE IF NOT EXISTS users
(username TEXT PRIMARY KEY, password_hash TEXT, salt TEXT)
''')
# 创建银行账户表
cursor.execute('''
CREATE TABLE IF NOT EXISTS bank_accounts
(id INTEGER PRIMARY KEY, username TEXT, bank_name TEXT, account_number TEXT,
login_password TEXT, payment_password TEXT, created_date TEXT,
last_modified TEXT, history TEXT)
''')
# 创建网站登录表
cursor.execute('''
CREATE TABLE IF NOT EXISTS website_logins
(id INTEGER PRIMARY KEY, username TEXT, website TEXT, url TEXT,
login_username TEXT, password TEXT, created_date TEXT,
last_modified TEXT, history TEXT)
''')
self.conn.commit()

# 添加新用户
def add_user(self, user):
cursor = self.conn.cursor()
cursor.execute('INSERT INTO users VALUES (?, ?, ?)',
(user.username, user.password_hash, user.salt))
self.conn.commit()

# 获取用户信息
def get_user(self, username):
cursor = self.conn.cursor()
cursor.execute('SELECT * FROM users WHERE username = ?', (username,))
user_data = cursor.fetchone()
if user_data:
user = User(user_data[0], '')
user.password_hash = user_data[1]
user.salt = user_data[2]
return user
return None

# 添加银行账户
def add_bank_account(self, username, bank_name, account_number, login_password, payment_password):
cursor = self.conn.cursor()
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor.execute('''
INSERT INTO bank_accounts
(username, bank_name, account_number, login_password, payment_password, created_date, last_modified, history)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
''', (username, bank_name, account_number, login_password, payment_password, now, now, ''))
self.conn.commit()

# 添加网站登录信息
def add_website_login(self, username, website, url, login_username, password):
cursor = self.conn.cursor()
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor.execute('''
INSERT INTO website_logins
(username, website, url, login_username, password, created_date, last_modified, history)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
''', (username, website, url, login_username, password, now, now, ''))
self.conn.commit()

# 更新银行账户信息
def update_bank_account(self, id, login_password, payment_password):
cursor = self.conn.cursor()
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor.execute('SELECT login_password, payment_password, last_modified, history FROM bank_accounts WHERE id = ?', (id,))
old_data = cursor.fetchone()
new_history = f"{old_data[2]}: Login={old_data[0]}, Payment={old_data[1]}\n{old_data[3]}"
new_history = '\n'.join(new_history.split('\n')[:3]) # 只保留最近3次修改记录
cursor.execute('''
UPDATE bank_accounts
SET login_password = ?, payment_password = ?, last_modified = ?, history = ?
WHERE id = ?
''', (login_password, payment_password, now, new_history, id))
self.conn.commit()

# 更新网站登录信息
def update_website_login(self, id, password):
cursor = self.conn.cursor()
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor.execute('SELECT password, last_modified, history FROM website_logins WHERE id = ?', (id,))
old_data = cursor.fetchone()
new_history = f"{old_data[1]}: {old_data[0]}\n{old_data[2]}"
new_history = '\n'.join(new_history.split('\n')[:3]) # 只保留最近3次修改记录
cursor.execute('''
UPDATE website_logins
SET password = ?, last_modified = ?, history = ?
WHERE id = ?
''', (password, now, new_history, id))
self.conn.commit()

# 获取用户的所有银行账户
def get_bank_accounts(self, username):
cursor = self.conn.cursor()
cursor.execute('SELECT * FROM bank_accounts WHERE username = ?', (username,))
return cursor.fetchall()

# 获取用户的所有网站登录信息
def get_website_logins(self, username):
cursor = self.conn.cursor()
cursor.execute('SELECT * FROM website_logins WHERE username = ?', (username,))
return cursor.fetchall()

db = Database()

# 检查密码强度
def check_password_strength(password):
score = 0
if len(password) >= 12:
score += 1
if re.search(r"[A-Z]", password):
score += 1
if re.search(r"[a-z]", password):
score += 1
if re.search(r"\d", password):
score += 1
if re.search(r"[!@#$%^&*(),.?\":{}|<>]", password):
score += 1
return ["非常弱", "弱", "中等", "强", "非常强"][score]

# 生成强密码
def generate_strong_password():
chars = string.ascii_letters + string.digits + string.punctuation
return ''.join(random.choice(chars) for _ in range(16))

# 获取加密密钥
def get_key(password, salt):
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32,
salt=salt,
iterations=100000,
backend=default_backend()
)
return base64.urlsafe_b64encode(kdf.derive(password.encode()))

# 加密密码
def encrypt_password(password, key):
f = Fernet(key)
return f.encrypt(password.encode()).decode()

# 解密密码
def decrypt_password(encrypted_password, key):
f = Fernet(key)
return f.decrypt(encrypted_password.encode()).decode()

# 登录界面
class LoginScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
layout = BoxLayout(orientation='vertical', padding=10, spacing=10)
self.username = TextInput(hint_text='用户名', multiline=False)
self.password = TextInput(hint_text='密码', password=True, multiline=False)
login_button = Button(text='登录')
register_button = Button(text='注册')

layout.add_widget(Label(text='密码管理器'))
layout.add_widget(self.username)
layout.add_widget(self.password)
layout.add_widget(login_button)
layout.add_widget(register_button)

login_button.bind(on_press=self.login)
register_button.bind(on_press=self.register)

self.add_widget(layout)

# 登录功能
def login(self, instance):
user = db.get_user(self.username.text)
if user and user.verify_password(self.password.text):
self.manager.current = 'password_manager'
self.manager.get_screen('password_manager').current_user = user
else:
self.show_popup('错误', '用户名或密码错误')

# 注册功能
def register(self, instance):
if self.username.text and self.password.text:
if db.get_user(self.username.text):
self.show_popup('错误', '用户名已存在')
else:
user = User(self.username.text, self.password.text)
db.add_user(user)
self.show_popup('成功', '注册成功,请登录')
else:
self.show_popup('错误', '请输入用户名和密码')

# 显示弹窗
def show_popup(self, title, content):
popup = Popup(title=title, content=Label(text=content),
size_hint=(None, None), size=(400, 200))
popup.open()

# 密码管理界面
class PasswordManagerScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.current_user = None
layout = BoxLayout(orientation='vertical', padding=10, spacing=10)

self.password_type = Spinner(text='选择密码类型', values=('银行账户', '网站/邮箱登录'))

# 银行账户输入布局
self.bank_layout = GridLayout(cols=2, spacing=10)
self.bank_name = TextInput(hint_text='银行名称', multiline=False)
self.account_number = TextInput(hint_text='账户号码', multiline=False)
self.login_password = TextInput(hint_text='登录密码', password=True, multiline=False)
self.payment_password = TextInput(hint_text='支付密码', password=True, multiline=False)

# 网站登录输入布局
self.website_layout = GridLayout(cols=2, spacing=10)
self.website = TextInput(hint_text='网站名称', multiline=False)
self.url = TextInput(hint_text='网址', multiline=False)
self.username = TextInput(hint_text='用户名', multiline=False)
self.password = TextInput(hint_text='密码', password=True, multiline=False)

# 添加银行账户输入字段
self.bank_layout.add_widget(Label(text='银行名称:'))
self.bank_layout.add_widget(self.bank_name)
self.bank_layout.add_widget(Label(text='账户号码:'))
self.bank_layout.add_widget(self.account_number)
self.bank_layout.add_widget(Label(text='登录密码:'))
self.bank_layout.add_widget(self.login_password)
self.bank_layout.add_widget(Label(text='支付密码:'))
self.bank_layout.add_widget(self.payment_password)

# 添加网站登录输入字段
self.website_layout.add_widget(Label(text='网站名称:'))
self.website_layout.add_widget(self.website)
self.website_layout.add_widget(Label(text='网址:'))
self.website_layout.add_widget(self.url)
self.website_layout.add_widget(Label(text='用户名:'))
self.website_layout.add_widget(self.username)
self.website_layout.add_widget(Label(text='密码:'))
self.website_layout.add_widget(self.password)

# 按钮布局
button_layout = BoxLayout(spacing=10)
add_button = Button(text='添加密码')
generate_button = Button(text='生成强密码')
show_passwords_button = Button(text='显示所有密码')
logout_button = Button(text='登出')

button_layout.add_widget(add_button)
button_layout.add_widget(generate_button)
button_layout.add_widget(show_passwords_button)
button_layout.add_widget(logout_button)

self.strength_label = Label(text='密码强度: ')

# 添加所有组件到主布局
layout.add_widget(self.password_type)
layout.add_widget(self.bank_layout)
layout.add_widget(self.website_layout)
layout.add_widget(self.strength_label)
layout.add_widget(button_layout)

# 绑定按钮事件
add_button.bind(on_press=self.add_password)
generate_button.bind(on_press=self.generate_password)
show_passwords_button.bind(on_press=self.show_passwords)
logout_button.bind(on_press=self.logout)
self.password_type.bind(text=self.on_password_type_change)

self.add_widget(layout)
self.on_password_type_change(None, self.password_type.text)

# 切换密码类型时更新界面
def on_password_type_change(self, instance, value):
if value == '银行账户':
self.bank_layout.opacity = 1
self.website_layout.opacity = 0
else:
self.bank_layout.opacity = 0
self.website_layout.opacity = 1

# 添加新密码
def add_password(self, instance):
if not self.current_user:
self.show_popup('错误', '请先登录')
return

if self.password_type.text == '银行账户':
if self.bank_name.text and self.account_number.text and self.login_password.text and self.payment_password.text:
key = get_key(self.current_user.password_hash, self.current_user.salt)
encrypted_login = encrypt_password(self.login_password.text, key)
encrypted_payment = encrypt_password(self.payment_password.text, key)
db.add_bank_account(self.current_user.username, self.bank_name.text, self.account_number.text,
encrypted_login, encrypted_payment)
self.show_popup('成功', '银行账户信息已添加')
self.clear_inputs()
else:
self.show_popup('错误', '请填写所有银行账户信息')
else:
if self.website.text and self.url.text and self.username.text and self.password.text:
key = get_key(self.current_user.password_hash, self.current_user.salt)
encrypted_password = encrypt_password(self.password.text, key)
db.add_website_login(self.current_user.username, self.website.text, self.url.text,
self.username.text, encrypted_password)
self.show_popup('成功', '网站登录信息已添加')
self.clear_inputs()
else:
self.show_popup('错误', '请填写所有网站登录信息')

# 生成强密码
def generate_password(self, instance):
password = generate_strong_password()
if self.password_type.text == '银行账户':
self.login_password.text = password
self.payment_password.text = generate_strong_password()
else:
self.password.text = password
self.update_password_strength()

# 更新密码强度显示
def update_password_strength(self):
if self.password_type.text == '银行账户':
login_strength = check_password_strength(self.login_password.text)
payment_strength = check_password_strength(self.payment_password.text)
self.strength_label.text = f'密码强度: 登录密码 - {login_strength}, 支付密码 - {payment_strength}'
else:
strength = check_password_strength(self.password.text)
self.strength_label.text = f'密码强度: {strength}'

# 显示所有密码
def show_passwords(self, instance):
if not self.current_user:
self.show_popup('错误', '请先登录')
return

key = get_key(self.current_user.password_hash, self.current_user.salt)
bank_accounts = db.get_bank_accounts(self.current_user.username)
website_logins = db.get_website_logins(self.current_user.username)

password_text = "银行账户:\n"
for account in bank_accounts:
password_text += f"银行: {account[2]}\n"
password_text += f"账户: {account[3]}\n"
password_text += f"登录密码: {decrypt_password(account[4], key)}\n"
password_text += f"支付密码: {decrypt_password(account[5], key)}\n"
password_text += f"创建日期: {account[6]}\n"
password_text += f"最后修改: {account[7]}\n"
password_text += f"修改历史: {account[8]}\n\n"

password_text += "\n网站/邮箱登录:\n"
for login in website_logins:
password_text += f"网站: {login[2]}\n"
password_text += f"网址: {login[3]}\n"
password_text += f"用户名: {login[4]}\n"
password_text += f"密码: {decrypt_password(login[5], key)}\n"
password_text += f"创建日期: {login[6]}\n"
password_text += f"最后修改: {login[7]}\n"
password_text += f"修改历史: {login[8]}\n\n"

content = ScrollView(size_hint=(1, None), size=(400, 300))
content.add_widget(Label(text=password_text, size_hint_y=None, height=len(password_text.split('\n')) * 20))

popup = Popup(title='所有密码', content=content,
size_hint=(None, None), size=(450, 400))
popup.open()

# 登出
def logout(self, instance):
self.current_user = None
self.manager.current = 'login'

# 显示弹窗
def show_popup(self, title, content):
popup = Popup(title=title, content=Label(text=content),
size_hint=(None, None), size=(400, 200))
popup.open()

# 清空输入框
def clear_inputs(self):
self.bank_name.text = ''
self.account_number.text = ''
self.login_password.text = ''
self.payment_password.text = ''
self.website.text = ''
self.url.text = ''
self.username.text = ''
self.password.text = ''
self.strength_label.text = '密码强度: '

# 主应用类
class PasswordManagerApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(LoginScreen(name='login'))
sm.add_widget(PasswordManagerScreen(name='password_manager'))
return sm

if __name__ == '__main__':
PasswordManagerApp().run()


试用Cursor AI自动编程有感
https://www.ezdata.top/4c42797f59c1.html
Author
average Joe
Posted on
October 4, 2024
Licensed under