最近使用腾讯云通信,忍不住要吐槽,若大的腾讯,居然连个写Java的人都没有,要不要这么轻视Java,连个像样点的Java版ServerSDK都不提供,只显示个“敬请期待”。
实在忍受不了管方提供的签名代码。就不能收拾收拾好吗。一分钟能对接的东西让人耗费半小时读你代码。
自己重新封装了个工具类,清爽多了:
public class QChatUtils {
private static final long EXPIRE_SECOND = Long.valueOf(60 * 60 * 24 * 180);
private static final String BC = "BC";
private static final String SHA256withECDSA = "SHA256withECDSA";
private static final String UTF8 = "UTF-8";
private QChatUtils() {}
private static Signature getSignatureWithPrivateKey(PrivateKey privateKey, String content) throws Exception {
Signature signature = Signature.getInstance(SHA256withECDSA, BC);
signature.initSign(privateKey);
signature.update(content.getBytes(Charset.forName(UTF8)));
return signature;
}
private static Signature getSignatureWithPublicKey(PublicKey publicKey, String content) throws Exception {
Signature signature = Signature.getInstance(SHA256withECDSA, BC);
signature.initVerify(publicKey);
signature.update(content.getBytes(Charset.forName(UTF8)));
return signature;
}
private static PrivateKey getPrivateKey(String privateKeyStr) throws IOException {
Security.addProvider(new BouncyCastleProvider());
Reader reader = new CharArrayReader(privateKeyStr.toCharArray());
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
PEMParser parser = new PEMParser(reader);
PrivateKeyInfo privateKeyInfo = (PrivateKeyInfo) parser.readObject();
parser.close();
PrivateKey privateKey = converter.getPrivateKey(privateKeyInfo);
return privateKey;
}
private static PublicKey getPublicKey(String publicKeyStr) throws IOException {
Reader reader = new CharArrayReader(publicKeyStr.toCharArray());
PEMParser parser = new PEMParser(reader);
JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
SubjectPublicKeyInfo subjectPublicKeyInfo = (SubjectPublicKeyInfo) parser.readObject();
parser.close();
PublicKey publicKey = converter.getPublicKey(subjectPublicKeyInfo);
return publicKey;
}
private static String getContent(String identifier, String time) {
StringBuffer sb = new StringBuffer();
sb.append("TLS.appid_at_3rd:").append(0).append("\n").append("TLS.account_type:").append(ThirdPartyUtils.QC_ACCOUNT_TYPE).append("\n").append("TLS.identifier:")
.append(identifier).append("\n").append("TLS.sdk_appid:").append(ThirdPartyUtils.QC_SDK_APP_ID).append("\n").append("TLS.time:").append(time).append("\n")
.append("TLS.expire_after:").append(EXPIRE_SECOND).append("\n");
return sb.toString();
}
/**
* 生成UserSig
*
* @param identifier 用户ID
* @return
* @throws Exception
*/
public static String getUserSig(String identifier) throws Exception {
String time = String.valueOf(System.currentTimeMillis() / 1000);
Signature signature = getSignatureWithPrivateKey(getPrivateKey(ThirdPartyUtils.QC_PRIVATE_KEY), getContent(identifier, time));
byte[] signatureBytes = signature.sign();
String sigTLS = Base64.encodeBase64String(signatureBytes);
Map<String, Object> map = new HashMap<String, Object>();
map.put("TLS.account_type", 0);
map.put("TLS.identifier", identifier);
map.put("TLS.appid_at_3rd", 0);
map.put("TLS.sdk_appid", ThirdPartyUtils.QC_SDK_APP_ID);
map.put("TLS.expire_after", EXPIRE_SECOND);
map.put("TLS.version", "201512300000");
map.put("TLS.sig", sigTLS);
map.put("TLS.time", time);
JSONObject jsonObject = JSONObject.fromObject(map);
String jsonString = jsonObject.toString();
Deflater deflater = new Deflater();
deflater.setInput(jsonString.getBytes(Charset.forName(UTF8)));
deflater.finish();
byte[] compressBytes = new byte[512];
int compressBytesLength = deflater.deflate(compressBytes);
deflater.end();
return new String(Base64Url.base64EncodeUrl(Arrays.copyOfRange(compressBytes, 0, compressBytesLength)));
}
/**
* 验证签名
*
* @param userSig 签名
* @param identifier 用户ID
* @return
* @throws Exception
*/
public static boolean checkUserSig(String userSig, String identifier) throws Exception {
Security.addProvider(new BouncyCastleProvider());
Base64 decoder = new Base64();
byte[] compressBytes = Base64Url.base64DecodeUrl(userSig.getBytes(Charset.forName(UTF8)));
Inflater inflater = new Inflater();
inflater.setInput(compressBytes, 0, compressBytes.length);
byte[] decompressBytes = new byte[1024];
int decompressLength = inflater.inflate(decompressBytes);
inflater.end();
String jsonString = new String(Arrays.copyOfRange(decompressBytes, 0, decompressLength));
JSONObject jsonObject = JSONObject.fromObject(jsonString);
String sigTLS = jsonObject.getString("TLS.sig");
byte[] signatureBytes = decoder.decode(sigTLS.getBytes(Charset.forName(UTF8)));
String sigTime = jsonObject.getString("TLS.time");
Signature signature = getSignatureWithPublicKey(QChatUtils.getPublicKey(ThirdPartyUtils.QC_PUBLIC_KEY), getContent(identifier, sigTime));
return signature.verify(signatureBytes);
}
public static void main(String[] args) {
try {
String userSig = QChatUtils.getUserSig("liam.huang@foxmail.com");
System.out.println("UserSig: " + userSig);
boolean checkResult = QChatUtils.checkUserSig(userSig, "liam.huang@foxmail.com");
System.out.println("Check UserSig Result: " + checkResult);
} catch (Exception e) {
e.printStackTrace();
}
}
}
使用的时候只需要调用
QChatUtils.getUserSig(identifier); // identifier