First, the principle
The verification code is actually a random string. The principle can be divided into two kinds:
1. Simple Verification Code
Generated directly from ASCII codes of letters and numbers. This is the verification code used in this article.
2. Complex verification Code
Through a random string, a specified string (such as accesskey), and the current time for the generation of verification code, the period is also SHA1 encryption. such as NetEase cloud letter SMS Verification Code generator:
Checksumbuilder.java
PackageCom.ray.im.util;Importjava.security.MessageDigest;/**@desc: Verification Code generation Tool * *@author: Shirayner * @date: September 26, 2017 pm 4:28:15*/ Public classChecksumbuilder {//1. Calculate and obtain checksum Public Staticstring Getchecksum (String Appsecret, String nonce, String curtime) {returnEncode ("SHA1", Appsecret + nonce +curtime); } //2. Calculate and get MD5 values Public Staticstring getMD5 (String requestbody) {returnEncode ("MD5", Requestbody); } //3. Encrypt according to the encryption method Private Staticstring Encode (string algorithm, String value) {if(Value = =NULL) { return NULL; } Try{messagedigest messagedigest=messagedigest.getinstance (algorithm); Messagedigest.update (Value.getbytes ()); returnGetformattedtext (Messagedigest.digest ()); } Catch(Exception e) {Throw NewRuntimeException (e); } } Private StaticString Getformattedtext (byte[] bytes) { intLen =bytes.length; StringBuilder buf=NewStringBuilder (LEN * 2); for(intj = 0; J < Len; J + +) {buf.append (hex_digits[(bytes[j)>> 4) & 0x0f]); Buf.append (Hex_digits[bytes[j]& 0x0f]); } returnbuf.tostring (); } Private Static Final Char[] hex_digits = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' F ' };}
View Code
Two, code 1. checksumbuilder-Verification Code Generator
PackageCom.ray.sms.aliyun.util;ImportJava.util.Random;/**@desc: Captcha Tool class * *@author: Shirayner * @date: November 7, 2017 morning 10:07:46*/ Public classChecksumbuilder {/*** @desc: 1. Randomly generated string * *@paramLength of String *@paramtype types (0: numbers only; 2: characters only; other numbers: numbers and characters) *@return* String Random string*/ Public StaticString Getrandomstr (intLengthinttype) {String str= ""; intBeginchar = ' a '; intEndChar = ' Z '; //only Numbers if(Type = = 0) {Beginchar= ' Z ' + 1; EndChar= ' Z ' + 10; } //only lowercase letters Else if(Type = = 2) {Beginchar= ' A '; EndChar= ' Z '; } //There are numbers and letters Else{Beginchar= ' A '; EndChar= ' Z ' + 10; } //Generating Random classesRandom random =NewRandom (); for(inti = 0; i < length; i++){ intTMP = (Beginchar + random.nextint (EndChar-Beginchar)); //greater than ' Z ' is the number if(tmp > ' z ') {tmp= ' 0 ' + (tmp-' z '); } STR+= (Char) tmp; } returnstr; } /*** @desc: 2. Get 6 Digit Verification code * *@return* String 6-bit digital verification code*/ Public StaticString getchecksum () {returnGETRANDOMSTR (6, 0); } }
View Code
2.checksumbuildertest-Test class
PackageCom.ray.sms.aliyun.util;ImportOrg.apache.logging.log4j.LogManager;ImportOrg.apache.logging.log4j.Logger;Importorg.junit.Test;/**@desc: * *@author: Shirayner * @date: November 7, 2017 morning 9:50:33*/ Public classChecksumbuildertest {Private Static FinalLogger Logger = Logmanager.getlogger (checksumbuildertest.class); @Test Public voidtestgetchecksum () {intlen=100000; String CheckSum=NULL; for(inti=0;i<len;i++) {CheckSum=checksumbuilder.getchecksum (); Logger.info ("CheckSum:" +checkSum); } }}
View Code
Iii. references
1.http://bbs.csdn.net/topics/391946551
2. NetEase Cloud server-side API documentation (see API checksum Check this section )
Javautil_04_ Verification Code Generator