SHA1 Signature Tool Class Java

Source: Internet
Author: User

 PackageCom.net.util;Importjava.security.MessageDigest;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Random;ImportJava.util.Set;ImportJava.util.SortedMap;/*' ============================================================================ ' API description: ' Createsha1sign Create Signature SHA1 ' GETSHA1 () Sha1 signature ' ======================================================================= ===== '*/ Public classSha1util { Public StaticString getnoncestr () {random random=NewRandom (); returnMd5util.md5encode (string.valueof (Random.nextint (10000)), "UTF-8"); }     Public StaticString Gettimestamp () {returnString.valueof (System.currenttimemillis ()/1000); }       //Create a signature SHA1     Public StaticString createsha1sign (sortedmap<string, string> signparams)throwsException {stringbuffer sb=NewStringBuffer (); Set es=Signparams.entryset (); Iterator it=Es.iterator ();  while(It.hasnext ()) {Map.entry Entry=(Map.entry) it.next (); String k=(String) Entry.getkey (); String v=(String) entry.getvalue (); Sb.append (k+ "=" + V + "&"); //to use the original value of Urlencoder! } String params= sb.substring (0, Sb.lastindexof ("&"));//System.out.println ("before SHA1:" + params);//System.out.println ("SHA1 signature is:" +GETSHA1 (params));        returnGETSHA1 (params); }    //SHA1 Signature     Public Staticstring GetSha1 (String str) {if(str = =NULL|| Str.length () = = 0) {            return NULL; }        CharHexdigits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ',                ' A ', ' B ', ' C ', ' d ', ' e ', ' F ' }; Try{messagedigest mdtemp= Messagedigest.getinstance ("SHA1"); Mdtemp.update (Str.getbytes ("UTF-8")); byte[] MD =mdtemp.digest (); intj =md.length; CharBuf[] =New Char[J * 2]; intK = 0;  for(inti = 0; I < J; i++) {                byteBYTE0 =Md[i]; Buf[k+ +] = hexdigits[byte0 >>> 4 & 0xf]; Buf[k+ +] = hexdigits[byte0 & 0xf]; }            return NewString (BUF); } Catch(Exception e) {return NULL; }    }}

SHA1 Signature Tool Class 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.