python hash hash value

Knowledge point increment
1 hash
of what is available hash (hashable)?
Briefly hashable data type, i.e., immutable data structure (String str, tuple tuple, object set objects).
  Hash so what effect?
It will generally be an amount of data is a process of converting data of a small, or even just a number, so that we can use a fixed time at which the query complexity, therefore, is very important for efficient hashing algorithms and data structures.
What is not hashed (unhashable)?
Similarly, not the hash data types can change the data structure (dict dictionary, list List, set SET)
the hash (Object)
hash value hash () takes an object for acquiring (string or value, etc.) . Returns a hash value of the object.

Hash algorithms (including python achieve)

  1. Introduction
    hash (hash) also translated as hash. Hash algorithm, is input to a variable length, converted into a fixed length output by the hash function, i.e. the hash value.
    This hashing is a one-way operation, i.e., not having irreversible restore the input information from the hash value, and therefore strictly speaking Hash algorithm is a message digest algorithm, not an encryption algorithm. Common hash algorithm: SM3, MD5, SHA-1 and so on.
  2. Application
    Hash mainly used in data structures, and the field of cryptography.
    Under different scenarios, select the hash function will be focused. For example, in management data structure, the main consideration of fast operation, and to ensure a uniform distribution of hash; the priority application must collision resistance in cryptography, to avoid the same two different plaintext hash value occurs.
    2.1 Application in the field of cryptography
    In cryptography, the role Hash algorithm is mainly used for message digest and signature, in other words, it is mainly used to verify the integrity of the entire message. For example, some go to the website and not directly stored in clear text user password is stored digest (hash value) processed through the hash password when a user logs only if the same comparison summary abstracts and database storage of input plaintext; even hacking or password to access the database maintenance personnel can not get the user's plain text, greatly improving security.
    2.2 application data structure
    using Hash algorithm data structure called a hash table, also known as hash table, mainly to improve the efficiency of queries. To access the records by key values are mapped to table a position to speed up the search. The mapping function is a hash function, array record store called hash table. When applied in a data structure, sometimes it requires a high operation speed and weaken consider collision resistance, you can build your own using a hash function.
  3. Python Hash algorithm to achieve
    3.1 custom hash function
    from a hash function defined medium typically Method In addition to leaving the remainder, shifted, hash, may be utilized to take the square. The following example is a hash function for my own definition, the use of modulo arithmetic and XOR.
  4. coding:utf-8

  5. Custom hash function

  6. def my_hash(x):
  7.  return (x % 7) ^ 2
  8. print (my_hash (1)) # output: 3
  9. print (my_hash (2)) # output: 0
  10. # Output print ((3) my_hash): 1
  11. print (my_hash (4)) # outputs:. 6
    3.2 hash () function
    has a built-in python hash function hash (), returns an object (number, string, can not be directly used to list, set, dictionary) of hash value. Sample code is as follows:
  12. coding:utf-8

  13. hash()

  14. print(hash(1))
  15. print (hash (1.0)) # same value, different types of hash values ​​are the same
  16. print(hash("abc"))
  17. print (hash ( "hello world" ))
    at runtime discovered a phenomenon: the same hash value string in one run at the same time is the same, but different hash values of different run times. This is due to the time Python randomly generated secret prefix string hash algorithm has a start / suffix mechanism, there is a random phenomenon: for the same input string, hash results obtained by different processes interpreter may be different. So when you need to make a reproducible process consistency across the hash, you need to use hashlib module.
    3.3 hashlib module
    hashlib provides a common digest algorithm, such as MD5, SHA1, and so on. Sample code is as follows:
  18. coding:utf-8

  19. Use hashlib module

  20. import hashlib
  21. md5 = hashlib.md5 () # use the MD5 algorithm
  22. data = "hello world"

  23. md5.update(data.encode('utf-8'))
  24. print(md5.hexdigest())

Variable types of data can not hash, such as list, dictionary: same values for different sites, different values for the same address
lists, dictionaries variable,

Numerical, alphabetical, strings, numbers, immutable tuples: the same values ​​for the same address, different address values ​​different

Determine how the variable immutable? Summary: to see a change of value is not the same id, id like a variable, you can not hash,
changed values, id change, was immutable, you can hash
Hash concept: Hash, generally translated to do "bulk column ", also has a direct transliteration of" hash ", that is, the arbitrary length input (also called pre-mapping, pre-image), through a hash algorithm, converted into a fixed-length output , the output is the hash value. This conversion is a compression map, i.e., the space hash value is typically much smaller than the input space, different inputs may hash to the same output, it is impossible to determine unique hash value from the input value. Simply means that A message of arbitrary length is compressed to a fixed length message digest function.
Hash algorithm: hashing algorithm is an algorithm used to solve a correspondence relationship between the data and the data. Its originally intended speed up data access.
Most search algorithm in the computer field are correlated with the size of stored data, a positive correlation, a measure of the efficiency of the algorithm to find the amount of what we call the average search length, under normal circumstances, the average length of the more outstanding search algorithm will not be shorter than logarithm scale data base 2 of ().

Guess you like

Origin www.cnblogs.com/abdm-989/p/11329122.html