Token check in Java

Source: Internet
Author: User
Tags array to string sha1 sha1 encryption

After the developer submits the information, the server sends a GET request to the filled server address URL, and the GET request carries four parameters:

Parameters Describe
Signature The cryptographic signature, signature, combines the token parameters and the timestamp parameters and Nonce parameters that the developer fills in.
Timestamp Time stamp
Nonce Random number
Echostr Immediately after the string

The developer verifies the request by verifying the signature (there is a check method below). If you confirm that the GET request is from the server, return the ECHOSTR parameter content as it is, and the access will be effective, or the access fails.

The encryption/verification process is as follows:

1. Three parameters of token, timestamp and nonce are sorted in dictionary order 2. The three parameter strings are stitched together into a string for SHA1 encryption 3. The developer obtains the encrypted string that can be compared to signature, which identifies the request from

Java code:

public class Signutil {private static String token = "Weixin";         public static Boolean Checksignature (string signature, string timestamp, String nonce) {Boolean result = false;        Sort tokens, timestamp and nonce by dictionary order string[] array = new String[]{token, timestamp, nonce};         Arrays.sort (array);         Concatenation of three parameter characters into a string str = Array[0].concat (array[1]). Concat (array[2]);        String sha1str = null;            try {//SHA1 encryption of the stitched string messagedigest MD = messagedigest.getinstance ("SHA-1");            Byte[] Digest = Md.digest (Str.getbytes ());        SHA1STR = Byte2str (digest); } catch (Exception e) {} if (sha1str! = null && sha1str.equals (signature)) {ResU        Lt = true;    } return result; }/* * Convert byte array to String */public static string Byte2str (byte[] array) {StringBuffer hexstr = new string        Buffer ();        String shahex= ""; for (int i = 0;i < Array.Length;            i++) {Shahex = Integer.tohexstring (Array[i] & 0xFF);            if (Shahex.length () < 2) {hexstr.append (0);        } hexstr.append (Shahex);    } return hexstr.tostring (); }}


Token check in Java

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.