hmac(hex-based message authentication code) In the process of hash calculation, the algorithm mixes key( It's actually salt ), and hashlib Compared with the ordinary encryption algorithm in the module , It can prevent the password from being cracked by the library , safer .

hmac Use of modules :

  • The import module

    import hmac
  • new(key,msg=None,digestmod) Method

    • Create a hash object
    • key and digestmod Parameter must specify ,key and msg( What needs to be encrypted ) Are all bytes type ,digestmod Specify encryption algorithm , such as ‘md5’,'sha1' etc.
  • object update(bytes) Method

    • Same as hashlib The common encryption algorithm in the module is the same , If the file is large , It can be done many times update() The way to pass in content , As long as the contents end up being the same , The result will not change
  • object digest() Method

    • return bytes Type hash value
  • object hexdigest() Method

    • Returns the hexadecimal hash value
  • Example :

    In [1]: import hmac # Import hmac modular
    In [2]: h1 = hmac.new(b'hash',b'helloworld',digestmod='sha1').hexdigest() # Create a hash object , Pass in content that needs to be encrypted at one time , Specify encryption algorithm sha1, And returns the hexadecimal hash value
    In [3]: h1
    Out[3]: '854276942fbdab217767515057a133474fb0fd83' In [4]: h2 = hmac.new(b'hash',b'helloworld',digestmod='sha1').digest()# return bytes Hash value of type In [5]: h2
    Out[5]: b'\x85Bv\x94/\xbd\xab!wgQPW\xa13GO\xb0\xfd\x83' In [6]: h3 = hmac.new(b'hash',digestmod='sha1')# Create a hash object In [7]: h3.update(b'hello')# First pass in some content that needs to be encrypted In [8]: h3.update(b'world')# Then pass in the rest of the content that needs to be encrypted In [9]: h3.hexdigest()# Returns the hexadecimal hash value , The result is the same when all content is passed in at one time
    Out[9]: '854276942fbdab217767515057a133474fb0fd83'

python in hmac More about the use of modules

  1. python in hashlib Examples of module usage

    python in hashlib Examples of module usage We've introduced one before Python Encrypted articles :Python Detailed explanation of encryption examples . Let's see today python in hashlib Examples of module usage , As follows . hashlib ha ...

  2. Python in optionParser How to use the module [ turn ]

    In this paper, the form of a more detailed example of Python in optionParser How to use the module , For in-depth learning Python It has a good reference value . Share it with you for your reference . The specific analysis is as follows : Generally speaking ,Python There are two of them ...

  3. python in threading Module details ( One )

    python in threading Module details ( One ) source  http://blog.chinaunix.net/uid-27571599-id-3484048.html threading Provides a comparison of thr ...

  4. 【 turn 】 About python in re modular split Use of methods

    notes : I've been studying text processing recently , You need to use regular cut text , So I got this article , It is useful to , Thank the original author . Original address :http://blog.sciencenet.cn/blog-314114-775285.html About ...

  5. Python Introduction and use of the module in

    stay Python There is a concept called module (module), This and C The header file in the language and Java The bags in are very similar to , For example Python To call sqrt function , Must use import Keyword import math This module , Let's take a look at ...

  6. python The nature of the import module in , The solution for not importing the handwriting module

    Recently, some of my friends wrote their own modules in the project , The problem of unable to import when importing . Let me share with you about python Some basic knowledge of import module in . 1 Look for paths when importing modules In every running python In the process , It's all maintained ...

  7. Python in time Module details

    Python in time Module details In normal code , We often need to deal with time . stay Python in , Modules related to time processing include :time,datetime as well as calendar. This article , Main explanation time modular . ...

  8. Python in collections modular

    Catalog Python in collections modular Counter defaultdict OrderedDict namedtuple deque ChainMap Python in collections ...

  9. Python in pathlib modular

    Python in pathlib modular Path.cwd(): Returns the path to the current directory Path.home(): Return to the home directory of the current user Path.stat(): Return this path information Path.touch(): create a file P ...

  10. Python Middle bag / Modular `import` operation

    Copyright notice : Blog is original for the author , Allowed to reprint , But the original address must be indicated : https://www.cnblogs.com/byronxie/p/10745292.html It is illustrated by examples import The role of . establish ...

Random recommendation

  1. springboot+druid

    Recent projects need to build new projects , I plan to use the form of micro service to build for later development . I saw a circle and found springboot Easy to build , Simple configuration , Enhanced annotation function ,"just run". Spring Boot ma ...

  2. jsp Custom tab pagination

    First step : Create paging entities page class package com.soda.util; /** * @description Paging entity classes * @author line * @time 2016 year 8 month 28 Japan 11: ...

  3. Node.js&NPM Installation and configuration ( turn )

    Node.js Installation and configuration Node.js It has been born for more than two years , Because it has been in rapid development , Most of the past installation and configuration introductions are aimed at 0.4.x In terms of version , Not suitable for the latest 0.6.x Version of The situation , Regarding this , We will be in 0.6.x Of ...

  4. CLR via Notes with the book

    CTS(common type system) The generic type system specifies , A type can contain zero or more members , as follows :   Field (Field): Data variables that are part of the object state . Fields are distinguished by name and type   Method ( ...

  5. .2-Vue The source code starts (2)

    Let's continue with the first section (GoGoGo) In the last section mergeOptions The function is finished , The last one to return options Given to vm.$options. This section continues to run the code : function initMixin(V ...

  6. Customize jstl fn function fns

    1. Introduce function declaration : jsp The page needs to introduce customization fns Function declaration :<%@ taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld&q ...

  7. Socket asynchronous communication

    Recently, I used to write data communication ! Hopefully that helped /// <summary> /// Get or set the server IP Address /// </summary> public string se ...

  8. Appium Environment building - Lite version

    Appium Automated environment preparation Installation configuration JDK download Android SDK And configure environment variables Install the emulator or connect the real machine install appium desktop install python and pycharm ( Development language and development tools ) ...

  9. PowerShell Empire Use notes

    ## setup script git clone https://github.com/EmpireProject/Empire.git cd Empire cd setup sudo ./install.sh ## ...

  10. UITextField Property and method description

    / initialization textfield And set the position and size UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(, , , )] // Set up ...