Fixed-length random number generator

Source: Internet
Author: User

Public String Generatetoken () throws Exception {    String token = system.currenttimemillis () + new Random (). Nextint () + "";    MessageDigest MD = new MessageDigest ("MD5");//Can get digest fingerprint of data, length consistent    byte[] MD5 = Md.digest (Token.getbytes ());// The length is uniformly 16 bytes    Base64encoder encoder = new Base64encoder ();//You can convert any byte array to a normal string    return Encoder.encode (MD5);}

Base64encoder is not found in the API documentation for JSE, it is not an API that is officially released by Sun, and can be viewed after use in eclipse

The Base64encoder encoding automatically converts 3 bytes, or 24 bits of binary code, into 4 bytes or 32 bits of binary code, it divides the original 24 bits by 6 bits per copy, and then the 4 bits in front of each copy 2, so the data range for each byte generated by the encoding is: 0- 00111111 is 0 to 63, a total of 64 data, each corresponding to a unique data information.


Can be used to prevent a form from repeating a commit:
When a user accesses a page, the server randomly generates an identifier token that is saved to the session and is written back to the form as a hidden element, and the token attribute in the session is deleted after the form is processed
After the user submits the form, the server authenticates:
1, whether there is a token element, there is the next step, do not process the form
2, if there is a token attribute in the session, proceed to the next step, do not process the form
3, the token attribute value in the session is the same as the user commits, the next step is the same, the form is not processed differently
4, process the form, and remove the token attribute from the session

This is the server side to prevent the form of repeated submission method, the client to be controlled by the JS code.



Fixed-length random number generator

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.