def verifyKey(Sock): global txtMsgList, SERVERPUBLIC while True: Message = Sock.recv(BUFF) # f = open("./temp.txt",'w') # f.write(str(Message)) # f.close() print("shoudao de xiaoxi "+str(Message)) (publickey, hash_256) = pickle.loads(Message) if hash_256 == hashalg.hash_sha256(publickey): txtMsgList.insert(END, "公钥完整性验证完成,可以开始传输文件\n") SERVERPUBLIC = publickey txtMsgList.insert(END, "收到公钥\n" + SERVERPUBLIC.decode('utf-8') + "\n") break else: txtMsgList.insert(END, "验证失败\n")
时间: 2023-06-20 10:08:21 浏览: 71
这段代码是一个Python函数,名为verifyKey。它的作用是从一个套接字(Sock)接收一个消息(Message),并验证该消息中包含的公钥的完整性。该消息使用pickle模块进行序列化,其中包含公钥和该公钥的SHA-256哈希值。如果哈希值与公钥的SHA-256哈希值匹配,则公钥被认为是完整的,并将其保存在全局变量SERVERPUBLIC中。如果哈希值不匹配,则函数向用户输出验证失败的消息。最后,该函数返回到调用它的地方,并且可以开始传输文件。
相关问题
@RestController public class CaptchaController { @Resource(name = "captchaProducer") private Producer captchaProducer; @Resource(name = "captchaProducerMath") private Producer captchaProducerMath; @Autowired private RedisCache redisCache; @Autowired private ISysConfigService configService; /** * 生成验证码 */ @GetMapping("/captchaImage") public AjaxResult getCode(HttpServletResponse response) throws IOException { AjaxResult ajax = AjaxResult.success(); boolean captchaEnabled = configService.selectCaptchaEnabled(); ajax.put("captchaEnabled", captchaEnabled); if (!captchaEnabled) { return ajax; } // 保存验证码信息 String uuid = IdUtils.simpleUUID(); String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid; String capStr = null, code = null; BufferedImage image = null; // 生成验证码 String captchaType = RuoYiConfig.getCaptchaType(); if ("math".equals(captchaType)) { String capText = captchaProducerMath.createText(); capStr = capText.substring(0, capText.lastIndexOf("@")); code = capText.substring(capText.lastIndexOf("@") + 1); image = captchaProducerMath.createImage(capStr); } else if ("char".equals(captchaType)) { capStr = code = captchaProducer.createText(); image = captchaProducer.createImage(capStr); } redisCache.setCacheObject(verifyKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES); // 转换流信息写出 FastByteArrayOutputStream os = new FastByteArrayOutputStream(); try { ImageIO.write(image, "jpg", os); } catch (IOException e) { return AjaxResult.error(e.getMessage()); } ajax.put("uuid", uuid); ajax.put("img", Base64.encode(os.toByteArray())); return ajax; } }编写一个测试这个接口的代码
您可以使用以下代码来测试这个接口:
```java
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
String captchaUrl = "http://localhost:8080/captchaImage"; // 替换为实际接口地址
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
HttpEntity<?> entity = new HttpEntity<>(headers);
ResponseEntity<String> response = restTemplate.exchange(captchaUrl, HttpMethod.GET, entity, String.class);
if (response.getStatusCode() == HttpStatus.OK) {
String responseBody = response.getBody();
System.out.println(responseBody); // 输出接口返回的数据
} else {
System.out.println("请求失败");
}
}
```
请注意替换 `captchaUrl` 为实际的接口地址。该代码会发送一个 GET 请求,并输出接口返回的数据。
<script> export default { data() { return { //验证码地址 codeUrl: '', form: { username: '', password: '', verifyCode: "", //验证码 verifyKey: "", } } }, // onShow: function() { // this.getCodeUrl() // }, created() { this.getCodeUrl() }, methods: { // 获取验证码 getCodeUrl() { uni.request({ url: this.$globalVar + '/captcha/get', success: res => { if (res.data.code === 0) { this.codeUrl = res.data.data.base64stringC; this.form.verifyKey = res.data.data.idKeyC; } }, fail: err => { console.log(err) } }) }, //登陆方法 login() { uni.showLoading({ title: '登录中...', mask: true }) uni.request({ url: this.$globalVar + '/system/login', method: 'POST', data: this.form, success(res) { if (res.data.code === 0) { uni.showToast({ title: "登录成功", duration: 2000, }); uni.hideLoading() uni.setStorageSync('token', res.data.data.token); uni.reLaunch({ url: '/pages/index/index' }) } else { uni.hideLoading() if (res.data.msg === '验证码错误') { setTimeout(() => { this.getCodeUrl() }, 2000) } else { uni.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }); } } }, fail(res) { uni.hideLoading() uni.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }); } }) }, // 去注册 gotoRegister() { uni.navigateTo({ url: '/pages/register/register' }) } } } </script> 当登录失败报错验证码错误时 刷新验证码 这是我代码 帮我改一下
你可以将以下代码替换到你的代码中,这样当登录失败且错误为验证码错误时,就会自动刷新验证码:
```
// 登录方法
login() {
uni.showLoading({
title: '登录中...',
mask: true
})
uni.request({
url: this.$globalVar + '/system/login',
method: 'POST',
data: this.form,
success(res) {
if (res.data.code === 0) {
uni.showToast({
title: "登录成功",
duration: 2000,
});
uni.hideLoading()
uni.setStorageSync('token', res.data.data.token);
uni.reLaunch({
url: '/pages/index/index'
})
} else {
uni.hideLoading()
if (res.data.msg === '验证码错误') {
setTimeout(() => {
this.getCodeUrl()
}, 2000)
} else {
uni.showToast({
title: res.data.msg,
icon: 'none',
duration: 2000
});
}
}
},
fail(res) {
uni.hideLoading()
uni.showToast({
title: res.data.msg,
icon: 'none',
duration: 2000
});
}
})
},
```
阅读全文