Python and encryption

Source: Internet
Author: User
Tags hmac sha1

Instructions for using Python and Hashlib modules:

used for encryption related operations, in the 3.x instead of the MD5 module and SHA module, mainly provide SHA1, SHA224, SHA256, SHA384, SHA512, MD5 algorithm; that is, in the 3.x version, only HASHL The IB module is not present in the MD5 module and the SHA module .

One, the module uses the instruction:

1, hashlib

ImportHASHLIBM=hashlib.md5 () #md5的方法使用m. Update (b"Hello")Print(M.hexdigest ()) m.update (b"My name is xkj")Print(M.hexdigest ()) M2=hashlib.md5 () m2.update (b"Hello my name is xkj")Print(M2.hexdigest ())#hexadecimalPrint(M2.digest ())# BinaryHash=hashlib.sha1 ()#SHA1 method uses hash.update (b"Admin")Print(Hash.hexdigest ()) hash=hashlib.sha256 ()#sha256 method uses  hash.update (b"Admin")Print(Hash.hexdigest ()) hash=hashlib.sha512 ()#sha512 method uses  hash.update (b"Admin")Print(Hash.hexdigest ())

2. Usage instructions for HMAC templates

Hash message authentication code, or HMAC, is an authentication mechanism based on the message identification Code (authentication code) of Mac. When using HMAC, both sides of the message communication authenticate the authenticity of the message by verifying the authentication key K added in the message;

Generally used in network communication message encryption, the premise is that both parties must first agree on a good key, like a connector password, and then send the message with key to encrypt the message, the receiver with key + message plaintext again encrypted, take the encrypted value with the sender's relative ratio is equal, so that the authenticity of the message can be verified, And the legitimacy of the sender.

Import   HMAC# is used for message authentication instead of password h=hmac.new ('  add up '. Encode ("  utf-8"),'  steady '. Encode ("utf-8 " ) print("HMAC:", H.hexdigest ())

3. function Extension Data

Read more about md5,sha1,sha256 and other introductory articles here https://www.tbs-certificates.co.uk/FAQ/en/sha256.html

Python and encryption

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.