Sha algorithm: Signature string SHA algorithm Java language Reference (Shahelper.java)

Source: Internet
Author: User

Shahelper.java
 PackageCom.util;/**  * @authorWangxiangyu * @date: October 16, 2017 9:00:47 * class Description: Sha Signature Algorithm*/ Public classShahelper {Private Final StaticString[] hexdigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; /*** Convert byte array to 16 binary string * *@paramB-byte array *@return16 binary strings*/     Public StaticString bytearraytohexstring (byte[] b) {StringBuffer RESULTSB=NewStringBuffer ();  for(inti = 0; i < b.length; i++) {resultsb.append (bytetohexstring (b[i)); }        returnresultsb.tostring (); }    Private StaticString bytetohexstring (byteb) {intn =b; if(N < 0) n= 256 +N; intD1 = n/16; intD2 = n 16; returnHEXDIGITS[D1] +HEXDIGITS[D2]; }    /*** Sha Summary calculation (byte[]). *      * @paramsrc byte[] *@throwsException *@returnbyte[] + bit digest*/     Public Static byte[] Shadigest (byte[] src)throwsException {java.security.MessageDigest alg=java.security.MessageDigest. getinstance ("SHA"); returnalg.digest (SRC); }        /*** Sha Summary calculation (String). *      * @paramsrc String type of target String *@throwsException *@returnString*/     Public StaticString Shadigest (String src)throwsException {returnBytearraytohexstring (Shadigest (Src.getbytes ("UTF-8"))); }    /**Test Crypt*/     Public Static voidMain (string[] args) {Try {            //the plaintext data obtainedString serialnumber = "0101102010031723101273807f00179e"; String Reqbizargs= "<reqbizargs version= ' 1 ' ><test>test</test></ReqBizArgs>"; String Key= "8204CDBD76BB69DAE040007F010017A4"; String Desstr= serialnumber + Reqbizargs +key; System.out.println ("Original String desstr = '" + desstr + "'"); //Generate SignatureString signature =shadigest (DESSTR); System.out.println ("Signature String signature = '" + Signature + "'"); } Catch(Exception ex) {ex.printstacktrace (); }    }}

Sha algorithm: Signature string SHA algorithm Java language Reference (Shahelper.java)

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.