下面列出了 org.apache.commons.codec.binary.Hex # decode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static String generateSignature(String appCertificate, short service, String appID, int unixTs, int salt, String channelName, long uid, int expiredTs, TreeMap<Short, String> extra) throws Exception {
// decode hex to avoid case problem
Hex hex = new Hex();
byte[] rawAppID = hex.decode(appID.getBytes());
byte[] rawAppCertificate = hex.decode(appCertificate.getBytes());
Message m = new Message(service, rawAppID, unixTs, salt, channelName, (int)(uid & 0xFFFFFFFFL), expiredTs, extra);
byte[] toSign = pack(m);
return new String(Hex.encodeHex(DynamicKeyUtil.encodeHMAC(rawAppCertificate, toSign), false));
}
/**
* Converts an hex back to an byte array.
*
* @param hexString the hex string input
* @return the byte array.
*/
public static byte[] fromHexToBytes(String hexString) {
final Hex codec = new Hex();
try {
return codec.decode(StringEncoder.getBytes(hexString));
} catch (DecoderException e) {
throw new IllegalArgumentException(
hexString + " could not be decoded. " + ExceptionUtils
.getMessage(e), e);
}
}
private static byte[] getBytesInternal(final String hexString) throws DecoderException {
final Hex codec = new Hex();
final String paddedHexString = 0 == hexString.length() % 2 ? hexString : "0" + hexString;
final byte[] encodedBytes = StringEncoder.getBytes(paddedHexString);
return codec.decode(encodedBytes);
}
private static byte[] getBytesInternal(final String hexString) throws DecoderException {
final Hex codec = new Hex();
final String paddedHexString = 0 == hexString.length() % 2 ? hexString : "0" + hexString;
final byte[] encodedBytes = StringEncoder.getBytes(paddedHexString);
return codec.decode(encodedBytes);
}