package com.yeshi.buwan.util.zhibo;
|
import javax.crypto.Mac;
|
import javax.crypto.spec.SecretKeySpec;
|
|
public class HuaJiaoSign {
|
|
public static String sign(String randNum,String authtime,String uri) {
|
|
String ak = "10017"; //ak是在开放平台系统自动分配的App-ID
|
String sk = "SoAQK7tpN4BQ12bt5KLGPOgzRkFZoDRB"; //sk是在开放平台系统自动分配的Secret-Key
|
String rand_num = randNum; //生成随机数
|
String auth_time = authtime; //生成时间戳
|
|
//测试uri
|
// String uri = "/live/getFeeds?platform=server&tag=juhe&num=1";
|
|
String auth_string = ak + "\n" + auth_time + "\n" + rand_num + "\n" + uri;
|
String encrypt_string = Base64.encode(hmacSha1(auth_string, sk));
|
String authorization = ak + ":" + encrypt_string;
|
return authorization;
|
}
|
|
private static byte[] hmacSha1(String value, String key) {
|
try {
|
// Get an hmac sha1 key from the raw key bytes
|
byte[] keyBytes = key.getBytes();
|
SecretKeySpec signingKey = new SecretKeySpec(keyBytes, "HmacSHA1");
|
|
// Get an hmac sha1 Mac instance and initialize with the signing key
|
Mac mac = Mac.getInstance("HmacSHA1");
|
mac.init(signingKey);
|
|
// Compute the hmac on input data bytes
|
byte[] rawHmac = mac.doFinal(value.getBytes());
|
return rawHmac;
|
} catch (Exception e) {
|
throw new RuntimeException(e);
|
}
|
}
|
|
private static final class Base64 {
|
|
private static final int BASELENGTH = 128;
|
private static final int LOOKUPLENGTH = 64;
|
private static final int TWENTYFOURBITGROUP = 24;
|
private static final int EIGHTBIT = 8;
|
private static final int SIXTEENBIT = 16;
|
private static final int SIGN = -128;
|
private static char PAD = '=';
|
private static byte[] base64Alphabet = new byte[BASELENGTH];
|
private static char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH];
|
|
static {
|
for (int i = 0; i < BASELENGTH; ++i) {
|
base64Alphabet[i] = -1;
|
}
|
for (int i = 'Z'; i >= 'A'; i--) {
|
base64Alphabet[i] = (byte) (i - 'A');
|
}
|
for (int i = 'z'; i >= 'a'; i--) {
|
base64Alphabet[i] = (byte) (i - 'a' + 26);
|
}
|
|
for (int i = '9'; i >= '0'; i--) {
|
base64Alphabet[i] = (byte) (i - '0' + 52);
|
}
|
|
base64Alphabet['+'] = 62;
|
base64Alphabet['/'] = 63;
|
|
for (int i = 0; i <= 25; i++) {
|
lookUpBase64Alphabet[i] = (char) ('A' + i);
|
}
|
|
for (int i = 26, j = 0; i <= 51; i++, j++) {
|
lookUpBase64Alphabet[i] = (char) ('a' + j);
|
}
|
|
for (int i = 52, j = 0; i <= 61; i++, j++) {
|
lookUpBase64Alphabet[i] = (char) ('0' + j);
|
}
|
lookUpBase64Alphabet[62] = '+';
|
lookUpBase64Alphabet[63] = '/';
|
|
}
|
|
private static String encode(byte[] binaryData) {
|
|
if (binaryData == null) {
|
return null;
|
}
|
|
int lengthDataBits = binaryData.length * EIGHTBIT;
|
if (lengthDataBits == 0) {
|
return "";
|
}
|
|
int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP;
|
int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP;
|
int numberQuartet = fewerThan24bits != 0 ? numberTriplets + 1
|
: numberTriplets;
|
char encodedData[];
|
|
encodedData = new char[numberQuartet * 4];
|
|
byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0;
|
|
int encodedIndex = 0;
|
int dataIndex = 0;
|
|
for (int i = 0; i < numberTriplets; i++) {
|
b1 = binaryData[dataIndex++];
|
b2 = binaryData[dataIndex++];
|
b3 = binaryData[dataIndex++];
|
|
l = (byte) (b2 & 0x0f);
|
k = (byte) (b1 & 0x03);
|
|
byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
|
: (byte) ((b1) >> 2 ^ 0xc0);
|
byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
|
: (byte) ((b2) >> 4 ^ 0xf0);
|
byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6)
|
: (byte) ((b3) >> 6 ^ 0xfc);
|
|
encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
|
encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
|
encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3];
|
encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f];
|
}
|
|
// form integral number of 6-bit groups
|
if (fewerThan24bits == EIGHTBIT) {
|
b1 = binaryData[dataIndex];
|
k = (byte) (b1 & 0x03);
|
|
byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
|
: (byte) ((b1) >> 2 ^ 0xc0);
|
encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
|
encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4];
|
encodedData[encodedIndex++] = PAD;
|
encodedData[encodedIndex++] = PAD;
|
} else if (fewerThan24bits == SIXTEENBIT) {
|
b1 = binaryData[dataIndex];
|
b2 = binaryData[dataIndex + 1];
|
l = (byte) (b2 & 0x0f);
|
k = (byte) (b1 & 0x03);
|
|
byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2)
|
: (byte) ((b1) >> 2 ^ 0xc0);
|
byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4)
|
: (byte) ((b2) >> 4 ^ 0xf0);
|
|
encodedData[encodedIndex++] = lookUpBase64Alphabet[val1];
|
encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)];
|
encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2];
|
encodedData[encodedIndex++] = PAD;
|
}
|
|
return new String(encodedData);
|
}
|
}
|
}
|