SHA-256 and SHA-512 encryption algorithms

Source: Internet
Author: User
Tags stringbuffer

This article is reproduced from: Portal, theft code shameful, we are the code of the porter, only handling, not stolen code

Package cc.restyle.util;
Import Java.security.MessageDigest;

Import java.security.NoSuchAlgorithmException; /** * SHA-512 Encryption * */public class SHA512 {/** incoming text content, return SHA-256 String */public static string encry256 (final Strin
    G StrText) {return SHA (StrText, "SHA-256"); /** incoming text content, return SHA-512 String */public static string encry512 (final String strText) {return Sha (StrText, "Sha
    -512 ");
        }/** String sha encryption */private static string Sha (Final String strtext,final string strtype) {//return value

        String Strresult=null;
            Whether it is a valid string if (StrText! = null && strtext.length () >0) {///encrypt start, create encrypted object, and pass in the encryption type
                try {messagedigest messagedigest = messagedigest.getinstance (strtype);
                Incoming encrypted string messagedigest.update (Strtext.getbytes ());
                Get bytes type result byte[] Bytebuffer = Messagedigest.digest (); Convert byte to String
                StringBuffer strhexstring = new StringBuffer (); 
                    for (int i =0;i<bytebuffer.length;i++) {String hex = integer.tohexstring (0xFF & Bytebuffer[i]);
                    if (Hex.length () ==1) {strhexstring.append (' 0 ');
                } strhexstring.append (hex);


            }//Get the result returned strresult = Strhexstring.tostring ();
            } catch (NoSuchAlgorithmException e) {e.printstacktrace ();
    }} return strresult;
 }
}

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.