@Override public Header[] getRequestHeaders() { List<Header> headers = getRequestHeadersList(); // Add authentication header. if (userName != null && passWord != null) { byte[] base64bytes = Base64.encode( (userName + ":" + passWord).getBytes(), Base64.DEFAULT ); String credentials = new String(base64bytes); headers.add(new BasicHeader(HEADER_AUTHORIZATION, HEADER_BASIC + " " + credentials)); } return headers.toArray(new Header[headers.size()]); }
private static String hmac_sha1(String key,String message) { String reString = null; try { byte[] data = key.getBytes("UTF-8"); //根据给定的字节数组构造一个密钥,第二参数指定一个密钥算法的名称 SecretKey secretKey = new SecretKeySpec(data, "HmacSHA1"); //生成一个指定 Mac 算法 的 Mac 对象 Mac mac = Mac.getInstance("HmacSHA1"); //用给定密钥初始化 Mac 对象 mac.init(secretKey); byte[] text = message.getBytes("UTF-8"); //完成 Mac 操作 byte[] text1 = mac.doFinal(text); reString = Base64.encodeToString(text1, Base64.URL_SAFE); } catch (Exception e) { e.printStackTrace(); } return reString; }
/** * Convert a bitmap object to a Base64 encoded string. * @param image The image to convert. * @return A String in Base64 containing the image data. */ protected String base64FromBitmap(Bitmap image) { // Create a compressed byte array from the provided image. 80% JPEG seems to be enough // for accurate recognition ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); image.compress(CompressFormat.JPEG, 80, byteArrayOutputStream); byte[] byteArray = byteArrayOutputStream.toByteArray(); String encoded = Base64.encodeToString(byteArray, 0); return encoded; }
/** * 生成签名字符串 * * @param url * 网络地址 * @param method * 请求方法 * @param params * 请求参数 * @param appsecret * 签名密钥 * @param dbg * 是否开启调试:调式模式可打印签名日志 * @return */ public String makeSign(String url, String method, RequestParams params, String appsecret, boolean dbg) { StringBuilder sb = new StringBuilder(); sb.append(method).append("&"); sb.append(Uri.encode(url)).append("&"); // 排序 List<BasicNameValuePair> basicNameValuePairs = params.getParamsList(); Collections.sort(basicNameValuePairs, paramsCompre); String paramStr = URLEncodedUtils.format(basicNameValuePairs, "UTF-8"); sb.append(Uri.encode(paramStr)); String digit = TXSHAUtil.getSHA1(sb.toString()); byte[] encrypt = TXXXTEA.encrypt(digit.getBytes(), appsecret.getBytes()); String base64 = Base64.encodeToString(encrypt, Base64.DEFAULT); String sign = Uri.encode(base64); if (dbg) { TXDebug.o(new Exception(), "url : " + sb.toString()); TXDebug.o(new Exception(), "digit : " + digit); TXDebug.o(new Exception(), "base64 : " + base64); TXDebug.o(new Exception(), "sign : " + sign); } params.put("sign", sign); return sign; }
public static String getUploadToken(String fileName) { String token = null; try{ //一:将上传策略转换成Json格式 JSONObject policy = new JSONObject(); StringBuilder scope = new StringBuilder(); scope.append(bucket); scope.append(":"); scope.append(fileName); policy.put("scope",scope.toString()); //只允许上传指定key的文件,而key就是文件名 scope = null; policy.put("deadline",getTimer()); //policy.put("insertOnly",1); //不允许修改 //policy.put("callbackFetchKey",1); //启动fetchKey上传模式 // policy.put("returnBody ","{\"name\" : \"$(fname))}"); Log.d("policy",policy.toString()); //二:对JSON编码的上传策略进行URL安全的Base64编码,得到待签名字符串: String encodPutPolicy = Base64.encodeToString(policy.toString().getBytes("UTF-8"), Base64.URL_SAFE); Log.i("encodPutPolicy","encodPutPolicy is " + encodPutPolicy); //三:使用SecertKey对上一步生成的待签名字符串计算HMAC-SHA1签名:并对签名进行URL安全的Base64编码: String encodSign = hmac_sha1(SK,encodPutPolicy); Log.i("encodSign","encodSign is " + encodSign); //五:将AccessKey、encodedSign和encodedPutPolicy用:连接起来: token = AK + ':' + encodSign + ':' + encodPutPolicy; Log.i("token","token is " + token); }catch (Exception e) { e.printStackTrace(); } return token; }