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)