28, object-oriented advanced, digest algorithm

Source: Internet
Author: User

First, object-oriented advanced

1, __getitem__: the instance passes a parameter, pass it here, return the value, return what value oneself can define.

classFoo:def __init__(self,name,sex): Self.name=name Self.sex=Sexdef __getitem__(self, item):return 'Nishishabi'             #custom-returned valuesF=foo ('Egg','male') Print(f['a'])

Normally, the code that should return the correct value of the parameter's object should look like this:

class Foo:      def __init__ (self,name,age,sex):          = name              = age = Sex def __getitem__ (self, item          ):         if  hasattr (self,item):             return self.  __dict__[item]

2, __setitem__: Set or assign a value for the class, the growth should be the __dict__ to increase the value of the property, (can also be set to other chaotic), mainly in the dictionary operation of the object will call this built-in function.

classFoo:def __init__(self,name,sex): Self.name=name Self.sex=Sexdef __setitem__(self, Key, value): Self.__dict__[Key]=value#this should be the normal function of it, set into the other also lineF=foo ('Egg','male') f['Hobby']='Apple'     #this built-in function is called when a dictionary class operation is performedPrint(F.__dict__)

3, __delitem__: Perform a dictionary similar to delete operations, the default execution of the built-in function (if you do not want him to have the role it should have, can also be set to other)

classFoo:def __init__(self,name,sex): Self.name=name Self.sex=Sexdef __setitem__(self, Key, value): Self.__dict__[key]=valuedef __delitem__(Self, key):#The dictionary class deletes the operation when it executes to it, which is what it should do, set it to the other line        delSelf.__dict__[Key]f=foo ('Egg','male') f['Hobby']='Apple'delf['name']Print(F.__dict__)

This is done with other custom actions, and does not perform the delete operation that he should have.

classFoo:def __init__(self,name,sex): Self.name=name Self.sex=Sexdef __setitem__(self, Key, value): Self.__dict__[key]=valuedef __delitem__(self, key):Print('Nishishabi')#in hereF=foo ('Egg','male') f['Hobby']='Apple'Print(F.__dict__)delf['name']Print(F.__dict__)

In summary: The item series is the built-in function that is provided when performing a dictionary-like operation. Abstract algorithm: Cryptographic hashlib: A module that provides a digest algorithm
import hashlib   #  module that provides the digest algorithm MD5 = hashlib.md5 () md5.update (b'  123456')print(md5.hexdigest ())#  AEE949757A2E698417463D47ACAC93DF

Note: No matter how different the algorithm, the function of summary is always the same;

For the same string using the same algorithm for the digest algorithm, the resulting value is always the same;

Using different algorithms to digest the same string, the resulting values should be different;

No matter what algorithm is used, the way of hashlib is always the same;

SHA: A kind of abstract algorithm, with the increase of the degree of complexity of the algorithm, the time and space occupied by the algorithm increase

The usefulness of the abstract algorithm:

1, password ciphertext storage, the user can then enter the Password digest algorithm after the comparison, the password verification operation.

2. Consistency verification of files

1) Check that the files we downloaded and the files on the remote server are consistent at the time of download

2) The files on both machines, you want to check if you know the same

Add Salt: Security upgrade to digest algorithm

Dynamic Add Salt: The object is variable when salt is added.

Import hashlibmm=hashlib.md5 (bytes ('salt', encoding='utf-8' ) ) mm.update (b'wangyuxing') ret=mm.hexdigest ()  Print(ret)

Add Salt:

Import= hashlib.md5 (bytes (' salt ', encoding='utf-8' ) # +b")# Add salt step MD5 = hashlib.md5 () md5.update (b'123456  ')print(Md5.hexdigest ())

28, object-oriented advanced, digest algorithm

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.