Without accumulating steps or even thousands of miles; without accumulating small streams, why.
Learn a python module every day. You can learn about 30 modules per month. You can learn about 30 modules per year ......
Today I saw a hashlib and HMAC module in Python. These two modules are used for hash encryption. When talking about encryption, you must first understand the basic knowledge of encryption: Hash encryption, symmetric encryption, asymmetric encryption, digital signature, and so on. Relevant knowledge can be found on msdn: http://msdn.microsoft.com/zh-cn/library/92f9ye3s.aspx
Using hashlib in Python for hash encryption is not simple. The following is a simple code:
Import hashlib <br/> MD5 = hashlib. MD5 () # create an MD5 encryption object <br/> md5.update ("jgood is a handsome boy") # update the data to be encrypted <br/> Print md5.digest () # encrypted result (Binary) <br/> Print md5.hexdigest () # The encrypted result is represented by a hexadecimal string. <Br/> Print 'block _ SIZE: ', md5.block _ size <br/> Print 'digest _ SIZE:', md5.digest _ size
It is very simple. In fact, if we encrypt a string, we don't need to write so much code above, just a statement:
Print '-' * 25, 'more concise sync', '-' * 25 <br/> Print hashlib. new ("MD5", "jgood is a handsome boy "). hexdigest ()
The hashlib module also supports other hash encryption algorithms, such as sha1 and shares'. To learn more, check the python manual.
Python hash vs. Net hash
I was born from. net programmers, who have previously written some. the hash Encryption Class under. net, in. net can use the following code to implement the MD5 encryption algorithm, which is not very difficult:
/// <Summary> <br/> // according to the specified encryption algorithm, encrypt a string <br/> /// </Summary> <br/> /// <Param name = "hashname"> encryption algorithm name </param> <br/> /// <Param name = "data"> data to be encrypted </param> <br/> // <returns> encrypted data </returns> <br/> Private Static string encrypt (string hashname, string data) <br/>{< br/> byte [] btdata = system. text. encoding. ASCII. getbytes (data); <br/> // create an instance of the hashalgorithm derived class <br/> hashalgorithm hasher = hashalgorithm. create (hashname); <br/> // use Hash encryption <br/> byte [] hasheddata = hasher. computehash (btdata); <br/> stringbuilder result = new stringbuilder (); <br/> foreach (byte B in hasheddata) <br/>{< br/> result. append (B. tostring ("X2"); // convert to a hexadecimal string to save <br/>}< br/> return result. tostring (); <br/>}
The code of the common hash encryption algorithm compiled with. NET is attached:
/// <Summary> <br/> // hashencryptor class: implement various hash encryption algorithms <br/> /// </Summary> <br/> /// <example> <br/> /// use MD5 Encryption <br/> // string data = "jgood "; <br/> // string encrypteddata = hashencryptor. MD5 (data ); <br/> /// </example> <br/> /// <remarks> <br/> // the encryption algorithm provided by hashencryptor is irreversible. <Br/> /// </remarks> <br/> Public sealed class hashencryptor <br/>{< br/> /// <summary> <br/> // /private constructor, this class cannot be instantiated <br/> /// </Summary> <br/> private hashencryptor () <br/>{< br/>}< br/> // <summary> <br/> // use the MD5 encryption algorithm to encrypt the string <br/> /// </Summary> <br/> /// <Param name = "data"> string to be encrypted </param> <br/> /// <returns> string </returns> <br/> Public static string MD5 (string data) <br/>{< br/> return encrypt ("MD5", data ); <br/>}< br/> /// <summary> <br/> // use the sha1 encryption algorithm to encrypt the string <br/> /// </Summary> <br/> // <Param name = "data"> string to be encrypted </param> <br/> // <returns> encrypted string </returns> <br/> Public static string sha1 (string data) <br/>{< br/> return encrypt ("sha1", data ); <br/>}< br/> /// <summary> <br/> // use the SHA 256-bit encryption algorithm to encrypt the string <br/> /// </Summary> <br/> /// <Param name = "data"> string to be encrypted </param> <br/> /// <returns> encrypted string </ returns> <br/> Public static string sha256 (string data) <br/>{< br/> return encrypt ("sha256", data ); <br/>}< br/> /// <summary> <br/> // use the Sha 384-bit encryption algorithm to encrypt the string <br/> /// </Summary> <br/> /// <Param name = "data"> string to be encrypted </param> <br/> /// <returns> encrypted string </ returns> <br/> Public static string sha384 (string data) <br/>{< br/> return encrypt ("sha384", data ); <br/>}< br/> /// <summary> <br/> // use the Sha 512-bit encryption algorithm to encrypt the string <br/> /// </Summary> <br/> /// <Param name = "data"> string to be encrypted </param> <br/> /// <returns> encrypted string </ returns> <br/> Public static string sha512 (string data) <br/>{< br/> return encrypt ("sha512", data ); <br/>}< br/> /// <summary> <br/> // according to the specified encryption algorithm, encrypt a string <br/> /// </Summary> <br/> /// <Param name = "hashname"> encryption algorithm name </param> <br/> /// <Param name = "data"> data to be encrypted </param> <br/> // <returns> encrypted data </returns> <br/> Private Static string encrypt (string hashname, string data) <br/>{< br/> byte [] btdata = system. text. encoding. ASCII. getbytes (data); <br/> // create an instance of the hashalgorithm derived class <br/> hashalgorithm hasher = hashalgorithm. create (hashname); <br/> // use Hash encryption <br/> byte [] hasheddata = hasher. computehash (btdata); <br/> stringbuilder result = new stringbuilder (); <br/> foreach (byte B in hasheddata) <br/>{< br/> result. append (B. tostring ("X2"); // convert to hexadecimal format to save <br/>}< br/> return result. tostring (); <br/>}< br/>}