博客
关于我
理解hmac module in Python
阅读量:348 次
发布时间:2019-03-04

本文共 724 字,大约阅读时间需要 2 分钟。

HMAC算法实现

HMAC(哈希消息认证码)是一种基于安全哈希函数的认证机制,广泛应用于数据完整性验证。本文将介绍Python中的hmac库实现方法。

主要功能

HMAC库提供了多种功能,包括密钥生成、消息处理和摘要算法选择等。以下是主要函数和方法:

  • 创建HMAC对象

    hmac.new(key, msg=None, digestmod='')
    创建一个新的HMAC对象,参数包括密钥(key)、消息(msg,可选,默认为空)以及摘要算法(digestmod,默认为空)。支持多种摘要算法如MD5、SHA-1等。
  • 更新消息

    HMAC.update(msg)
    将消息片段添加到当前HMAC对象中,支持多次调用以处理大数据量。
  • 生成摘要

    HMAC.digest()
    计算并返回消息的摘要值。
  • 生成十六进制字符串

    HMAC.hexdigest()
    返回摘要值的十六进制字符串表示。
  • 对象方法

    HMAC对象支持多种方法包括:

    copy()、digest_size、block_size、compare_digest(a, b)

使用示例

以下是一个典型的使用示例:

import hmacmessage = b'Hello, world!'key = b'secret'h = hmac.new(key, message, digestmod='MD5')# 如果消息较长,可以多次调用h.update(msg)h.update(b'更多数据')hexdigest = h.hexdigest()

参考资料

该实现基于以下安全哈希算法标准:

  • Secure Hash Standard (SHS)
  • MessageDigest算法家族(如MD5、SHA系列等)

转载地址:http://upge.baihongyu.com/

你可能感兴趣的文章
org.apache.ibatis.binding.BindingException: Invalid bound statement错误一例
查看>>
org.apache.ibatis.exceptions.PersistenceException:
查看>>
org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned
查看>>
org.apache.ibatis.type.TypeException: Could not resolve type alias 'xxxx'异常
查看>>
org.apache.poi.hssf.util.Region
查看>>
org.apache.xmlbeans.XmlOptions.setEntityExpansionLimit(I)Lorg/apache/xmlbeans/XmlOptions;
查看>>
org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /
查看>>
org.hibernate.HibernateException: Unable to get the default Bean Validation factory
查看>>
org.hibernate.ObjectNotFoundException: No row with the given identifier exists:
查看>>
org.springframework.amqp.AmqpConnectException:java.net.ConnectException:Connection timed out:connect
查看>>
org.springframework.beans.factory.BeanDefinitionStoreException
查看>>
org.springframework.boot.context.properties.ConfigurationBeanFactoryMetadata
查看>>
org.springframework.boot:spring boot maven plugin丢失---SpringCloud Alibaba_若依微服务框架改造_--工作笔记012
查看>>
SQL-CLR 类型映射 (LINQ to SQL)
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
查看>>
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded
查看>>
org.tinygroup.serviceprocessor-服务处理器
查看>>
org/eclipse/jetty/server/Connector : Unsupported major.minor version 52.0
查看>>
org/hibernate/validator/internal/engine
查看>>