Python built-in modules hashlib, hmac and uuid Usage Analysis, hashlibuuid

Source: Internet
Author: User
Tags hmac md5 hash

Python built-in modules hashlib, hmac and uuid Usage Analysis, hashlibuuid

This example describes the usage of the Python built-in modules hashlib, hmac, and uuid. We will share this with you for your reference. The details are as follows:

I. hashlib

The md5 and sha algorithms generate a fixed-length message digest using the message digest algorithm. The message digest algorithm is irreversible. However, the same message has the same value after the Digest algorithm. You can verify the data integrity by comparing the message digest.

The sha algorithm is safer than the MD5 algorithm, but it takes a little longer.

1. Original message Summary

import hashlib# ######## md5 ########hash = hashlib.md5()hash.update('admin')print hash.hexdigest()#21232f297a57a5a743894a0e4a801fc3# ######## sha1 ########hash = hashlib.sha1()hash.update('admin')print hash.hexdigest(),len(hash.hexdigest())# d033e22ae348aeb5660fc2140aec35850c4da997 40# ######## sha256 ########hash = hashlib.sha256()hash.update('admin')print hash.hexdigest(),len(hash.hexdigest())# 8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918 64# ######## sha384 ########hash = hashlib.sha384()hash.update('admin')print hash.hexdigest(),len(hash.hexdigest())# 9ca694a90285c034432c9550421b7b9dbd5c0f4b6673f05f6dbce58052ba20e4248041956ee8c9a2ec9f10290cdc0782 96# ######## sha512 ########hash = hashlib.sha512()hash.update('admin')print hash.hexdigest(),len(hash.hexdigest())# c7ad44cbad762a5da0a452f9e854fdc1e0e7a52a38015f23f3eab1d80b931dd472634dfac71cd34ebc35d16ab7fb8a90c81f975113d6c7538dc69dd8de9077ec 128

2. Summary after adding salt (salt)

Because the digest obtained by the same message algorithm is the same, you can obtain the original message value by hitting the database. The solution is to add a salt and splice the original message before calculation.

Import hashlib ######### md5 ######## hash = hashlib. md5 ('898oafs09f') hash. update ('admin') print hash. hexdigest () # The above is equivalent to hash = hashlib. md5 () hash. update ('898oafs09fadmin') print hash. hexdigest ()

Ii. HMAC

HAMC internally processes and encrypts the key and content we create, which is quite secure. It is generally used to respond to challenges.

import hashlibimport hmach = hmac.new('keysstring')h.update('hello')print h.hexdigest(),len(h.hexdigest())# 2ca7ac50a9bca542e58e0baad15f8383 32

Iii. UUID

UUID is a 128-bit globally unique identifier, usually represented by a 32-byte string. It guarantees the uniqueness of time and space. In python, it is called UUID, and other languages may be called GUID.

It ensures the uniqueness of the generated ID through MAC address, timestamp, namespace, random number, and pseudo-random number.

UUID consists of five algorithms:

1. uuid1 () -- Based on the timestamp. Generated by MAC address, current timestamp, and random number. Global uniqueness can be guaranteed. However, MAC security issues are also caused by the use of MAC. IP addresses can be used in LAN to replace MAC.
2. uuid2 ()-based on distributed computing. The environment DCE (this function is not available in Python) algorithm is the same as uuid1. The difference is that the first four positions of the timestamp are converted into the posix uid. This method is rarely used in practice.
3. uuid3 () -- name-based MD5 hash value. By calculating the MD5 Hash Value of the name and namespace, the uniqueness of different names in the same namespace and those of different namespaces are ensured, however, the same name in the same namespace generates the same uuid.
4. uuid4 ()-based on random numbers. A pseudo-random number has a certain recurrence probability, which can be calculated.
5. uuid5 ()-name-based SHA-1 hash value. The algorithm is the same as that of uuid3, but the SHA-1 algorithm is used.

The uuid2 algorithm is not implemented in python.

import uuidprint uuid.uuid1()print uuid.uuid3(uuid.NAMESPACE_DNS, 'testme')print uuid.uuid4()print uuid.uuid5(uuid.NAMESPACE_DNS, 'testme')# 1a52b39e-a197-11e6-b5c6-8056f2d4c814# 7a67f5d4-50fd-36f7-bbeb-1c739ab40b8c# 0c0cc4f0-6ad0-40d3-a796-119dcfaddf60# dac48d1f-a443-578c-8754-856842a2f98d

PS: if you are interested in encryption and decryption, refer to the online tools on this site:

Online text encryption and decryption tools (including AES, DES, and RC4 ):
Http://tools.jb51.net/password/txt_encode

MD5 online encryption tool:
Http://tools.jb51.net/password/CreateMD5Password

Online hash/hash algorithm encryption tool:
Http://tools.jb51.net/password/hash_encrypt

Online MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160 encryption tools:
Http://tools.jb51.net/password/hash_md5_sha

Online sha1/shaloud/sha256/sha384/sha512 encryption tool:
Http://tools.jb51.net/password/sha_encode

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.