zoukankan      html  css  js  c++  java
  • Security and Cryptography in Python

    Security and Cryptography in Python - HMAC

    image-20210307102233320

    Implement in Python

    import hashlib
    
    # Alice and Bob share a secret key
    secret_key = "secret key".encode()
    
    # Alice wants to compute a MAC
    m = "Hey Bob. You are still awesome.".encode()
    sha256 = hashlib.sha256()
    sha256.update(secret_key)
    sha256.update(m)
    hmac = sha256.digest()
    
    print(m, hmac)
    
    # Bob receives and validates the HMAC
    sha256 = hashlib.sha256()
    sha256.update(secret_key)
    sha256.update(m)
    hmac = sha256.digest()
    print(m, hmac)
    

    Running Result:

    image-20210307103809072

    What happens if Eve modifies a message.

    import hashlib
    
    
    def modify(m):
        l = list(m)
        l[0] = l[0] ^ 1
        return bytes(l)
    
    
    # Alice and Bob share a secret key
    secret_key = "secret key".encode()
    
    # Alice wants to compute a MAC
    m = "Hey Bob. You are still awesome.".encode()
    sha256 = hashlib.sha256()
    sha256.update(secret_key)
    sha256.update(m)
    hmac = sha256.digest()
    print(m, hmac)
    
    # Eve comes along
    m = modify(m)
    print(m)
    
    # Bob receives and validates the HMAC
    sha256 = hashlib.sha256()
    sha256.update(secret_key)
    sha256.update(m)
    hmac = sha256.digest()
    print(m, hmac)
    
    

    Running Result:

    image-20210307104305678

    相信未来 - 该面对的绝不逃避,该执著的永不怨悔,该舍弃的不再留念,该珍惜的好好把握。
  • 相关阅读:
    ConnectionUtils
    设置组件内容模板和头部模板
    设置idea 代码模板
    Win10 安装流程 Maven
    IDEA: Error:java: 无效的源发行版: 9
    eclipse js的自动提示
    SQLserver SQL语句自动格式化工具的调出
    java计算两个n阶矩阵相乘
    JSP页面输出数据库表格
    threadpool 的配置实用
  • 原文地址:https://www.cnblogs.com/keepmoving1113/p/14493840.html
Copyright © 2011-2022 走看看