hash_val = str.encode(hash_obj.hexdigest())
时间: 2024-05-25 15:10:11 浏览: 15
这行代码的作用是将哈希对象 `hash_obj` 的十六进制摘要转换为字节串,并将其编码为指定字符集的字符串。
具体来说,`hash_obj.hexdigest()` 返回哈希对象的十六进制摘要,即一个由十六进制字符组成的字符串。然后,`str.encode()` 将该字符串转换为字节串。其中,`str` 是一个字符串对象,可以指定字符集(例如 UTF-8)。
最终,将字节串赋值给 `hash_val` 变量。
相关问题
def fpe(frame_phone_input,root): phone_input = frame_phone_input.get() def fpe_encrypt(plaintext, key, radix=10, rounds=4): # 对明文进行哈希 hash_val = hmac.new(key.encode(), plaintext.encode(), hashlib.sha256).digest() # 将哈希值转换为整数 num_val = int.from_bytes(hash_val, byteorder='big') # 将整数转换为指定进制的字符串 str_val = '' while num_val > 0: num_val, rem = divmod(num_val, radix) str_val = str(rem) + str_val # 对字符串进行 FPE 加密 for i in range(rounds): str_val = ''.join([chr((ord(c) - ord('0') + i) % (radix - 1) + ord('0')) for c in str_val]) # 返回加密后的字符串 return str_val key = 'my-secret-key' encrypted = fpe_encrypt(phone_input, key) phone_label = tk.Label(root, text=("加密后的手机号:", encrypted), font=("微软雅黑 -20")) phone_label.place(x=500, y=560)怎么把加密后的手机号分三行显示
你可以使用字符串的切片功能将加密后的手机号分成三段,然后分别显示在三个 Label 中。下面是一个修改过的代码示例:
```
def fpe(frame_phone_input, root):
phone_input = frame_phone_input.get()
def fpe_encrypt(plaintext, key, radix=10, rounds=4):
# 对明文进行哈希
hash_val = hmac.new(key.encode(), plaintext.encode(), hashlib.sha256).digest()
# 将哈希值转换为整数
num_val = int.from_bytes(hash_val, byteorder='big')
# 将整数转换为指定进制的字符串
str_val = ''
while num_val > 0:
num_val, rem = divmod(num_val, radix)
str_val = str(rem) + str_val
# 对字符串进行 FPE 加密
for i in range(rounds):
str_val = ''.join([chr((ord(c) - ord('0') + i) % (radix - 1) + ord('0')) for c in str_val])
# 返回加密后的字符串
return str_val
key = 'my-secret-key'
encrypted = fpe_encrypt(phone_input, key)
# 将加密后的手机号分成三段
chunk_size = len(encrypted) // 3
chunk1 = encrypted[:chunk_size]
chunk2 = encrypted[chunk_size:2*chunk_size]
chunk3 = encrypted[2*chunk_size:]
# 在三个 Label 中分别显示加密后的手机号的三段
phone_label1 = tk.Label(root, text=("加密后的手机号:", chunk1), font=("微软雅黑 -20"))
phone_label1.place(x=500, y=560)
phone_label2 = tk.Label(root, text=chunk2, font=("微软雅黑 -20"))
phone_label2.place(x=500, y=590)
phone_label3 = tk.Label(root, text=chunk3, font=("微软雅黑 -20"))
phone_label3.place(x=500, y=620)
```
这样,加密后的手机号就会分成三行显示。
def color(): if request.method == "POST": password=request.form['password'] image_file = request.files["image"] if password==str(password_hash): image_bytes = image_file.read() image_array = np.frombuffer(image_bytes, dtype=np.uint8) image = cv2.imdecode(image_array, cv2.IMREAD_COLOR) #img = Image.open(io.BytesIO(image_bytes)) data = model_person_data(object_model,image) print("转数据") data1 = pd.DataFrame(data)帮我修改为request接受一个uri值,并转为图片
当使用URI传递图像时,您需要使用Python的`requests`库向URI发送HTTP请求,将响应作为图像读取,并将其传递给OpenCV以进行处理。下面是一个修改版本的代码,可以实现您的需求:
```python
import requests
import numpy as np
import cv2
def color():
if request.method == "POST":
password = request.form['password']
image_uri = request.form['image_uri']
if password == str(password_hash):
response = requests.get(image_uri)
image_bytes = response.content
image_array = np.frombuffer(image_bytes, dtype=np.uint8)
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
data = model_person_data(object_model, image)
print("转数据")
data1 = pd.DataFrame(data)
```
注意,您需要将`image_uri`参数传递给函数作为`request.form`的一部分,它将包含URI字符串。然后,您需要使用Python的`requests`库从URI获取响应。`response.content`将返回响应的字节内容,您可以将其传递给OpenCV进行处理。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)