package com.tq.jjb.common.util;
import java.io.IOException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import net.sf.json.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class SMSCodeUtil {
private static final String APP_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//网易云信分配的账号
private static final String APP_SECRET="xxxxxxxxxxxx";//网易云信分配的密钥
private static final String NONCE="000000";//随机数
private static final String SENDMSG_URL="https://api.netease.im/sms/sendcode.action";//发送验证码的请求路径URL
private static final String CHECKCODE_URL="https://api.netease.im/sms/verifycode.action";//校验验证码的请求路径URL
private static final char[] HEX_DIGITS={‘0‘,‘1‘,‘2‘,‘3‘,‘4‘,‘5‘,‘6‘,‘7‘,‘8‘,‘9‘,‘a‘,‘b‘,‘c‘,‘d‘,‘e‘,‘f‘};
private static final String ALGORITHM="SHA";
//计算并获取验证码
public static String getVerificationCode(String appSecret,String nonce,String curTime){
String param = appSecret+nonce+curTime;
String verificationCode = "";
try {
MessageDigest messageDigest=MessageDigest.getInstance(ALGORITHM);
messageDigest.update(param.getBytes());
byte[] bytes = messageDigest.digest();
int len=bytes.length;
StringBuilder sb=new StringBuilder(len*2);
for(int i=0;i<len;i++){
sb.append(HEX_DIGITS[(bytes[i]>>4)&0x0f]);
sb.append(HEX_DIGITS[bytes[i]&0x0f]);
}
verificationCode = sb.toString();
return verificationCode;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
//发送短信获取生成的验证码
public static String sendMessage(String phone) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost post = new HttpPost(SENDMSG_URL);
String curTime=String.valueOf((new Date().getTime()/1000L));
String checkSum=SMSCodeUtil.getVerificationCode(APP_SECRET,NONCE,curTime);
//设置请求的header
post.addHeader("AppKey",APP_KEY);
post.addHeader("Nonce",NONCE);
post.addHeader("CurTime",curTime);
post.addHeader("CheckSum",checkSum);
post.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
//设置请求参数
List<BasicNameValuePair> nameValuePairs =new ArrayList<BasicNameValuePair>();
BasicNameValuePair basicNameValuePair = new BasicNameValuePair("mobile",phone);
nameValuePairs.add(basicNameValuePair);
post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
//执行请求
HttpResponse response=httpclient.execute(post);
String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8");
//判断是否发送成功,发送成功返回true
String code = JSONObject.fromObject(responseEntity).getString("code");
if (code.equals("200")){
return "success";
}
return "error";
}
//验证短信验证码是否匹配
public static String checkVerificationCode(String phone,String verificationCode) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost post = new HttpPost(CHECKCODE_URL);
String curTime=String.valueOf((new Date().getTime()/1000L));
String checkSum=SMSCodeUtil.getVerificationCode(APP_SECRET,NONCE,curTime);
//设置请求的header
post.addHeader("AppKey",APP_KEY);
post.addHeader("Nonce",NONCE);
post.addHeader("CurTime",curTime);
post.addHeader("CheckSum",checkSum);
post.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8");
//设置请求参数
List<BasicNameValuePair> nameValuePairs =new ArrayList<BasicNameValuePair>();
BasicNameValuePair mobilephone = new BasicNameValuePair("mobile",phone);
nameValuePairs.add(mobilephone);
BasicNameValuePair codeverificationCode = new BasicNameValuePair("code",verificationCode);
nameValuePairs.add(codeverificationCode);
post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
//执行请求
HttpResponse response=httpclient.execute(post);
String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8");
//判断是否发送成功,发送成功返回true
String code = JSONObject.fromObject(responseEntity).getString("code");
if (code.equals("200")){
return "success";
}
return "error";
}
public static void main(String[] args) throws IOException {
// //测试getVerificationCode()方法
// String appSecret="xxxxxxxxxxxx";//网易云信分配的密钥
// String nonce="000000";//随机数
// String curTime=String.valueOf((new Date().getTime()/1000L));
// String aa = MobileMSGUtil.getVerificationCode(appSecret, nonce, curTime);
// System.out.println(aa);
// //测试sendMessage()方法
// String phone = "00000000000";
// String code = MobileMSGUtil.sendMessage(phone);
// System.out.println("短信验证码获取的状态"+code);
// //测试checkVerificationCode()方法
// String phone = "00000000000";
// String verificationCode = "0000";
// String code = MobileMSGUtil.checkVerificationCode(phone,verificationCode);
// System.out.println("短信验证码匹配的状态"+code);
}
}