主要讲hash,题目是“A Way to Identify Everything”,也就是用hash可以验证发送的数据是否被修改
hash
Example
1 Hashing in Python
文字的hash
1 2 3 4 5 6
import hashlib # Hash functions expect bytes as input: the encode() method turns strings to bytes input_bytes = b"backpack" output = hashlib.sha256(input_bytes) # We use hexdigest() to convert bytes to hex because it's easier to read print(output.hexdigest())
2 Hashing images
图片的hash
1 2 3 4 5
from hashlib import sha256 file = open("my_image.jpg", "rb") hash = sha256(file.read()).hexdigest() file.close() print(f"The hash of my file is: {hash}")
3 Sending untamperable emails
加了一部分密文,hash
1 2 3 4 5 6 7
from hashlib import sha256 secret_phrase = "bolognese" defget_hash_with_secret_phrase(input_data, secret_phrase): combined = input_data + secret_phrase return sha256(combined.encode()).hexdigest() email_body = "Hey Bob,I think you should learn about Blockchains! I've been investing in Bitcoin and currently have exactly 12.03 BTC in my account." print(get_hash_with_secret_phrase(email_body, secret_phrase))
defsend_welcome_message(self,writer): message = dedent(f""" === Welcome {writer.nickname}! There are {len(self.connection_pool) - 1} user(s) here beside you Help: - Type anything to chat - /list will list all the connected users - /quit will disconnect you === """)
writer.write(f"{message}/n".encode())
defbroadcast(self,writer,message): for user in self.connection_pool: if user != writer: # We don't need to also broadcast to the user sending the message user.write(f"{message}/n".encode())
defbroadcast_user_join(self,writer): self.broadcast(writer,f"{writer.nickname} just joined") defbroadcast_user_quit(self,writer): self.broadcast(writer,f"{writer.nickname} just quit")
if writer.is_closing(): break # We're new outside the message loop, and the user has quit.Let's clean up... writer.close() await writer.wait_closed() connection_pool.remove_user_from_pool(writer)
asyncdefmain(): server = await asyncio.start_server(handle_connection,"0.0.0.0",8888)
from hashlib import sha256 message = "Hello Bob, Let's meet at the Kruger National Park on 2020-12-12 at 1pm." hash_message = sha256(("p@55w0rd" + message).encode()).hexdigest() print(hash_message)
bob验证数据未被修改
1 2 3 4 5 6
from hashlib import sha256 alices_message = "Hello Bob, Let's meet at the Kruger National Park on 2020-12-12 at 1pm." alices_hash = "39aae6ffdb3c0ac1c1cc0f50bf08871a729052cf1133c4c9b44a5bab8fb66211" hash_message = sha256(("p@55w0rd" + alices_message).encode()).hexdigest() if hash_message == alices_hash: print("Message has not been tampered with")
2 加密传送数据
发送数据:
公钥发送
私钥对数据签名
接收
verify验证
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import nacl.encoding import nacl.signing
#Generate a new key-pair for Bob bobs_private_key = nacl.signing.SigningKey.generate() bobs_public_key = bobs_private_key.verify_key print(bobs_public_key)
#Since it's bytes,we'll need to serialize the key to a readable format before publishing it: bobs_public_key_hex = bobs_public_key.encode(encoder=nacl.encoding.HexEncoder) print("/n/n") print(bobs_public_key_hex) #Now,Let's sign a message with it signed = bobs_private_key.sign(b"Send $37 to Alice") print("/n/n") print(signed)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import nacl.encoding import nacl.signing
# From the above example... bobs_public_key = 'acc5cc1750b841f0b383e5f620282946476c37d602c42b9dbaccb9db758b728e'
# We generate the verify_key verify_key = nacl.signing.VerifyKey(bobs_public_key,encoder=nacl.encoding.HexEncoder)
signed_message = b"/x8b/x08/x93/xb0/xda!r)/x19i/x18d/xe0/xbbi/x8a@c/xf8-/x8e/xbdr/xb7/xe8%/x0eez/x99/xd2/x0c/xc8/x14_/x8d/x02zWV/x8e/x81/xfb[9/x9b/x9d/x1b/xbb/xda/xe7/x05/x945/xc2/xef~/x17/x90/xfd3'/xc3/x00Send $37 to Alice"
# Now we attempt to verify the message # Any invalidation will result in an Exception being thrown verify_key.verify(signed_message)