Security and Cryptography in Python

Security and Cryptography in Python - Hash Functions(1)

Properties of a Hash Function

  1. It is an one-way deterministic function
  2. The output is dependent on all input bits
  3. Output is uniformly distributed
  4. "Impossible" (difficult) make a collision

Use of Hash Functions

  1. Digital signature
  2. Shadow files (passwords)
  3. HMAC
  4. Make deterministic identifiers

Code in Python:

import hashlib

def modify(m):
    l = list(m)
    l[0] = l[0] ^ 1
    return bytes(l)

m = "This is the hash value message".encode()

sha256 = hashlib.sha256()
sha256.update(m)
d = sha256.digest()
print(d)

sha256 = hashlib.sha256()
sha256.update(m)
d = sha256.digest()
print(d)

m = modify(m)
print(m)

sha256 = hashlib.sha256()
sha256.update(m)
d = sha256.digest()
print(d)

Running Result:

image-20210221201144526

相信未来 - 该面对的绝不逃避,该执著的永不怨悔,该舍弃的不再留念,该珍惜的好好把握。
原文地址:https://www.cnblogs.com/keepmoving1113/p/14426192.html