package com.offcn.api.nwn.md5;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import com.offcn.process.NWN;
public class nwngetsign {
		
    public static String nwnGetSign (String parameter) {
    	StringBuffer sortstring=new StringBuffer();
    	 	//方法1
    	    	
    	//parameter = "appid=jiaowu&template_id=[473,475]&timestamp="+NWN.timestamp;
    
    	String arr[]=parameter.split("&");
  		Arrays.sort(arr);
  		for (int i = 0; i < arr.length; i++) {
  			
  			//System.out.println(arr[i].substring(arr[i].indexOf("=")+1));
  	  		arr[i]=arr[i].substring(arr[i].indexOf("=")+1);
  	  		
  	  		sortstring.append(arr[i]);
  	  		//System.out.println(sortstring);
					
		}
  		sortstring.toString();
  	//	System.out.println("sortstring====" + sortstring);
  		String str = sortstring.toString();
  		
  		  		
  		str = str + "bf2h3%^j?ljkj3706kji88697"; //教务的加密，规则固定需要加 
  		//System.out.println("getStr===" + str);
  		NWN.suprise_str = str;
    	//String str1 = "appid=tiku&avatar=http://thirdqq.qlogo.cn/qqapp/1106773681/5B50BBF83A00DC46B16B708F720A5D46/100&device_id=861837034477409&login_type=qq&nickname=hobo&open_id=1&platform=Android&version=1"+"&123456"; 
    String res = new LMFMD5().MD5(str);
    System.out.println("getRes===" +res);
   
    return res;
  
    
    }
    
    public static String nwnPostSign (String parameter) {
    	
       StringBuffer stringBuffer = new StringBuffer();
		
		String arr[] = parameter.split(",");
		Arrays.sort(arr);
		for (int i = 0; i < arr.length; i++) {
			
			arr[i] = arr[i].substring(arr[i].indexOf(":")+1);
			stringBuffer.append(arr[i]);
		}
		stringBuffer.toString();
//		System.out.println(stringBuffer);
		// 正则去掉说有特殊字符，慎用：     "[^0-9a-zA-Z\u4e00-\u9fa5.，,。？“”]+"  
		//正则去掉双引号  "\\u0022"
		
		String str = stringBuffer.toString().replaceAll("\\\"", "");
  		
  		str = str + "bf2h3%^j?ljkj3706kji88697"; //教务的加密，规则固定需要加 
  		
        System.out.println("postStr===" + str);
  		NWN.suprise_str = str;
  		
  		String res = new LMFMD5().MD5(str);
  	    System.out.println("strRes===" +res);
    	return res;
    }
    
    public static void  getstr(){
    String str="access_token=7BBFBDAE62CA6B9EC0F4B4E810F1C38C&expires_in=7776000&refresh_token=579B4051EF86407B82CC5E2AF9434F8B";
    System.out.println(str.substring(str.indexOf("=")+1, str.indexOf("&")));
	}
    public static String  killQuotes(String parameter,char s){
		StringBuffer sb=new StringBuffer();
		for(int k=0;k<parameter.length();k++){
			if(parameter.charAt(k)!= s){
				sb.append(parameter.charAt(k));
			}
		}
		return sb.toString();
	}	
	
    
    public static String GetSign(String parameter){
		parameter=parameter+",\"sign\":\""+GetSignValue(parameter)+"\"";
		return parameter;
	}
    
  //��ǩֵ
  	public static String GetSignValue(String parameter){
  		parameter=parameter.replace("\"", "");
  		parameter=parameter.replace(":", "=");
  		String arr[]=parameter.split(",");
  		Arrays.sort(arr);
  		parameter=Arrays.toString(arr);
  		parameter=parameter.toString().replace(", ", "&");
  		parameter=(parameter.substring(1,parameter.length()-1))+"&123456";
  		return LMFMD5.MD5(parameter);
  	}
}
