Share Python mds,sha256 encryption algorithm, C # corresponding SHA256 encryption algorithm

Source: Internet
Author: User
Tags hmac

"' 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

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.