Commit 1d1ff5f8 by zhiwei

提交新的js加密参数

parent 061bc022
......@@ -15,7 +15,7 @@
<dependency>
<groupId>com.zhiwei.crawler</groupId>
<artifactId>crawler-core</artifactId>
<version>0.5.2-SNAPSHOT</version>
<version>0.5.6.2-RELEASE</version>
<scope>provided</scope>
</dependency>
<dependency>
......
package com.zhiwei.toutiao.bean;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
* 今日头条签名类
* @author bewiler hk
*
*/
public class Signature {
private String cp;
private String as;
private String signature;
public Signature(String userId, String max_behot_time){
this.signature = this.getSign(userId, max_behot_time);
getASCP();
}
public Signature(){
getASCP();
}
public String getCp() {
return cp;
}
public String getAs() {
return as;
}
public String getSignature() {
return signature;
}
/**
* 获取加密参数
* @return
* @throws IOException
*/
private String getSign(String userId, String max_behot_time){
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
String jsText = getJSText(); // 读取js文件
String str = "0";
if(userId!=null){
str = userId + max_behot_time;
}
try {
engine.eval(jsText);
if(engine instanceof Invocable) {
Invocable invoke = (Invocable)engine;
String sign = invoke.invokeFunction("merge", str).toString();
return sign;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @Title: getAS
* @Description: TODO(获取今日头条加密值)
* @param @return 设定文件
* @return String 返回类型
*/
private void getASCP()
{
long i = (long)Math.floor(new Date().getTime()/1000L);
String t = Long.toHexString(i).toUpperCase();
char[] ts = t.toCharArray();
String e = parseStrToMd5L32(i+"").toString().toUpperCase();
char[] s = e.substring(0, 5).toCharArray();
char[] a = e.substring(e.length()-5,e.length()).toCharArray();
String c = "";
String o = "";
for(int n = 0; 5 > n; n++)
{
o += ""+s[n] + ts[n];
}
for (int r = 0; 5 > r; r++)
{
c += ""+ts[r + 3] + a[r];
}
String as = "A1" + o + t.substring(t.length()-3, t.length());
String cp = t.substring(0,3) + c + "E1";
this.as = as;
this.cp = cp;
}
/**
* 计算字符串Md5
* @Title: md5
* @param str
* @return String
*/
public static String md5(String str) {
String result = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = str.getBytes("utf-8");
md.update(bytes);
bytes = md.digest();
result = bytesToHexString(bytes);
} catch(Exception e) {}
return result;
}
/**
* 将二进制转换成16进制字符串
* @Title bytesToHexString
* @param buf
* @return String
*/
private static String bytesToHexString(byte bytes[]) {
String result = null;
if(bytes != null) {
if(bytes.length > 0) {
StringBuffer sb = new StringBuffer();
for(int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex);
}
result = sb.toString().toLowerCase();
}
}
return result;
}
/**
* @param str
* @return
* @Date: 2013-9-6
* @Author: lulei
* @Description: 32位小写MD5
*/
public static String parseStrToMd5L32(String str){
String reStr = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest(str.getBytes());
StringBuffer stringBuffer = new StringBuffer();
for (byte b : bytes){
int bt = b&0xff;
if (bt < 16){
stringBuffer.append(0);
}
stringBuffer.append(Integer.toHexString(bt));
}
reStr = stringBuffer.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return reStr;
}
/**
* 读取js文件
* @return
*/
private String getJSText() {
try {
StringBuffer sb = new StringBuffer();
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("signature.js");
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line = "";
while((line = br.readLine())!=null)
{
sb.append(line);
}
br.close();
return sb.toString();
} catch (IOException e) {
return null;
}
}
}
package com.zhiwei.toutiao.bean;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
* 今日头条签名类
* @author bewiler hk
*
*/
public class Signature {
private String cp;
private String as;
private String signature;
public Signature(String userId, String max_behot_time){
this.signature = this.getSign(userId, max_behot_time);
getASCP();
}
public Signature(){
getASCP();
}
public String getCp() {
return cp;
}
public String getAs() {
return as;
}
public String getSignature() {
return signature;
}
/**
* 获取加密参数
* @return
* @throws IOException
*/
private String getSign(String userId, String maxBehotTime){
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
String jsText = getJSText(); // 读取js文件
String str = "https://www.toutiao.com/toutiao/api/pc/feed/?category=pc_profile_ugc&utm_source=toutiao&visit_user_id=" + userId + "&max_behot_time=" + maxBehotTime;
try {
engine.eval(jsText);
if(engine instanceof Invocable) {
Invocable invoke = (Invocable)engine;
String sign = invoke.invokeFunction("merge", str).toString();
return sign;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @Title: getAS
* @Description: TODO(获取今日头条加密值)
* @param @return 设定文件
* @return String 返回类型
*/
private void getASCP()
{
long i = (long)Math.floor(new Date().getTime()/1000L);
String t = Long.toHexString(i).toUpperCase();
char[] ts = t.toCharArray();
String e = parseStrToMd5L32(i+"").toString().toUpperCase();
char[] s = e.substring(0, 5).toCharArray();
char[] a = e.substring(e.length()-5,e.length()).toCharArray();
String c = "";
String o = "";
for(int n = 0; 5 > n; n++)
{
o += ""+s[n] + ts[n];
}
for (int r = 0; 5 > r; r++)
{
c += ""+ts[r + 3] + a[r];
}
String as = "A1" + o + t.substring(t.length()-3, t.length());
String cp = t.substring(0,3) + c + "E1";
this.as = as;
this.cp = cp;
}
/**
* 计算字符串Md5
* @Title: md5
* @param str
* @return String
*/
public static String md5(String str) {
String result = null;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] bytes = str.getBytes("utf-8");
md.update(bytes);
bytes = md.digest();
result = bytesToHexString(bytes);
} catch(Exception e) {}
return result;
}
/**
* 将二进制转换成16进制字符串
* @Title bytesToHexString
* @return String
*/
private static String bytesToHexString(byte bytes[]) {
String result = null;
if(bytes != null) {
if(bytes.length > 0) {
StringBuffer sb = new StringBuffer();
for(int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex);
}
result = sb.toString().toLowerCase();
}
}
return result;
}
/**
* @param str
* @return
* @Date: 2013-9-6
* @Author: lulei
* @Description: 32位小写MD5
*/
public static String parseStrToMd5L32(String str){
String reStr = null;
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] bytes = md5.digest(str.getBytes());
StringBuffer stringBuffer = new StringBuffer();
for (byte b : bytes){
int bt = b&0xff;
if (bt < 16){
stringBuffer.append(0);
}
stringBuffer.append(Integer.toHexString(bt));
}
reStr = stringBuffer.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return reStr;
}
/**
* 读取js文件
* @return
*/
private String getJSText() {
try {
StringBuffer sb = new StringBuffer();
InputStream is = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("signature.js");
BufferedReader br=new BufferedReader(new InputStreamReader(is));
String line = "";
while((line = br.readLine())!=null)
{
sb.append(line);
}
br.close();
return sb.toString();
} catch (IOException e) {
return null;
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment