"' introduced Hmac,hashlib encryption module ' ' Import hmacimport hashlibdef jm_sha256 (key, value): ' sha256 encrypted return: The results of the encryption are converted into a 16-string format and capitalized ' hsobj = hashlib.sha256 (Key.encode ("Utf-8")) Hsobj.update (Value.encode ("Utf-8 ")) return Hsobj.hexdigest (). Upper () def jm_md5 (key, value): ' MD5 encrypted return: encrypted result converted to 16 binary string and capitalized "' hsobj = hashlib.md5 (Key.encode (" Utf-8 ")) Hsobj.update (Value.encode (" Utf-8 ")) return Hsobj.hexdigest (). Upper () def hmac_sha256 (key, value): ' hmacsha256 encrypted return: encrypted result converted to 16 binary string and capitalized ' message = Value.encode (' utf-8 ') return hmac.new (Key.encode (' utf-8 '), message, digestmod= hashlib.sha256). Hexdigest (). Upper () def hmac_md5 (key, value): ' hmacmd5 encrypted return: encrypted result converted to 16 binary string form , and uppercase ' message = Value.encode (' utf-8 ') return hmac.new (Key.encode (' utf-8 '), message, digestmod= HASHLIB.MD5). Hexdigest (). Upper ()
Where sha256 corresponds to the C # encryption method as follows
<summary>//SHA256 encryption///</summary>/<param name= "strkey" > Encryption key </param& Gt <param name= "Strdata" > What to encrypt </param>//<returns></returns> public string Getsh A256hashfromstring (String strkey,string strdata) {byte[] Bytvalue = System.Text.Encoding.UTF8.GetBytes ( Strkey+strdata); try {SHA256 sha256 = new Sha256cryptoserviceprovider (); byte[] RetVal = Sha256.computehash (Bytvalue); StringBuilder sb = new StringBuilder (); for (int i = 0; i < retval.length; i++) {sb. Append (Retval[i]. ToString ("X2")); } return SB. ToString (); } catch (Exception ex) {throw new Exception ("Getsha256hashfromstring () Fail,error:" + ex. Message); }}///<summary>/HMACSHA256 encryption//</summary>//<param name= "secret" > Encryption key </param>//<param name = "message" > Content to be encrypted </param>//<returns></returns> public string Createtoken (string SECR Et,string message) {var encoding = System.Text.Encoding.UTF8; byte[] Keybyte = encoding. GetBytes (secret); byte[] messagebytes = encoding. GetBytes (message); using (var hmacsha256 = new HMACSHA256 (keybyte)) {byte[] hashmessage = hmacsha256. ComputeHash (messagebytes); StringBuilder sb = new StringBuilder (); for (int i = 0; i < Hashmessage. Length; i++) {sb. Append (Hashmessage[i]. ToString ("X2")); } return SB. ToString (); } }
Share Python mds,sha256 encryption algorithm, C # corresponding SHA256 encryption algorithm