Security and Cryptography in Python - Hash Functions(1)

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

posted @ 2021-02-21 16:32  晨风_Eric  阅读(68)  评论(0编辑  收藏  举报